目的
[2019/12/24 インデントと共に設定できるように変更・見やすくした]
[2018/10/22 他のキーマップと被っていて不便だったので修正しました]
[2019/3/12 複数の言語に対応させました]
最近,vimを使ってC++のソースコードを編集する機会が多い.
試行錯誤する際に便利なのが"yy"で1行ヤンクして"p"でコピーして別パターンを試す流れである.
その時に1行まるごとコメントアウトをするのだが,この動作をすることが多いため,簡単にできないかと思い,無理やりショートカットを作成した.
Ctrl+i : コメントアウト
Ctrl+s : アンコメントアウト(実際には先頭文字を消去する動作)
ショートカットの作成
~/.vimrc に以下を記述.
nnoremap <C-i> 0i//<Esc>
nnoremap <C-s> 0xx
使った感想
普段だったらインサートモードにして"//"を挿入してEscでノーマルモードにしていたのだが,そこがひとまとまりの動作になったのでとても便利であった.
しかし,この設定ではC++のみにしか対応できず,他の言語を用いる場合にはvimrcを書き換える必要があり,不便である.そこで,ファイルの拡張子によって分岐させる方法でキーマップを設定した.
複数言語に対して対応するように変更したもの
普段はC++とPythonとTexしか使っていないのだが,ついでに他の言語に対しても設定を作成した.
if has("autocmd")
filetype plugin indent on
autocmd FileType c setlocal sw=2 sts=2 ts=2 noexpandtab
autocmd FileType c nnoremap <buffer> <C-i> <Home>i//<Esc>
autocmd FileType c nnoremap <buffer> <C-f> <Home>"_x"_x<Esc>
autocmd FileType c nnoremap <buffer> <C-b> :make
autocmd FileType c nnoremap <buffer> <C-e> :make run
autocmd FileType cpp setlocal sw=2 sts=2 ts=2 noexpandtab
autocmd FileType cpp nnoremap <buffer> <C-i> <Home>i//<Esc>
autocmd FileType cpp nnoremap <buffer> <C-f> <Home>"_x"_x<Esc>
autocmd FileType cpp nnoremap <buffer> <C-b> :make
autocmd FileType cpp nnoremap <buffer> <C-e> :make run
autocmd FileType python setlocal sw=2 sts=2 ts=2 noexpandtab
autocmd FileType python nnoremap <buffer> <C-i> <Home>i#<Esc>
autocmd FileType python nnoremap <buffer> <C-f> <Home>"_x<Esc>
autocmd FileType python nnoremap <buffer> <C-e> :terminal python3 %
autocmd FileType html setlocal sw=2 sts=2 ts=2 noexpandtab
autocmd FileType html nnoremap <buffer> <C-i> <End>a--><Esc><Home>i<!--<Esc>
autocmd FileType html nnoremap <buffer> <C-f> <End>xxx<Esc><Home>xxxx<Esc>
autocmd FileType ruby setlocal sw=2 sts=2 ts=2 noexpandtab
autocmd FileType js setlocal sw=2 sts=2 ts=2 noexpandtab
autocmd FileType javasctipt nnoremap <buffer> <C-i> <Home>i//<Esc>
autocmd FileType javasctipt nnoremap <buffer> <C-f> <Home>xx<Esc>
autocmd FileType zsh setlocal sw=2 sts=2 ts=2 noexpandtab
autocmd FileType scala setlocal sw=2 sts=2 ts=2 noexpandtab
autocmd FileType json setlocal sw=2 sts=2 ts=2 noexpandtab
autocmd FileType css setlocal sw=2 sts=2 ts=2 noexpandtab
autocmd FileType css nnoremap <buffer> <C-i> <End>a*/<Esc><Home>i/*<Esc>
autocmd FileType css nnoremap <buffer> <C-f> <End>xx<Esc><Home>xx<Esc>
autocmd FileType scss setlocal sw=2 sts=2 ts=2 noexpandtab
autocmd FileType sass setlocal sw=2 sts=2 ts=2 noexpandtab
autocmd FileType sh nnoremap <buffer> <C-i> <Home>i#<Esc>
autocmd FileType sh nnoremap <buffer> <C-f> <Home>x<Esc>
endif