Search and replace in Vim across all the project files

后端 未结 11 1888
我寻月下人不归
我寻月下人不归 2020-12-07 07:00

I\'m looking for the best way to do search-and-replace (with confirmation) across all project files in Vim. By \"project files\" I mean files in the current directory, some

相关标签:
11条回答
  • 2020-12-07 07:28

    The other big option here is simply not to use vim:

    sed -i 's/pattern/replacement/' <files>
    

    or if you have some way of generating a list of files, perhaps something like this:

    find . -name *.cpp | xargs sed -i 's/pattern/replacement/'
    grep -rl 'pattern1' | xargs sed -i 's/pattern2/replacement/'
    

    and so on!

    0 讨论(0)
  • 2020-12-07 07:31

    Basically, I wanted the replace in a single command and more importantly within vim itself

    Based on the answer by @Jefromi i've created a keyboard shortcut, which I had set in my .vimrc file like this

    nmap <leader>r :!grep -r -l  * \| xargs sed -i -e 's///g'
    

    now from the vim, on a stroke of leader+r I get the command loaded in vim, which i edit like below,

    :!grep -r -l <find> <file pattern> | xargs sed -i -e 's/<find>/<replace>/g'
    

    Hence, I do the replace with a single command and more importantly within vim itself

    0 讨论(0)
  • 2020-12-07 07:32

    Greplace works well for me.

    There's also a pathogen ready version on github.

    0 讨论(0)
  • 2020-12-07 07:37

    Populate :args from a shell command

    It's possible (on some operating systems1)) to supply the files for :args via a shell command.

    For example, if you have ack2 installed,

    :args `ack -l pattern`
    

    will ask ack to return a list of files containing 'pattern' and put these on the argument list.

    Or with plain ol' grep i guess it'd be:

    :args `grep -lr pattern .`  
    


    You can then just use :argdo as described by the OP:

    :argdo %s/pattern/replacement/gce
    


    Populate :args from the quickfix list

    Also check out nelstrom's answer to a related question describing a simple user defined command that populates the arglist from the current quickfix list. This works great with many commands and plugins whose output ends up in the quickfix list (:vimgrep, :Ack3, :Ggrep4).

    The sequence to perform a project wide search could then be done with:

    :vimgrep /pattern/ **/*
    :Qargs 
    :argdo %s/findme/replacement/gc
    

    where :Qargs is the call to the user defined command that populates the arglist from the quickfix list.

    You'll also find links in the ensuing discussion to simple plugins that get this workflow down to 2 or 3 commands.

    Links

    1. :h {arglist} - vimdoc.sourceforge.net/htmldoc/editing.html#{arglist}
    2. ack - betterthangrep.com/
    3. ack.vim - github.com/mileszs/ack.vim
    4. fugitive - github.com/tpope/vim-fugitive
    0 讨论(0)
  • 2020-12-07 07:38
    1. Make sure you’re using Neovim (or Vim 7.4.8+, but really just use Neovim)
    2. Install FZF for the command line and as a vim plugin
    3. Install Ag, so that it’s available automatically to FZF in vim
    4. If using iTerm2 on OSX, set the alt/option key to Esc+

    Usage

    Search the text you want to change in the current directory and it’s children with

    :Ag text

    • Keep typing to fuzzy filter items
    • Select items with alt-a
    • Deselect items with alt-d
    • Enter will populate the quickfix list
    • :cfdo %s/text/newText/g | :w

    Now you have chabges made inside Vim NeoVim

    source

    0 讨论(0)
  • 2020-12-07 07:45

    If you don't mind of introducing external dependency, I have brewed a plugin ctrlsf.vim (depends on ack or ag) to do the job.

    It can format and display search result from ack/ag, and synchronize your changes in result buffer to actual files on disk.

    Maybe following demo explains more

    ctrlsf_edit_demo

    0 讨论(0)
提交回复
热议问题