UNIX / Linux: vi / vim perform search and replace operation

https:/‮ual.www/‬tturi.com
UNIX / Linux: vi / vim perform search and replace operation

To perform a search and replace operation in vi or vim, you can use the :%s/old-text/new-text/g command. This command will search for all occurrences of "old-text" in the current file and replace them with "new-text". The g at the end of the command tells vim to perform the replacement globally, meaning it will replace all occurrences of "old-text" in the entire file, not just the first occurrence on each line.

Here's an example of how to use the :%s command:

:%s/old-text/new-text/g

You can also specify a range of lines to search and replace within. For example, to search and replace only within lines 10 to 20, you can use the following command:

:10,20s/old-text/new-text/g

You can also use the :%s command with regular expressions to perform more sophisticated search and replace operations. For example, to replace all occurrences of "old-text" with "new-text" only if "old-text" is at the beginning of a line, you can use the following command:

:%s/^old-text/new-text/g
Created Time:2017-10-30 14:27:23  Author:lautturi