NeoVimのvim-plugを使った一番シンプルな設定方法
以前の記事ではdeinやdeopleteなどを使ってneovimのプラグインを管理していたのですが、
そもそもそれほど多くのプラグインを使わない場合は、init.vimだけで記述した方が管理しやすいと思います。
なので、今自分はNeoVimの設定を、vim-plugというプラグインマネージャーを使うことで、
全ての記述をinit.vimだけで行うことにしました。
$ sw_vers
ProductName: macOS
ProductVersion: 11.2.3
BuildVersion: 20D91
vim-plugの導入
以下のコマンドをterminalで実行してvim-plugを導入します。
sh -c 'curl -fLo "${XDG_DATA_HOME:-$HOME/.local/share}"/nvim/site/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim'
以下のコマンドでプラグインの保存用ディレクトリを作成します。
mkdir -p ~/.config/nvim/plugged
以下のコマンドでinit.vimを作成し、
touch ~/.config/nvim/init.vim
編集します。
nvim ~/.config/nvim/init.vim
init.vimに以下をコピー&ペーストします
"行番号を表示
set number
"タブ文字の代わりにスペースを使う
set expandtab
"プログラミング言語に合わせて適切にインデントを自動挿入
set smartindent
"各コマンドやsmartindentで挿入する空白の量
set shiftwidth=4
"Tabキーで挿入するスペースの数
set softtabstop=4
"カレントディレクトリを自動で移動
set autochdir
"バッファ内で扱う文字コード
set encoding=utf-8
"書き込む文字コード : この場合encodingと同じなので省略可
set fileencoding=utf-8
"読み込む文字コード : この場合UTF-8を試し、だめならShift_JIS
set fileencodings=utf-8,cp932
"eコマンド等でTabキーを押すとパスを保管する : この場合まず最長一致文字列まで補完し、2回目以降は一つづつ試す
set wildmode=longest,full
"LeaderキーをSpaceに設定(これだけでは意味をなさない)
let mapleader = "\<Space>"
"C++,Java等のインラインブロックを中括弧付きのブロックに展開
nnoremap <C-j> ^/(<CR>%a{<CR><Esc>o}<Esc>
"カーソル上の単語を置換
nnoremap <expr> S* ':%s/\<' . expand('<cword>') . '\>/'
"挿入モード終了時にIMEをオフ
inoremap <silent> <Esc> <Esc>:call system('fcitx-remote -c')<CR>
"ファイルタイプごとにコンパイル/実行コマンドを定義
function! Setup()
"フルパスから拡張子を除いたもの
let l:no_ext_path = printf("%s/%s", expand("%:h"), expand("%:r"))
"各言語の実行コマンド
let g:compile_command_dict = {
\'c': printf('gcc -std=gnu11 -O2 -lm -o %s.out %s && %s/%s.out', expand("%:r"), expand("%:p"), expand("%:h"), expand("%:r")),
\'cpp': printf('g++ -std=gnu++17 -O2 -o %s.out %s && %s/%s.out', expand("%:r"), expand("%:p"), expand("%:h"), expand("%:r")),
\'java': printf('javac %s && java %s', expand("%:p"), expand("%:r")),
\'cs': printf('mcs -r:System.Numerics -langversion:latest %s && mono %s/%s.exe', expand("%:p"), expand("%:h"), expand("%:r")),
\'python': printf('python3 %s', expand("%:p")),
\'ruby': printf('ruby %s', expand("%:p")),
\'javascript': printf('node %s', expand("%:p")),
\'sh': printf('chmod u+x %s && %s', expand("%:p"), expand("%:p"))
\}
"実行コマンド辞書に入ってたら実行キーバインドを設定
if match(keys(g:compile_command_dict), &filetype) >= 0
"下ウィンドウがターミナルであることを前提としている
nnoremap <expr> <F5> '<C-w>ji<C-u>' . g:compile_command_dict[&filetype] . '<CR>'
endif
endfunction
command! Setup call Setup()
"ファイルを開き直したときに実行コマンドを再設定
autocmd BufNewFile,BufRead * Setup
"RubyとJSではインデントを2マスにする
autocmd FileType ruby,javascript set shiftwidth=2 softtabstop=2
" undoを編集前に戻せるように設定
if has('persistent_undo')
set undodir=~/.config/nvim/undo
set undofile
endif
" プラグインの設定
" :PlugInstall でインストール
call plug#begin('~/.config/nvim/plugged')
" プラグインをここに追記していく
Plug 'tpope/vim-commentary'
Plug 'neoclide/coc.nvim', {'branch': 'release'}
Plug 'preservim/nerdtree'
Plug 'ryanoasis/vim-devicons'
Plug 'nathanaelkane/vim-indent-guides'
Plug 'ryanoasis/vim-devicons'
call plug#end()
" NERDTreeの設定
nmap <C-t> :NERDTreeToggle<CR>
" vim-indent-guidesの設定
set tabstop=2 shiftwidth=2 expandtab
let g:indent_guides_enable_on_vim_startup = 1
let g:indent_guides_exclude_filetypes = ['help', 'nerdtree']
let g:indent_guides_start_level = 2
nvimのコマンドモードで、:PlugInstall を実行して、記載したプラグインをインストールします。
最低限のneovimです