Some Vim Basics


This blog post is about some useful commands for the text editor Vim.

Show some technical infos about the current file like words, lines, ..

g + <Strg + g>

Search backwards from the current position

?searchterm

Forward search from the current position

/searchterm

Within the search mode, jump to the next match using the key n.

Search and Replace

Search the entire document and replace it with a specified string. The option g at the end of the command allows one to not just replace the first appearance of the search term, but any match.

:%s/<regex pattern to search for>/<replace by>/g

Search only the current line and replace it with a specified string:

:s/<regex pattern to search for>/<replace by>/g

To perform search and replace for multiple rows, select the area using and the arrow keys. Afterwards use the same syntax as for a single line.

Edit multiple lines

The following shortcuts are about appending a character to the beginning or the end of each line:

Append char '!' at the end of every line

:%s/$/!/ 

Append char '!' at the beginning of every line

:%s/^/!/ 

References

Vim Wiki: Search and Replace

Vim Wiki: Insert Text in Multiple Lines