Thursday, November 19, 2009

Shift-Tab'ing

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

:1,3bdelete

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.

http://www.vim.org/scripts/script.php?script_id=1071