LoginSignup
5
4

More than 5 years have passed since last update.

denite.nvim で grep -> qfreplace をやる

Last updated at Posted at 2017-07-05

はじめに

いつもは丁寧に記事を書くのだが、今回はさっと書きます。

対象

  • vim/neovim を使っている
  • unite.vim を知ってる
  • 今は denite.nvim 使ってる

内容

unite.vim では以下の事ができたが、denite.nvim ではできなかった。

  • Unite grep する
  • 候補を選択する
  • replace-action を起動する(thinca/vim-qfreplace が必要)
  • 置換バッファが開くので、編集して保存
  • 複数ファイルにまたがった置換を行うことができる

denite.nvim は速度に優れているので、grep みたいなことはとても得意。
でも、grep 結果を一括置換できないので、うーん。と思っていた。

しかし、先日 denite.nvim に custom-action が搭載された。
これ、自分で qfreplace 叩けばできるんじゃないの?
unite.vim の replace-action を見てみたら、めちゃ簡単なコードだったので書いてみた。

実装

.vimrc

  if dein#tap('denite.nvim') && dein#tap('vim-qfreplace')
    function! MyDeniteReplace(context)
      let qflist = []
      for target in a:context['targets']
        if !has_key(target, 'action__path') | continue | endif
        if !has_key(target, 'action__line') | continue | endif
        if !has_key(target, 'action__text') | continue | endif

        call add(qflist, {
              \ 'filename': target['action__path'],
              \ 'lnum': target['action__line'],
              \ 'text': target['action__text']
              \ })
      endfor
      call setqflist(qflist)
      call qfreplace#start('')
    endfunction
    call denite#custom#action('file', 'qfreplace', function('MyDeniteReplace'))
  endif

※ 行儀悪い部分もあるが許して(g:, l: つけてないとか)

おわりに

プラグイン化しないの?とか言われそうなので、そこだけ言っておくとこんな簡単なコードをプラグインにしてもなあと思ったのと、このコードは 2 種類の plugin に依存したコードなので、みんな vimrc に書けばいいんじゃないのと思ったので、特に予定はないです。

5
4
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4