Software >> OS >> Unix >> Utilities >> vi >>  Examples of search and replace with regular expressions [plaintext]

To search for lines ending with "-N" where N is a digit and replace with -0N i.e. prefix with 0 :%s/-\(.\)$/-0/g To search for lines that start with == and insert a newline followed by ``` followed by original text i.e. == /^== :%s//\r```\r&/gc /^==*$ :%s//\r```\r```/gc

Examples of ranges

Range Description Example
21 line 21 :21s/old/new/g
1 first line :1s/old/new/g
$ last line :$s/old/new/g
. current line :.w single.txt
% all lines (same as 1,$) :%s/old/new/g
21,25 lines 21 to 25 inclusive :21,25s/old/new/g
21,$ lines 21 to end :21,$s/old/new/g
.,$ current line to end :.,$s/old/new/g
.+1,$ line after current line to end :.+1,$s/old/new/g
.,.+5 six lines (current to current+5 inclusive) :.,.+5s/old/new/g
.,.5 same (.5 is interpreted as .+5) :.,.5s/old/new/g