🐷

【NeoVim】telescope.nvimの設定と使い方

に公開

はじめに

telescope.nvimはNeoVim用プラグインです。ファイルのインクリメンタルサーチやgrepもtelescopeを通じてできるようになります。自分は現在ツリー表示のファイラーを使わず、telescopeでファイルを探していますが、今のところ快適に使用できています。

事前準備

事前準備として以下を行います。

  • Neovim(0.9.0以上)のインストール
  • ripgrepのインストール
  • fdのインストール
  • telescope.nvimのインストール

Neovim(0.9.0以上)のインストール

curl -LO https://github.com/neovim/neovim/releases/latest/download/nvim-linux-x86_64.appimage
chmod u+x nvim-linux-x86_64.appimage
./nvim-linux-x86_64.appimage

ripgrepのインストール

sudo apt install ripgrep

fdのインストール

sudo apt install fd-find

telescope.nvimのインストール

パッケージ管理にはpackerを使用しています。設定ファイルは以下のように管理しています。詳しくは
【NeoVim】自分用のNeoVim環境を作った話を参照ください。

~/.config/nvim
└ init.lua                # luaディレクトリ配下の設定ファイルを読み込む
   ├ lua
   │ ├ plugins.lua        # プラグインの読み込み
   │ ├ telescope_cfg.lua  # telescope.nvim固有設定
   └ plugin
     └ packer_compiled.lua

プラグインの設定は以下のようにしています。

~/.config/nvim/lua/plugins.lua
vim.cmd([[packadd packer.nvim]])

require('packer').startup(function(use)
    use 'nvim-telescope/telescope.nvim'         -- ファイル検索
    use 'nvim-lua/plenary.nvim'                 -- Telescopeの依存ライブラリ
end)

telescope.nvimの設定は以下です。

~/.config/nvim/lua/telescope_cfg.lua
local telescope = require('telescope')

telescope.setup {
    defaults = {
        layout_config = {
            width = 0.75,
        },
        file_ignore_patterns = {
            "%.git/",
            "%vendor",
        },
    },
    pickers = {
        find_files = {
            hidden = true,
        },
    },
}

local vim = vim
vim.api.nvim_set_keymap('n', '<Leader>ff', "<cmd>Telescope find_files<CR>", { noremap = true, silent = true })
vim.api.nvim_set_keymap('n', '<Leader>fg', "<cmd>Telescope live_grep<CR>", { noremap = true, silent = true })
vim.api.nvim_set_keymap('n', '<Leader>fb', "<cmd>Telescope buffers<CR>", { noremap = true, silent = true })
vim.api.nvim_set_keymap('n', '<Leader>fh', "<cmd>Telescope help_tags<CR>", { noremap = true, silent = true })

プラグインのインストール

設定ファイルを保存後、neovimを開きなおして以下のコマンドを入力し、プラグインをインストールします。

:PackerSync

プラグインのインストール後確認

インストール後、以下のコマンドを入力して正しくインストールされているか確認します。

:checkhealth telescope

以下のように表示されれば正常にインストールされています。

==================================
telescope: health#telescope#check

Checking for required plugins ~
- OK plenary installed.
- OK nvim-treesitter installed.

Checking external dependencies ~
- OK rg: found ripgrep 11.0.2
- OK fd: found fd 7.4.0

===== Installed extensions ===== ~

telescope.nvimの基本的な使い方

以下は基本的なショートカットキーです。<leader>キーは各環境のものに読み替えてください。

  • ファイルを検索する: <Leader>ff
  • grepする: <Leader>fg
  • bufferの内容表示: <Leader>fb
  • ヘルプタグの表示: <Leader>fh

ファイル検索画面表示時の主なショートカットキーは以下です。

  • Ctrl+?: ショートカットキーを上部に表示
  • Ctrl+p/n: 1つ上/下へ移動
  • Enter: 選択されているファイルを開く
  • Ctrl+v: 選択されているファイルを垂直に分割して表示

Discussion