Wednesday, December 19, 2007

Indent

This is useful for a quick and dirty indent. It works on a variety of file formats, but I typically use it on HTML or XML.

1) open a file
2) issue a :filetype command to see if indenting is ON
3) if it's not, turn it on by doing a :filetype indent on
4) filter the entire file through the program specified by "equalprg" (default is none)
by doing gg=G (no ex mode here).
5) you can set equalprg accordingly if you want to use a 3rd party program:

:setlocal equalprg=tidy\ -quiet\ -m\ -utf8\ %

or if you want to use make rather than equalprg

:setlocal makeprg=tidy\ -quiet\ -errors\ %

then just issue a :make.

Thursday, December 6, 2007

Hey Man

Want to see the man page for whatever command is under the cursor? Hit K in normal mode, and Vim will open the man page the same as your favorite shell.

Value Under Cursor

Sometimes I run into funky characters in files I'm editing. Sometimes I want to know what they are. Vim makes this easy. Put your cursor on the character you want to examine and type ga in normal mode. You can also use the ex command :ascii if you prefer.

Wednesday, December 5, 2007

Quickfix

Vim has a quickfix feature that allows you to create a list of items to be resolved and easily navigate that list. One easy way to add items to the quickfix list is by using Vim's internal grep feature like so:

:vimgrep /some pattern/ somefile

Once the command is run, the quickfix list is populated with lines matching the pattern in the specified file. From there, you can open the quickfix buffer by using the :cope ex command. Navigating to a line in the quickfix buffer and pressing enter will take you to the specified line in the source file you are editing.

A more advanced use of quickfix is as an assistant in debugging errors across multiple files. A common debugging practice is to insert print statements inside of various conditionals in your source code. If you follow the format "filename:line number:error message", Vim can understand the debugging output of your program and load it into the quickfix buffer. In Ruby (a language I program in often), you would do something like the following:

puts("#{__FILE__}:#{__LINE__}:some error happened")

Place similar statements amongst all files you are interested in tracing, have your program write to a file called "debug.txt", and then open Vim. Issue the following command :cfile debug.txt, and Vim will open the file pertaining to the first error reported at the appropriate line. Again, you can issue a :cope command to navigate the quickfix list and trace through all the errors reported.

Friday, November 30, 2007

Shifting

It's often handy to shift blocks of text left and right, and Vim makes it easy. I like to set shiftwidth=1 in my vimrc file so I can have nice fine-grained control over my shifts. First you should define the following snippit (stolen from Vim Tips) in your vimrc:

vnoremap < <gv
vnoremap > >gv

Now highlight a column of text using blockwise visual selection (ctrl-v on Unix platforms, ctrl-q on Windows) and then hit > or <

>>>> (would shift four shiftwidths to the right)

You can also use traditional ex mode to accomplish the same. Go to the first column of a given block and type ma (in normal mode) to set mark a. Go the the end of the block you want to indent, first column, and set mark b by typing mb (in normal mode). Now issue the following ex-command.

:'a, 'b>> (shift two columns to the right)

Search Motions

You can use a search as a motion in conjunction with an operator. Say you have the following sentence:

A quick brown fox jumps over the lazy dog.

Vim (and Vi for that matter...) allow you to do things such as:

y/over (yanks "a quick brown fox jumps" into the default register)

c/over (puts you into insert mode with the sentence "over the lazy dog")

d/lazy (leaves you with the sentence "lazy dog")

etc...

Fast Select All

Want to select an entire file in visual mode very quickly? Try ggVG in normal mode.

Text Objects

Text objects are commands that can be used in combination with an operator or when in visual mode. Basically, you give a command followed by a description and Vim applies it to the relevant text object. Available commands include things like:

aw - a word
iw - inner word
aW - a word (leading and trailing whitespace included)
as - a sentence
etc...

In practice, these allow quick manipulation on the applied block of text. For example, you're in normal mode, and your cursor is positioned in the middle of a word. You type caw and Vim executes "change a word" deleting the existing word and switching to insert mode for you to perform your edit. For the full details on this feature see :help text-objects.

Tuesday, November 20, 2007

Filtering

Sometimes it's helpful to filter your current editing session through an external command. Vim makes this easy to accomplish with the following ex command, :%!command. To filter a file containing strictly numbers through the Unix sort command using numerical comparison, you would :%!sort -u

