Monday, November 30, 2009

Text States

If you're using Vim 7+, you can use g- and g+ to go between text states. For a more advanced usage see the :help earlier and :help later.

Tuesday, November 24, 2009

Jumping to a Buffer

If you have split windows open with multiple buffers, you can jump directly to a specific buffer number by doing the following:


N is the window number you want to move the cursor to.

Thanks to Duff for the coffee!

Monday, November 23, 2009

Indent From Insert

In the comments of my last post, graywh left a great tip that I didn't know about. If you're working in insert mode, you can change the indent level of the current line using <C-t> and <C-d>. These commands work no matter where your cursor is positioned on the current line and adjust the indent level based on your shiftwidth setting.

Thursday, November 19, 2009


I have the following in my vimrc:

set sts=4
set et

This allows me to take advantage of the convenience of the tab key when editing while actually inserting spaces into the current buffer. Intuitively I've always wished I could shift-tab to unindent the current line one tab stop. After a minute of fiddling with my vimrc, I found a solution.

imap <S-Tab> <C-o><<

I should also mention that << and >> shifts the provided text based on your shiftwidth setting.

Tuesday, November 17, 2009

The Tao of Programming

I stumbled onto this quip recently and found it particularly poignant.

"A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James"

Monday, November 16, 2009

Last Insert

Hitting gi in normal mode will take you to the last location where you were in insert mode. This is great for bouncing back to where you were editing after browsing the current buffer from normal mode.

Friday, November 13, 2009

Last Directory Visited

From the shell, typing "cd -" will take you to the last directory visited. This is useful from a command-line navigation perspective and from inside of shell scripts as well.

Thursday, November 12, 2009

Last Command

Hitting "fc" from a Bash shell invokes your default editor (hopefully Vim) on your last shell command and executes it after Vim exits. This is great for command-line history editing after the fact.

-e ENAME selects which editor to use. Default is FCEDIT, then EDITOR, then vi.

Wednesday, November 11, 2009

Window Scrolling

Hit ctrl-e to scroll the current window down. Hit ctrl-y to scroll the current window up. An advantage of using these commands is that the cursor stays in the current location. Try it!

Reader Question: Tags?

Daniel Näslund writes:

"I use tags a lot. But I'm annoyed by all those open files it leaves
behind. It would be great if the file opened with g] or Ctrl-] would be
closed with Ctrl-T. If the file had been previously opened it would
remain open.

Another thing is a way to close all files except those currently viewed."

I don't use tags too often, so perhaps another reader can answer that question, but if you want to make all buffers except for the current buffer hidden, you can type in the :on ex-command. If you want to hide modified buffers as well, you can do :on!.

If the desired behavior is to actually delete the buffer, and you know it's buffer # or filename, you can do the following:

:bdelete file1 file2 file3

or something like


Lastly, if you want to use a script to accomplish this, there's the BufOnly.vim which provides a single command to unload all buffers except the current one.

Reader Question: MMM Mode?

Chris Keating writes:

"Could you ask your readership if there's a vim equivalent for emacs MMM mode:

mmm mode basically allows you to have different syntax highlighting for different parts of your document. This is really handy when you have SQL embedded in strings, etc."

Anybody have any ideas?

Tuesday, November 10, 2009

Setting Options

Typing :options will allow you to set options interactively from inside of Vim. Just choose a command set to edit, and edit from there. This is a nice way to discover new settings.

Monday, November 9, 2009

Undo Levels

You can set the number of desired undo levels by issuing a :set undolevels=N ex command.

Friday, November 6, 2009


If you're tired of Vim asking you if you want to save the current buffer, you can :set autowrite to avoid this. Be careful if you enable this settings as it relies on your own prudence to prevent accidents from happening.

Thursday, November 5, 2009

Next and Previous Characters

Hitting "^" will take you to the first non-blank character on the current line. Hitting the Enter key will take you to the first non-blank character on the next line. Hitting "-" will take you to the first non-blank character on the previous line. All these should be executed from normal mode.

Wednesday, November 4, 2009

Beginning and End of a Buffer

Hitting "gg" in normal mode will take you to the first line of the current buffer. Hitting "G" will take you to the end of the current buffer.

Tuesday, November 3, 2009

Grab A Ruler

If you :set ruler in your vimrc, you'll retain a constant ruler at the bottom of your editing window. It contains the same statistics as provided by ctrl-g and allows valuable information to be gleaned with a quick glance.

Monday, November 2, 2009

Knowing Where You Are

If you want to know the current filename, what percentage of the current file you're editing, whether the current file has been modified, or the character or line position, all you have to do is hit ctrl-g from normal mode inside Vim.