Vim multi-File search and replacement tutorial in Linux

Source: Internet
Author: User

On the Linux platform, there are many good shell command combinations to do multiple file lookups/replacements, such as these commands: Find,sed,grep,awk,perl. But other platforms such as windows are less convenient, and Vim's built-in multiple file lookup commands are useful, although slightly slower than external commands.

VIM Multi-file lookup

This vim built-in command is Vimgrep, and there are two basic ways to use it:

: vim[grep][!]/{pattern}/[g][j] {file} ...
: vim[grep][!] {Pattern} {file} ...

The file section supports wildcards, * represents the current directory, * * represents the current directory and its subdirectories (recursion), for example, */*.C represents the C source program file in the current directory, and **/*.C represents all source program files under the current directory and its recursive subdirectories. The file section can be specified more than once.

The following command looks at the matching results:

: Cn[ext] Next result
: cp[revious] Previous result
: Cw[indow] Quickfix window, result file list

For more detailed use see: Help Vimgrep and the refs below.

Vim multi-file replacement


In fact, as long as the following two commands are available (assuming that you want to replace the hate in all files in the current directory with. Txt/.cpp):

: args *.txt *.cpp:argdo%s/hate/love/gc | Update

: args *.txt *.cpp
: Argdo%S/HATE/LOVE/GC | Update

will be done. Explained as follows,

: args *.txt *.cpp

: args *.txt *.cpp

This write scans the. txt and. cpp files in the current directory and adds them to the parameter list. But this writing will only seedlings the current directory, and if you want to recursively scan all the subordinate directories, use the

: Args **/*.txt

: Args **/*.txt

If you only want to scan the next level of the directory (that is, not scanning the current directory), use the

: Args */*.txt

: Args */*.txt

and

: Argdo%S/HATE/LOVE/GC | Update

: Argdo%S/HATE/LOVE/GC | Update

is to change the hate of all files in the argument list to love and write to the hard disk (if there is no |update, it will not be written, but the corresponding substitution will be interrupted).

Last but not least, when using a replacement command, always remember to back up, because the replacement is directly written to the hard drive drop oh ...

Well, I hope this article is useful to everyone ~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.