
David Rayner has been writing this list of VI/VIM features for the past 21 years, and it is still updated frequently today. The
list covers the obvious things like macros, simple movement and so forth, however it also goes into detail of
the these and many other features that you think are only simple but instead they have many different useful and at times complex
functions.
For example this what there is simply for substitution.
----------------------------------------
#substitution
:%s/fred/joe/igc : general substitute command
:%s//joe/igc : Substitute what you last searched for *N*
:%s/~/sue/igc : Substitute your last replacement string for *N*
:%s/\r//g : Delete DOS returns ^M
" Is your Text File jumbled onto one line? use following
:%s/\r/\r/g : Turn DOS returns ^M into real returns
:%s= *$== : delete end of line blanks
:%s= \+$== : Same thing
:%s#\s*\r\?$## : Clean both trailing spaces AND DOS returns
:%s#\s*\r*$## : same thing
" deleting empty lines
:%s/^\n\{3}// : delete blocks of 3 empty lines
:%s/^\n\+/\r/ : compressing empty lines
:%s#< [^>]\+>##g : delete html tags, leave text (non-greedy)
:%s#< \_.\{-1,}>##g : delete html tags possibly multi-line (non-greedy)
” VIM Power Substitute
:’a,’bg/fred/s/dick/joe/igc : VERY USEFUL
” duplicating columns
:%s= [^ ]\+$=&&= : duplicate end column
:%s= \f\+$=&&= : same thing
:%s= \S\+$=&& : usually the same
” memory
%s#.*\(tbl_\w\+\).*#\1# : produce a list of all strings tbl_* *N*
:s/\(.*\):\(.*\)/\2 : \1/ : reverse fields separated by :
:%s/^\(.*\)\n\1$/\1/ : delete duplicate lines
” non-greedy matching \{-}
:%s/^.\{-}pdf/new.pdf/ : delete to 1st pdf only
” use of optional atom \?
:%s#\< [zy]\?tbl_[a-z_]\+\>#\Lgc : lowercase with optional leading characters
” over possibly many lines
:%s/// : delete possibly multi-line comments
:help /\{-} : help non-greedy
” substitute using a register
:s/fred/a/g : sub “fred” with contents of register “a”
:s/fred/ asome_text s/g
:s/fred/\=@a/g : better alternative as register not displayed
” multiple commands on one line
:%s/\f\+\.gif\>/\r&\r/g | v/\.gif$/d | %s/gif/jpg/
:%s/a/but/gie|:update|:next : then use @: to repeat
” ORing
:%s/suck\|buck/loopy/gc : ORing (must break pipe)
:%s/\v(.*\n){5}/&\r : insert a blank line every 5 lines *N*
” Calling a VIM function
:s/__date__/\=strftime(”%c”)/ : insert datestring
” Working with Columns sub any str1 in col3
:%s:\(\(\w\+\s\+\)\{2}\)str1:\1str2:
” Swapping first & last column (4 columns)
:%s:\(\w\+\)\(.*\s\+\)\(\w\+\)$:\3\2\1:
” filter all form elements into paste register
:redir @*|sil exec ‘g#< \(input\|select\|textarea\|/\=form\)\>#p’|redir END
:nmap ,z :redir @*sil exec ‘g@< \(input\select\ textarea\ /\=form\)\>@p’ redir END
” substitute string in column 30 *N*
:%s/^\(.\{30\}\)xx/\1yy/
” decrement numbers by 3
:%s/\d\+/\=(submatch(0)-3)/
” increment numbers by 6 on certain lines only
:g/loc\|function/s/\d/\=submatch(0)+6/
” better
:%s#txtdev\zs\d#\=submatch(0)+1#g
:h /\zs
” increment only numbers gg\d\d by 6 (another way)
:%s/\(gg\)\@< =\d\+/\=submatch(0)+6/
:h zero-width
" rename a string with an incrementing number
:let i=10 | 'a,'bg/Abc/s/yy/\=i/ |let i=i+1 # convert yy to 10,11,12 etc
" as above but more precise
:let i=10 | 'a,'bg/Abc/s/xx\zsyy\ze/\=i/ |let i=i+1 # convert xxyy to xx11,xx12,xx13
" find replacement text, put in memory, then use \zs to simplify substitute
:%s/"\([^.]\+\).*\zsxx/\1/
" Pull word under cursor into LHS of a substitute
:nmap z :%s#\< =expand(”“)\>#
” Pull Visually Highlighted text into LHS of a substitute
:vmap z :%s/\< *\>/
” substitute singular or plural
:’a,’bs/bucket\(s\)*/bowl\1/gic *N*
—————————————-
Again here’s the list, and have fun VIMing!
Also here’s a link to the regular VIM site, and the Cocoa port; MacVim.