• 25-telescope-config
  • telescope.nvim

telescope.nvim

telescope.nvim

Setup

まずは telescope の UI の設定をしましょう。

特筆すべきは defaults.mappings.i で、ここで、検索中に使用できるキーバインドを登録します。 また、デフォルトのキーバインドも一部紹介します。

KeybindAction
DEFAULTS
<c-n>一つ下を選択する
<c-p>一つ上を選択する
<esc><esc>検索を閉じる
Additional
<esc>, jk検索を閉じる
<c-j>, <down>一つ次の検索履歴で検索する
<c-k>, <up>一つ昔の検索履歴で検索する
25-telescope-config/telescope.lua
local telescope = require("telescope")
local actions = require("telescope.actions")
 
telescope.setup({
  defaults = {
    mappings = {
      i = {
        ["<esc>"] = actions.close,
        ["jk"] = actions.close,
        -- cycle through previously done searches
        ["<c-j>"] = actions.cycle_history_next,
        ["<c-k>"] = actions.cycle_history_prev,
        ["<down>"] = actions.cycle_history_next,
        ["<up>"] = actions.cycle_history_prev,
      },
    },
    cache_picker = { num_pickers = 3 }, -- default 1
    -- put prompt (input box) position to the top
    layout_config = {
      prompt_position = "top",
    },
    -- put results in ascending order
    sorting_strategy = "ascending",
    -- shorten file names displayed
    path_display = {
      shorten = { len = 1, exclude = { -1, -2 } },
      smart = true,
      truncate = true,
    },
    -- ripgrep remove indentation
    vimgrep_arguments = {
      "rg",
      "--color=never",
      "--no-heading",
      "--with-filename",
      "--line-number",
      "--column",
      "--smart-case",
      "--trim",
    },
  },
  pickers = {
    -- remove ./ from fd results
    find_files = {
      find_command = { "fd", "--type", "f", "--strip-cwd-prefix" },
    },
  },
  extensions = {
    fzf = {
      fuzzy = true, -- false will only do exact matching
      override_generic_sorter = true, -- override the generic sorter
      override_file_sorter = true, -- override the file sorter
      case_mode = "smart_case", -- or "ignore_case" or "respect_case", the default case_mode is "smart_case"
    },
    media_files = {
      filetypes = { "png", "webp", "jpg", "jpeg", "mp4", "pdf" },
      find_cmd = "rg",
    },
  },
})
 
telescope.load_extension("fzf")
telescope.load_extension("media_files")
telescope.load_extension("dap")

Setup Pickers

次に、様々な Picker を起動するキーバインドを設定していきます。

すべてのピッカーに独自のキーを割り当てているとすぐにキーが枯渇してしまうので、多機能なプラグインのキーバインドを決めるときはキーバインドのプレフィックスを決めることが多いです。

私の場合は <leader>f をプレフィックスとして、その後にもう 1 文字足していろいろな種類のピッカーが立ち上がるようにしています。

例えば <leader>fp: Git File Picker, <leader>fl: File Picker, <leader>fs: Live Grep, ...といった具合です。

まずこれを 25-telescope-config/telescope.lua に追記してください。

25-telescope-config/telescope.lua
local ts_builtin = require("telescope.builtin")
local ts_prefix = "<leader>f"

telescope fzf bindings

はじめは telescope の組み込みのピッカーを何個かキーバインドで起動できるようにします。

それぞれの細かい動作については telescope.nvim - Pickersをご覧ください。

なお、ts_prefix .. "f" == "<leader>ff" です。

