« Back to Index

[Vim substitution examples]

View original Gist on GitHub

Tags: #vim #substitution #replace #global #viml #forloop #vimscript

Vim substitution examples.viml

" finds any line with `example: ...` and appends `tracker: ''` underneath it
%s/example:.*\n/\0    tracker: ''\r/g

" for each line that has content, get the line number and if an even line number, then do a substitution
:g/./ if getcurpos()[1] % 2 == 0 | s/foo/bar/g | endif

" alternative approach to above where substitution pattern can be empty as it's part of the global pattern
:g/foo/ if getcurpos()[1] % 2 == 0 | s//bar/g | endif

" yet another way using a 'for' loop
for i in range(2, line('$'),2)| :exe i.'s/foo/bar/g'|endfor