Practical VIM
No Commercial Software Will Ever Come Close
VIM is not intuitive at first, but once you take the time to figure it out you'll never put it down. If you can only learn one UNIX tool, this should be it.
This is a collection of VIM tips that I've found useful in every day programming and administration. Over time I'll update and add to these notes.
Special Characters
Non-visible characters can be inserted with Ctrl+v followed by another control sequence or key.
Ctrl+v Tab Ctrl+v Enter
Database Access
Any database that supports command line input or command line options can be used. PostgreSQL output can be copied from source code line this:
:w !psql -c "SELECT id,status,summary_rows FROM job_status;"
The previous example assumes that the current user is also the name of the database, otherwise you must specify a database name and password:
:w !psql -d workbase workflow \ -c "SELECT id,status,summary_rows FROM job_status;"
Recording
q is unused in vi, perhaps that's why it was selected to start and stop macro recording. The following records a macro into register e that reduces consecutive spaces:
qe :%s/ / /g :%s/ / /g q
Play register e by typing:
@e
Printing
Any method of UNIX printing can be used through the :w command. I'm using CUPS in these examples.
Print the current visual selection:
:'<,'>w !lp
This prints the current file to a HP LaserJet 3300 at work:
:w !lp -d HP3300
This prints from the current line to the end of the file:
:.,$w !lp
Quick Operations
VIM has a clever set of key combinations for picking out content to apply an operation to.
dw
Delete word (defined by non-alphanumeric characters)
dW
Delete word (defined by white-space)
yis
Yank inner sencence
vip
Select inner paragraph
d$
Delete to end of line
v3w
Delete the next three words
gq2j
Reformat next two lines using textwidth
gqip
Reformat paragraph using textwidth
f7
Move the cursor to the first character 7 on the current
line
Line Breaks
If you're editing a file that was created on a Mac you'll see ^M marking the end of each line. You can set VIM to save the file using UNIX line breaks by typing :set ff=unix.
LF UNIX CR Mac CRLF Windows
Mass Changes
The following removes any single space that appears at the end of each line in the current file:
:%s/ $/$/g
I came up with this combo to delete all of the unused lines in a vCard file:
:g/:$/d
While cleaning up a vCard file before processing I also initiated these commands:
:g/VERSION:2.1/d :g/^N:/d :%s/;PREF//g :%s/;;/;/g
VIM functions can be executed through a shell script by using the -c parameter. This one changes <body> tag in the file manual.html.
% vim -c ':%s/<BODY bgcolor="#A0A0A0">/<BODY>/g' -c ':wq' manual.html
Replacing a whole collection of files might look like this:
% find . -name "*.html" -exec vim -c \
':%s/<BODY bgcolor="#A0A0A0">/<BODY>' -c ':wq' {} \;
Spell Checking
VIM sets itself apart from other editors that allow the user to run commands by enabling the user to incorporate the content with the input and output of a command. Here's how you might use aspell to spell check your document:
:! aspell -c %
Other Useful Keys
J
Joins the next line to the current line. It simply removes the
\n or \r\n codes from the end of the current
line.
Set Options
VIM has a lot of useful options that can be set in .vimrc or using the :set command. Type :set all to list all of the current set options:
With few exceptions every set option can be turned off by preceeding the variable with no. Here are a few useful ones:
paste
Do not allow auto-indent to take over when entering text. Very useful
for copying between terminal windows.
tw=xx
Automatically wrap text at xx characters. Use 0 as
the value to turn this feature off.
expandtab
Replace tabs with spaces defined by the tabstop=
parameter.
By default most editors set the tab stop at 8 characters, which I find quite offensive. Put the following entries in ~/.vimrc to set the tabs to 4 spaces (according to PEP 8), and to use a dot to mark tabs and trailing spaces:
set tabstop=4 set listchars=tab:·\ ,trail:· set list
Change a Group of Files
The key is to use find to locate the files and use vim's -c parameter to specify an action. The following find every occurance of "dogs" and replaces it with "cats" in files with .asp extentions.
find . -name "*.asp" -exec vim -c ":%s/dogs/cats/g" -c ":wq" {} \;
-c must be used for every command, including wq to write the file and quit.
References
Vim HowTo - Printing Text by Sven Guckes
My life with text editors by Mark Stosberg
Vim Cookbook by Steve Oualline