Vim Trick: Replace One String with Another

These 3 vim tricks are very usefull when you need to replace strings in text files.

1. This “vim command” replaces string1 with string2 :s/string1/string2/ only in the first string1 match, on the current line.

[Esc]:s/string1/string2/

2. This replaces string1 with string2 :s/string1/string2/g on the current line, replacing all the string1 matches.

[Esc]:s/string1/string2/g

3. To replace string1 with string2 globally, all the matches in the text file, use this “vim command” :%s/string1/string2/g

[Esc]:%s/string1/string2/g

These replace “vim commands syntax” is similar with the sed replace command: s/alfa/beta/g.

To read the other vim articles, go here.

Tagged with: , , , , ,
Posted in The Linux and Unix Articles!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Subscribe

  

Subscribe to get the latest Linux news and how to guides directly on your e-mail!