Vi / VIM Find And Replace All Text Substitute Command

Originally published at: https://www.cyberciti.biz/faq/vim-text-editor-find-and-replace-all-text/

I am a new Linux user. I started to use vim or vi text editor. How do I find all occurrence of the word called ‘eth0’ and replace it with ‘br0’ on Linux operating systems?

You can use the substitute command. Type “:” to go to the command mode. Then
1,$s/eth0/br0/g

Explanation:

  • 1,$ from line 1 to the last line ($ means “last”)
  • s: the substitute command
  • / a character used to define the string. It can be something else. For example, if in the string you want to substitute there is a slash, you can use @.
  • eth0: the string you are looking for
  • / : character to delimit the first string and to start the second one
  • br0: the string you substituting to eth0
  • / again…
  • g: optional parameter. Measn global. If in one line you have more than one occurrence, all of them are substituted.

Very powerful command, and fast!

1 Like