Friday, November 16, 2007

Mouse Support

I usually try to avoid the mouse for reasons of efficiency; however, there are times when the mouse can actually save you time. A good example of which is resizing windows. Vim 7.x has an option to respect the mouse even when you're not using Gvim. To enable mouse support type :set mouse=a.

Ex Commands

Vim supports tons of ex commands, which can be very useful for efficient editing. You can get a master-list of all the ex commands Vim supports by typing :help holy-grail.

GOTO File Under Cursor (not harmful)

If you're editing a file and have your cursor positioned on a file you would like to open with Vim, press `gf' from normal mode, and it will open in the same window. Ctrl-w f will open it in a new window, and Ctrl-w gf will open it in a new tab.

Color Schemes

I'm pretty sure this is a 7.x feature as well. Vim now supports color schemes. These are stored in the `colors' folder inside of your Vim install dir. Have a look in there, pick a scheme, and type in a command such as :colorscheme blue. Colors will vary with language syntax, but should stay relatively consistent. Set your favorite color scheme in your vimrc, or make your own scheme.

This site has lots more color schemes:

http://www.cs.cmu.edu/~maverick/VimColorSchemeTest/

Time Travel

Who says OS X has a monopoly on time machines? Vim can time-travel as well. Ever since the 7.x branch, you can type :earlier 4h to go back in time four hours on the file you're editing then :later 4h to go four hours forward again. See :help earlier for all the options. Note, how far back you can go is limited by the size of you `undolevels' setting.

Command Line History

Want to see the ex commands you've executed as of late? Easy, just type q: from normal mode to open your command-line history. From the command-line history window, you can yank commands or execute them as needed. To go back to normal mode type :q. You can also enter command-line history mode from ex mode by typing ctrl-f.

Diagraphs

Sometimes during a text-editing session, you may need to substitute binary characters like the infamous ^M out of your file. Or alternatively, you may just want to insert an internationalized character like ΓΌ. Vim makes this easy to do. During an editing session, typing :digraph will give you a list of available special characters. Pick the desired character from the list, go into insert mode, and press ctrl-k followed by the character code listed in the digraph table. To insert the umlauted u mentioned above I did ctrl-k u:. Note that this works when typing in ex commands too such as substitutions. A final bonus is you type a macro in insert mode, insert control characters as needed, such as ctrl-w k to jump up a window, yank the macro into a register, and execute as needed.

Paste Text from OS Clipboard

The last tip dealt with copying text to your OS clipboard. Another useful feature is being able to paste from your OS clipboard to your current Vim session. All you need to do this is a simple "+gP. Happy vim'ing!

Copy Text to OS Clipboard

This is a nice feature if you are editing using vertically split windows and want to copy the contents of a single window to your OS clipboard. In regular Vim or Gvim, go into visual mode, and highlight the text you would like to copy to your clipboard. Then, yank the text as follows, "+y. It should now be available to any application that uses your standard clipboard.

Macro Registers

Most Vim users know about macros and find them useful; however, until recently, I didn't realize they are actually stored in the same registers we all know and love. For example, you create a new macro by pressing `qa'. You type in a series of commands that you would like to repeat, end the macro, done. But wait, you made a mistake. Well, you can easily edit the macro stored in `qa'. I typically issue a :new to open a new window, move my cursor to that window and then "ap. The contents of the macro are printed to the window. From there, you can edit the macro as needed and then yank the macro back in to the desired register by executing something like 0"ay$. Once yanked, the new macro will execute as you would expect it would.

Friday, November 9, 2007

Hello World

Hello, my name is Travis Whitton. I'm a programmer by trade, and I've been a user of the Vim text-editor for the last nine years. Although, I still don't consider myself an expert, I do enjoy digging into the vast array of features Vim provides, and the intention of this blog basically to keep track of what I've been learning. I plan on posting new information regularly, and my goal is to try and post at least one tip every workday. I welcome any comments or suggestions and especially appreciate any sharing of information.

The first tip I'm going to share is a very basic one, but I find it to be a nice time-saver. Anytime I am typing, I find it a waste to move my fingers off the home keys. This means that taking my left hand away from the middle-row of the keyboard to press the Esc key is something I want to avoid. I recently learned that ctrl-[ works as a handy substitute for Esc. Try it, you might like it.