Search and replace in Vim across all the project files

后端 未结 11 1889
我寻月下人不归
我寻月下人不归 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:46

    maybe do this:

    :noautocmd vim /Search/ **/*
    :set hidden
    :cfirst
    qa
    :%s//Replace/gce
    :cnf
    q
    1000@a
    :wa
    

    Explanation:

    • :noautocmd vim /Search/ **/* ⇒ lookup (vim is an abbreviation for vimgrep) pattern in all files in all subdirectories of the cwd without triggering autocmds (:noautocmd), for speed's sake.
    • :set hidden ⇒ allow having modified buffers not displayed in a window (could be in your vimrc)
    • :cfirst ⇒ jump to first search result
    • qa ⇒ start recording a macro into register a
    • :%s//Replace/gce ⇒ replace all occurrences of the last search pattern (still /Search/ at that time) with Replace:
      • several times on a same line (g flag)
      • with user confirmation (c flag)
      • without error if no pattern found (e flag)
    • :cnf ⇒ jump to next file in the list created by the vim command
    • q ⇒ stop recording macro
    • 1000@a ⇒ play macro stored in register a 1000 times
    • :wa ⇒ save all modified buffers

    * EDIT * Vim 8 way:

    Starting with Vim 8 there is a better way to do it, as :cfdo iterates on all files in the quickfix list:

    :noautocmd vim /Search/ **/*
    :set hidden
    :cfdo %s//Replace/gce
    :wa
    
    0 讨论(0)
  • 2020-12-07 07:49

    I've decided to use ack and Perl to solve this problem in order to take advantage of the more powerful full Perl regular expressions rather than the GNU subset.

    ack -l 'pattern' | xargs perl -pi -E 's/pattern/replacement/g'
    

    Explanation

    ack

    ack is an awesome command line tool that is a mix of grep, find, and full Perl regular expressions (not just the GNU subset). Its written in pure Perl, its fast, it has syntax highlighting, works on Windows and its friendlier to programmers than the traditional command line tools. Install it on Ubuntu with sudo apt-get install ack-grep.

    xargs

    Xargs is an old unix command line tool. It reads items from standard input and executes the command specified followed by the items read for standard input. So basically the list of files generated by ack are being appended to the end of the perl -pi -E 's/pattern/replacemnt/g' command.

    perl -pi

    Perl is a programming language. The -p option causes Perl to create a loop around your program which iterates over filename arguments. The -i option causes Perl to edit the file in place. You can modify this to create backups. The -E option causes Perl to execute the one line of code specified as the program. In our case the program is just a Perl regex substitution. For more information on Perl command line options perldoc perlrun. For more information on Perl see http://www.perl.org/.

    0 讨论(0)
  • 2020-12-07 07:52
    1. :grep <search term> (or whatever you use to populate the quickfix window)
    2. :cfdo %s/<search term>/<replace term>/g | update
    

    Step 1 populates the quickfix list with items you want. In this case, it's propagated with search terms you want to change via grep.

    cfdo runs the command following on each file in the quickfix list. Type :help cfdo for details.

    s/<search term>/<replace term>/g replaces each term. /g means replace every occurrence in the file.

    | update saves the file after every replace.

    I pieced this together based upon this answer and its comments, but felt it deserved its own answer since it's all in one place.

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

    EDIT: Use cfdo command instead of cdo to significantly reduce the amount of commands that will be run to accomplish this (because cdo runs commands on each element while cfdo runs commands on each file)

    Thanks to the recently added cdo command, you can now do this in two simple commands using whatever grep tool you have installed. No extra plugins required!:

    1. :grep <search term>
    2. :cdo %s/<search term>/<replace term>/gc
    3. (If you want to save the changes in all files) :cdo update
    

    (cdo executes the given command to each term in the quickfix list, which your grep command populates.)

    (Remove the c at the end of the 2nd command if you want to replace each search term without confirming each time)

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

    One more option in 2016, far.vim plugin:

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