Linux uses vim for multiple file lookup and replacement methods

  
                

vim is a commonly used text editor for Linux. By using multiple shell commands to find and replace multiple files, what specific commands will you use? The following small series will introduce you to the Linux system vim multi-file search and replace method.

On the linux platform, there are many excellent shell command to do a combination of multi-file search /replace, such as these commands: find, sed, grep, awk, perl. But other platforms such as Windows are not so convenient. At this time, vim's built-in multi-file search command is useful, although it is slightly slower than external commands.

vim multi-file lookup

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

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

:vim[grep][! ] {pattern} {file} . . .

The file part supports wildcards, * represents the current directory, ** represents the current directory and its subdirectories (recursive), such as */*.c represents the c source file in the current directory, **/*. c represents all source files in the current directory and its recursive subdirectories. The file part can be specified multiple times.

The following command looks at the matching result:

:cn[ext] Next result

:cp[revious] Previous result

:cw[indow ] quickfix window, list of result files

For more detailed usage see: help vimgrep and refs below.

vim multi-file replacement

In fact, you can just use the following two commands (assuming you want to replace the hate in the file with the extension .txt/.cpp in the current directory with love) :

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

Copyright © Windows knowledge All Rights Reserved