25-telescope-config/telescope.lua
-- telescope fzf bindings
vim.keymap.set("n", ts_prefix .. "f", ts_builtin.buffers, { desc = "ts.buffers" })
vim.keymap.set("n", ts_prefix .. "p", ts_builtin.git_files, { desc = "ts.git_files" })
vim.keymap.set("n", ts_prefix .. "l", ts_builtin.find_files, { desc = "ts.find_files" })
vim.keymap.set("n", ts_prefix .. "s", ts_builtin.live_grep, { desc = "ts.live_grep" })
vim.keymap.set("n", ts_prefix .. "h", ts_builtin.help_tags, { desc = "ts.help_tags" })
vim.keymap.set("n", ts_prefix .. "r", ts_builtin.resume, { desc = "ts.resume" })
vim.keymap.set("n", ts_prefix .. "j", ts_builtin.jumplist, { desc = "ts.jumplist" })
vim.keymap.set("n", ts_prefix .. "e", ts_builtin.symbols, { desc = "ts.symbols" })
vim.keymap.set("n", ts_prefix .. "C", ts_builtin.commands, { desc = "ts.commands" })
vim.keymap.set("n", ts_prefix .. "c", ts_builtin.command_history, { desc = "ts.command_history" })
vim.keymap.set("n", ts_prefix .. "i", telescope.extensions.media_files.media_files, { desc = "ts.media_files" })

telescope other bindings

普段のコーディングでは使えないですが、このピッカーはインストールされている colorscheme を検索することができます。

また、colorscheme を選択すると、その colorscheme をその場で適用してくれて、どんな感じなのかを見ることができます。(<leader><leader>fc)

25-telescope-config/telescope.lua
-- telescope other bindings
vim.keymap.set("n", "<leader>" .. ts_prefix .. "c", ts_builtin.colorscheme, { desc = "ts.colorscheme" })

telescope lsp bindings

次のらは LSP のセットアップが終わると使えるようになるピッカーです。

LSP がない状態で起動するとエラーが発生しますが、キーバインドとして登録するだけだったら問題ないので追記しておきましょう。LSP のセットアップで詳しく書きます。

25-telescope-config/telescope.lua
-- telescope lsp bindings
vim.keymap.set("n", ts_prefix .. "t", ts_builtin.lsp_document_symbols, { desc = "ts.lsp_document_symbols" })
vim.keymap.set("n", ts_prefix .. "y", ts_builtin.lsp_workspace_symbols, { desc = "ts.lsp_workspace_symbols" })
vim.keymap.set("n", ts_prefix .. "m", "<Cmd>TodoTelescope<CR>", { desc = "TodoTelescope @folke/todo-comments.nvim" })

telescope git bindings

Git 用ピッカーです。30-git-config で詳しく書きます。

25-telescope-config/telescope.lua
-- telescope git bindings
local ts_prefix_git = ts_prefix .. "g"
vim.keymap.set("n", ts_prefix_git .. "c", ts_builtin.git_commits, { desc = "ts.git_commits" })
vim.keymap.set("n", ts_prefix_git .. "l", ts_builtin.git_bcommits, { desc = "ts.git_bcommits" })
vim.keymap.set("n", ts_prefix_git .. "b", ts_builtin.git_branches, { desc = "ts.git_branches" })

telescope dap bindings

Dap (Debugger) 用ピッカーです。80-debug-config で詳しく書きます。

25-telescope-config/telescope.lua
-- telescope dap bindings
local ts_prefix_dap = ts_prefix .. "d"
vim.keymap.set("n", ts_prefix_dap .. "m", function()
telescope.extensions.dap.commands({})
end, { desc = "ts.dap.commands({})" })
vim.keymap.set("n", ts_prefix_dap .. "c", function()
telescope.extensions.dap.configurations({})
end, { desc = "ts.dap.configurations({})" })
vim.keymap.set("n", ts_prefix_dap .. "b", function()
telescope.extensions.dap.list_breakpoints({})
end, { desc = "ts.dap.list_breakpoints({})" })
vim.keymap.set("n", ts_prefix_dap .. "v", function()
telescope.extensions.dap.variables({})
end, { desc = "ts.dap.variables({})" })
vim.keymap.set("n", ts_prefix_dap .. "f", function()
telescope.extensions.dap.frames({})
end, { desc = "ts.dap.frames({})" })
Last updated on October 9, 2022