Vim/Vi tricks
Here is a collection of things that I found useful and/or surprising.
blockmode
If you ever need to convince somebody that vim is a powerful editor
then show them the block mode.
crtl-v and then move the cursor
You can select rectangular areas and then change them (c), copy (y) them, paste (p) them, delete (d) them ...
%:v/regexp/CMD or %:g/regexp/CMD
You can apply a command (like delete) to NON matching lines (:v/regexp/) or matching lines (:g/regexp/).
This deletes e.g all lines containing the sting "string":
%:g/string/d
Instead of % for the whole file you can of course use a range or a selection of lines marked with shift-v and cursor movement.
How can we use this to give more complex commands such as Ahello (append "hello" to the end of the line)? The "A" would bring us into insert mode and how would we get out at the next matching line. The trick is to specify the edit mode. The command norm specifies to use normal mode. The following will append the string "end of the line" to each line matching the sting "string":
%:g/string/norm Aend of the line
© Guido Socher,
Please enable Javascript to see my email address
License: CC BY 4.0, http://creativecommons.org/licenses/by/4.0/