0 votes
in VIM by
How do you search and replace a string in Vim both within a single file and across multiple files in a directory?

1 Answer

0 votes
by

In Vim, to search and replace a string within a single file, use the command :%s/search_string/replacement_string/g. This replaces all occurrences of ‘search_string’ with ‘replacement_string’. To confirm each replacement, add ‘c’ at the end (: %s/search_string/replacement_string/gc).

For multiple files in a directory, you’ll need to leverage Vim’s ability to execute shell commands. First, open Vim in the directory containing the files. Then, use the following command: :args *.txt | argdo %s/search_string/replacement_string/ge | update. This will find and replace ‘search_string’ with ‘replacement_string’ in all .txt files in the current directory.

...