Showing posts with label quickfix. Show all posts
Showing posts with label quickfix. Show all posts

Monday, April 7, 2008

Quick Uncomment

This tip was contributed by my friend (and expert Vim user), Christopher Suter. BTW, if anybody else has a tip they would like to contribute, just send me an email via tinymountain at gmail dot com.

To uncomment a /* */ - style block comment, put the cursor on the beginning or ending tag - whichever is closest - and in normal mode, do %dd``dd

this sequence jumps to the matching 'brace' (/* or */) deletes that one, then jumps to the previous jump point, which is of course the first 'brace.' you can replace dd with xx if the braces are not on their own lines for example:

/* this.commentedInvocation(); */

A really handy idiom that is great for large block-commented code.

Thanks Chris!

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.