Thursday, February 28, 2008

More Marks

Most people know the basics of using marks in Vim; however, there are some special mark locations you may not know about.

`. - move cursor to the line and column of your last edit
'. - move cursor tot the line of your last edit
`' - move to last jump position
`" - move to location before file file was last closed

:marks - show entire mark list
:mark x - show mark stored in register x
:jumps - show the entire jump list

ctrl-o - move cursor to older position in jump list
ctrl-i - move cursor to newer position in jump list

In case you don't know basic marks, in normal mode, press m followed by a-z to set a given mark to your current position in the file. To jump back to that position type `a, `b, etc...

A jump is executed whenever you move the cursor to an arbitrary position in the file, i.e., 123G (line 123).

Wednesday, February 27, 2008


Say you're editing in Vim and wrapping it up for the day. You have everything configured to your liking and don't want to go through the hassle of setting everything up tomorrow. Vim allows you to very easily store your current editing session like so:

:mksession work.vim

After that you can quit Vim and come back whenever you want. Once you're back inside Vim, you can type:

:source work.vim

and your last editing session is reloaded. You can also load a session from the command-line like so:

vim -S work.vim

Friday, February 15, 2008

Home Middle Last

Vim provides a lot of ways to get around the screen to where you want to go. Sometimes you may want to quickly put your cursor at the top, middle, or bottom of the screen. In normal mode, just do the following:

H = Home (top of screen)
M = Middle (middle of screen)
L = Last (bottom of screen)

Thursday, February 14, 2008

Drop Cache

Ok, this isn't a Vim tip, but it does apply to development that a lot of people do with Vim. I do a lot of SQL work here at Grooveshark and constantly wrestle with trying to get an accurate picture of how efficient a given query is. Linux and MySQL both cache very aggressively, which makes benchmarking a pain. MySQL provides the SQL_NO_CACHE query option to avoid it's query cache, which is convenient; however, Linux still caches the disk reads; thereby, skewing any performance measurements. In the past, I've been forced to do ridiculous things like cat a file larger than available RAM to /dev/null or unmount and remount the filesystem that the database files are housed on. Luckily, Linux kernels 2.6.16 and newer provide a mechanism to clear the inode, page, and dentry caches on demand avoiding all this headache. All you have to do is echo a value to the proc filesystem, and you're done.

To free pagecache, dentries and inodes:

echo 3 > /proc/sys/vm/drop_caches

Note: this must be done as root, and you should issue a sync before doing so.

Wednesday, February 13, 2008

Last File Location

Some Linux distributions set this up by default and some don't. I like Vim to open a file I'm working on to the last location I was in before exiting. This can be accomplished by adding the following to your .vimrc.

au BufReadPost * if line("'\"") > 0 && line("'\"") <= line("$") | exe "normal g'\"" | endif

New Man

I read a lot of manpages, and I like to have power and flexibility when doing so. Luckily, Vim is up to the task:

My primary Desktop is a modern Linux distro, so it uses utf-8 as the locale. I added the following to my bashrc (actually zshrc, but this should work for bash as well).

function man
/usr/bin/man $* | col -bp | iconv -c | view -c 'set ft=man nomod nolist' -

If you're on an older system that doesn't use utf-8 try something like this instead:

function man
/usr/bin/man $* | col -b | view -c 'set ft=man nomod nolist' -

Note that the above functions may need to modified for your shell.

Friday, February 8, 2008


Now for some fun ;-). A friend asked me for a Vim macro this morning to strip all but the text contained with the double quotes of a file with the following format:

<option value="Automotive">Automotive</option>
<option value="Banking">Banking</option>
<option value="Biotech">Biotech</option>


After the macro it would be:


Initially, I came up with the following sequence (macro storage command omitted):

df"f"d$j0 (9 chars)

This works fine, but my OCD kicked in, and I decided to make it shorter:

df"f"D+ (7 chars)

For a brief moment, I thought this was as short as I could make it, but then...

df"wD+ (6 chars)

As far as I know, this is the shortest possible macro to accomplish this task, but I would love to be proven wrong.


My friend Chris Sutter has contributed another solution using text objects that is pretty nifty.

di"Vp (5 chars)

or if you want to jump to next line as with the previous 6 char macro:

di"Vgp (6 chars)

Good stuff... thanks Chris!

Thursday, February 7, 2008

Add and Subtract

Move your cursor over a number in Vim. In normal mode, press ctrl-a to increment it's value by one. Press ctrl-x to decrement it's value. Believe it or not, this actually comes in handy pretty often.

Tuesday, February 5, 2008

Insensitive Search

Most Vim users realize that you can :set ic and :set noic to control case-sensitivity in searches. However, you may not know that you can do a case-insensitive search on-the-fly as well by doing the following /\cmy search pattern.