初めてのNeovim。プラグインマネージャー設定&プラグイン設定
こんにちは。ke1nyです。
Reactを独学し、簡単なCRUDアプリを作りたいなと考えていて、
開発のモチベーションを高めるため、新しいエディタ環境で開発していきたいと思い、Neovimに挑戦しています。
ここで記載している新しいエディタとは、時代の最先端の開発しているエディタという意味ではなく、個人で学習するのに新しく知識を身につけるという意味です。
前回は、Neovimの環境構築を行い、設定ファイルの構築までしました。
次は、プラグインを読み込む設定をしていきたいところですが、その前に
プラグインマネージャーというのをインストールします。
プラグインマネージャーは言葉の通り、プラグインをマネージャーする。
つまり、管理するためのツールですね。
こちらも多くのプラグインがありますが、今回はvim-plugを導入してみようと思います。vim-plug挫折しました!!笑 なので、lazy.nvimに乗り換えました。
気が向いたら、lazy.nvim版のプラグイン設定内容を残します。
この記事は備忘録として残しています。下記の内容はvim-plugの書き方です。
ターミナルで、vimと入力したときにNeovimが起動するよう設定
vi ~/.zshrc
alias vim="nvim"
この設定をしていると、ターミナルを開いてvimと入力すると
Neovimが起動してくれるので便利!
プラグインを使用するために、ファイルを作成
まず初めに、プラグイン用のファイルを作成しNeovim起動します。
cd ~/.config/nvim/lua
mkdir plugins && cd plugins
nvim setup_plugin.lua
vim-plugのインストール方法
以下のプログラムを、setup_plugin.luaファイル内に記載します。
local fn = vim.fn
local command = vim.api.nvim_command
local install_path = fn.stdpath('data')..'/site/autoload/plug.vim'
if fn.empty(fn.glob(install_path)) > 0 then
fn.system({'curl', '-fLo', install_path, '--create-dirs',
'https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim'})
end
-- Vim-Plugのインストールが完了したら、プラグインをインストールする
vim.defer_fn(function()
-- プラグインの設定
command [[call plug#begin('~/.local/share/nvim/plugged')]]
command [[Plug 'junegunn/vim-easy-align']]
command [[call plug#end()]]
command 'PlugInstall --sync | source $MYVIMRC'
-- オプション設定
require('plugins.vim-easy-align')
end, 0)
簡単に何をしているのかを説明すると、vim-plugの環境がなければ、gitからvim-plugをインストールし、インストールが完了したらプラグインをインストール。
個人環境で使用したいオプション設定をするためrequireしているといった感じです。
プラグインのオプション設定をしたい場合は、新規にフォルダをluaフォルダ内に作成して、そのpathをrequireで読み込んであげれば良いですね。
そして、setup_plugin.luaもinit.luaからrequireすれば
プラグインを使用することができます。
ちなみにソースを見ると、毎回プラグインをインストールしなおしていると感じるかもしれませんが、vim-plugはインストールしたプラグインを自動的に認識して、インストール済みのプラグインをスキップするそうです。
なので気にする必要はないということ。
いやぁ〜プラグインマネージャーって便利ですね!
このソースコードを書くために、いろいろ調べて試行錯誤して、ようやく構築できました。vimの経験者からしたら、こんなの楽ちんの楽ちんかもしれませんが、いずれそう思えるよう頑張ってNeovim使いこなせるように日々精進していきます!
何のプラグインを入れようか
調べると色々なプラグインがあるんですねぇ〜正直悩みます…
私は昔からVisualStudioにお世話になっていたので、VisualStudioで実装できていた機能を実現できそうなプラグイン、かつ、私にとって開発モチベーションを高めてくれるプラグイン、React,Expressの開発に必要なLSPをインストールしようと思います。
開発モチベーションを高めるプラグイン
ファイルツリー
nvim-tree.lua:ファイルツリーを表示してくれる
command [[Plug 'nvim-tree/nvim-tree.lua']]
command [[Plug 'nvim-tree/nvim-web-devicons']]
アイコンが表示されない場合は、フォントがインストールされていないので
インストールする
brew tap homebrew/cask-fonts
brew install font-hack-nerd-font
ターミナルの設定で、フォントをHack Nerd Fontに設定すると
アイコン付きのフォルダツリーが表示されます。
pluginsフォルダ内にtree.luaを作成し以下を設定して、
setup_plugin.lua内でrequireを記述する
vim.g.loaded_netrw = 1
vim.g.loaded_netrwPlugin = 1
require("nvim-tree").setup({
sort_by = 'extension',
view = {
width = '20%',
side = 'right',
signcolumn = 'no',
},
on_attach = 'default'
})
ステータスライン
nvim-lualine/lualine.nvim:ステータスラインの見た目を変える
command [[Plug 'nvim-lualine/lualine.nvim']]
デフォルト設定をplugins.lualine.luaファイル内に記述し、
setup_plugin.lua内でrequireを記述する
require('lualine').setup {
options = {
icons_enabled = true,
theme = 'onedark',
component_separators = { left = '', right = ''},
section_separators = { left = '', right = ''},
disabled_filetypes = {
statusline = {},
winbar = {},
},
ignore_focus = {},
always_divide_middle = true,
globalstatus = false,
refresh = {
statusline = 1000,
tabline = 1000,
winbar = 1000,
}
},
sections = {
lualine_a = {'mode'},
lualine_b = {'branch', 'diff', 'diagnostics'},
lualine_c = {'filename'},
lualine_x = {'encoding', 'fileformat', 'filetype'},
lualine_y = {'progress'},
lualine_z = {'location'}
},
inactive_sections = {
lualine_a = {},
lualine_b = {},
lualine_c = {'filename'},
lualine_x = {'location'},
lualine_y = {},
lualine_z = {}
},
tabline = {},
winbar = {},
inactive_winbar = {},
extensions = {}
}
タブライン
barbar.nvim:Neovim上部のタブを管理するのに便利なプラグイン
何が便利かは実際に使用してみて、後日感想を追加できれば追加します。
command [[Plug 'romgrk/barbar.nvim']]
カラースキーマ
次に設定するシンタックスハイライトに対応するカラースキーマを入れる
この中で好きなものを選べばいいということだが、多い…
とりあえず個人的に気になったものをインストールしてみることにした
command [[Plug 'EdenEast/nightfox.nvim']]
pluginsフォルダ内にカラースキーマ用の設定ファイルを作成し
以下の設定を追加
local command = vim.api.nvim_command
command [[colorscheme nightfox]]
インストールできたのだが、カラースキームの色が違うときは、
Terminal.appではなくiTerm2をインストールして使いましょう!
True Colorに対応しているターミナルであればiTerm2以外でも良いと思います。
シンタックスハイライト
nvim-treesitter/nvim-treesitter
command [[Plug 'nvim-treesitter/nvim-treesitter']]
言語ごとに解析してくれて色付けもしてくれるらしい。カラースキーマが対応しているものを使用しないとうまく色付けがされないらしい。
ここら辺は、実際に開発して調整できれば調整していきたいと思った。
設定ファイルには、デフォルト設定を入れておく
require'nvim-treesitter.configs'.setup {
-- A list of parser names, or "all" (the five listed parsers should always be installed)
ensure_installed = { "c", "lua", "vim", "vimdoc", "query", "comment", "css", "html", "javascript", "json", "tsx", "typescript" },
-- Install parsers synchronously (only applied to `ensure_installed`)
sync_install = false,
-- Automatically install missing parsers when entering buffer
-- Recommendation: set to false if you don't have `tree-sitter` CLI installed locally
auto_install = true,
---- If you need to change the installation directory of the parsers (see -> Advanced Setup)
-- parser_install_dir = "/some/path/to/store/parsers", -- Remember to run vim.opt.runtimepath:append("/some/path/to/store/parsers")!
highlight = {
enable = true,
-- NOTE: these are the names of the parsers and not the filetype. (for example if you want to
-- disable highlighting for the `tex` filetype, you need to include `latex` in this list as this is
-- the name of the parser)
-- list of language that will be disabled
disable = { "c", "rust" },
-- Or use a function for more flexibility, e.g. to disable slow treesitter highlight for large files
disable = function(lang, buf)
local max_filesize = 100 * 1024 -- 100 KB
local ok, stats = pcall(vim.loop.fs_stat, vim.api.nvim_buf_get_name(buf))
if ok and stats and stats.size > max_filesize then
return true
end
end,
-- Setting this to true will run `:h syntax` and tree-sitter at the same time.
-- Set this to `true` if you depend on 'syntax' being enabled (like for indentation).
-- Using this option may slow down your editor, and you may see some duplicate highlights.
-- Instead of true it can also be a list of languages
additional_vim_regex_highlighting = false,
},
--ポストアップデートフックの有効or無効
updater = {
enable = true
}
}
Term2をインストールする前は、Mac標準のターミナルを使用していたので
色が変わっていなくて、色々と調べTrue Color対応でないと正しい色が表示されないことを知り、AppleさんTrue Color対応お願いしますよぉと心の中で思いました。早くiTerm2をインストールしておけば良かったと思ったが、色々と調べられて勉強になったので良しとする。
Visual Studio的なプラグイン
コーディング支援
coc.nvimかhrsh7th/nvim-cmp、neovim/nvim-lspconfigのどちらかを使うのが良いらしいが、とりあえず導入が簡単そうなcon.nvimを選択してみたものの、設定ファイルでゴニョゴニョできなかったので、hrsh7th/nvim-cmp、neovim/nvim-lspconfigとでコーディング支援の環境を整えてみようと思います。
ちなみに、coc.nvimでプラグインにインストールするときは下記を実行
command [[Plug 'neoclide/coc.nvim', {'branch': 'release'}]]
coc.nvimをインストールした場合、キーマップ用の設定ファイルに
以下を記述
vim.api.nvim_set_keymap('n', '<leader>cf', '<Plug>(coc-format)', { silent = true }) --format
vim.api.nvim_set_keymap('n', '<leader>rn', '<Plug>(coc-rename)', { silent = true }) --置換
coc.nvimプラグインのインストールは時間が掛かりました。
さて、本題のhrsh7th/nvim-cmp、neovim/nvim-lspconfigをプラグインマ
ネージャーに取り込んでいきましょう。
まず、初めにhrsh7th/nvim-cmpのプラグインを設定します。
これをインストールすることで、適当なファイルを開いて文字を入力すると、ファイル中に含まれる文字を候補として提示されます。
あとは以下のプラグインを追加しました。
folke/trouble.nvim :エラーを検出してくれる
telescope.nvim:ファイル検索・テキスト検索
windwp/nvim-autopairs:括弧やクォーテーションなどを自動で閉じてくれるプラグインです。
windwp/nvim-ts-autotag:オートタグ
windwp/nvim-autopairs:括弧やクォーテーションなどを自動で閉じる
MuniTanjim/prettier.nvim : ソース整形
mcauley-penney/tidy.nvim:空白を削除
便利なプラグインが沢山あるので、少しずつカスタマイズして上達していければ良いなと思ってます。
この記事が気に入ったらサポートをしてみませんか?