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.