How Linux uses Vim to find and replace multiple files

Source: Internet
Author: User

Vim is a common text editor under the Linux system, which can be used to find and replace multiple files by using a variety of shell commands. The following small series to introduce you to the Linux system vim multi-File search and replace method.

On the Linux platform, there are a number of excellent shell command combinations to do long file lookups/substitutions, such as these: Find,sed,grep,awk,perl. But other platforms such as windows are not so convenient, when the vim built-in multi-file Search command is useful, although a little bit slower than the external command.

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), such as */*.C represents the C source program file under the current directory, and **/*.C represents all the 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 usage see: Help Vimgrep and refs below.

Vim Multi-file replacement

As a matter of fact, the following two commands are required (assuming that you want to replace the hate in all files with the. txt/.cpp file name in the current directory with Love):

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

: args *.txt *.cpp

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

Complete, explained as follows:

: args *.txt *.cpp

: args *.txt *.cpp

This will scan the. txt and. cpp files in the current directory and add them to the parameter list. But this will only rice seedlings the current directory, if you want to recursively scan all sub-directories,

: Args **/*.txt

: Args **/*.txt

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

: Args */*.txt

: Args */*.txt

and

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

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

is to change the hate of all the 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 also be interrupted).

Last but not least, when using the Replace command, always remember to back up, because the replacement is written directly to the hard disk.

The above is the Linux system vim multi-file Find and Replace methods introduced, through the find,sed and other commands to achieve multi-file related processing, you can also use VIM built-in multi-file Search command.

How Linux uses Vim to find and replace multiple files

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.