Vim Substitution

Vim Substitution

vim substitution is one tool any advanced vim user should know. right Basically, vim substitution is similar to search and replace used on other file editors but vim goes much further than just searching and replacing files and characters. For anyone who creates bash scripts and edits a ton of config files like myself, will attest to the need to comment and uncomment many lines at a time.

I’ll start with the basic uses of substitution and go into the more meaty commands as we go along.

Substitution

Often times you will need to “Search and Replace” once or even multiple times and in multiple locations when editing a file. vim has a really nice built-in feature which allows us to use this kind of substitution.

These commands will be run in COMMAND mode (ie., by typing : when in NORMAL mode) which will then convert the mode from NORMAL into COMMAND mode.

Substitute in the current line only

Find each occurrence of foo in the current line only and replace it with bar. While in command mode type:

foo
:s/foo/bar/g

# which will change foo to bar on that line only 
bar

Substitute in the entire file

Find all occurrences of foo and replace with bar. While in command mode type:

foo
foo
foofoo
FOO
Foo
:%s/foo/bar/g

# Which changes every foo in the file to no matter the font case:
bar
bar
barbar
bar
bar

You will notice that foo was replaced by bar in every sense of the case. FOO, Foo, and foofoo where using different case and foofoo isn’t technically the same word, but substitute doesn’t care.

Take caution when using this since it could change more than you want in a the file you are editing.

Substitute certain lines only

Find all occurrences of foo from lines 1 - 20 and only replace those with bar. While in COMMAND mode type:

:1,20s/foo/bar/g

Comment-out certain lines

This one is good for when you are needing to comment out a number of lines without having to exit NORMAL mode and enter into INSERT mode for each new line just to place in a # symbol. This one will add a # symbol at the beginning of the line from lines 1 - 20. While in COMMAND type:

:1,20s/^/#/g

Remove all blank lines and or comments

Often in .conf files and script files the default config file can have a ton of comments and blank lines, which is often good when you are needing to know details of how to tweak the file, but when you just need to get to the thing you are needing to edit without have all of the extra info you can do so with this command:

g/^#/d
g/^$/d

Now the ^# combo stands for all lines beginning with the # symbol whereas the ^$ is a little different and represents all blank lines. The d at the end means delete whereas the g means globally or the entire file.

Substitute ranges

Ranges are a little more advanced in that they can do things starting from where the cursor is currently to the beginning or either to the end of the file.

  • <.> - The current line
  • <$> - The last line
  • <%> - All lines

This command will delete the current line to the end of the file.

:.,$d

This works really well but I will often just hit d then 100 then d in NORMAL mode, which will delete 100 lines from the current line where 100 is usually enough to get rid everything I need. Sometimes you might have to run enter in 1000.

This next command will delete from the current line to the beginning of the file.

:.,1d

This command will delete from line 10 to the end of the file.

:10,$d

Similarly, if you head to line 10 you can type in d then g then g which will do the same thing.