Panda Noir

JavaScript の限界を究めるブログでした。最近はいろんな分野を幅広めに書いてます。

tcomment_vimをドットリピートできるようにする

repeat.vimを併用することで対応させます。

# dein.toml
[[plugins]]
repo = 'tpope/vim-repeat'

[[plugins]]
repo = 'tomtom/tcomment_vim'
hook_add = '''
au MyAutoCmd VimEnter * nnoremap <silent> <Plug>RepeatTComment :TComment<Bar>
            \ silent! call repeat#set("\<Plug>RepeatTComment")<CR>
au MyAutoCmd VimEnter * nmap <c-_><c-_> <Plug>RepeatTComment
'''

解説

根幹となるのは「nnoremap <silent> <Plug>RepeatTComment」と「nmap <c-_><c-_> <Plug>RepeatTComment」の部分です。

前者は<Plug>RepeatTComment:TComment <Bar> ...を割り当てています。:TCommentを実行したあとにsilent! call repeat#set("\<Plug>RepeatTComment")<CR>を実行しているだけです。repeat.vimはドットを押すと、repeat#setで登録したコマンドが実行されるようになっているので、<Plug>RepeatTCommentが呼ばれた直後にrepeat#setで登録することで繰り返しを実現します。

これが大まかな部分で、残りのコード(au MyAutoCmd VimEnter *など)は読み込むタイミングを制御しているだけです。

注意

上のコードではaugroup MyAutoCmdを行っています。詳しい解説はこの記事ではしないので、以下の参考リンクなどをお読みください。

参考