Vim Tips Wiki
Register
Advertisement
Tip 351 Printable Monobook Previous Next

created 2002 · complexity intermediate · author Karthick Gururaj · version 6.0


I'm a software developer and I find Vim's quickfix very helpful. You can also use this while debugging your code, in a slightly different way.

Usually, you will have some print messages in your code, and after the program runs, you'll look at the output to see the execution trace (e.g which if-constructs were taken, how many times did a while loop iterate.. ). If you precede these statements with a <filename>:<linenumber>:, then, the program output can be parsed with a :cfile, and the execution trace becomes very simple.

For instance, in C

// fdebug is a pointer to the debug file called, debug.txt say.
#define DEBUG_MESG() fprintf(fdebug, "%0s:%0d:\n", __FILE__, __LINE__)
...
function( )
{
  ...
  if (something)
    DEBUG_MESG( );
  else
    DEBUG_MESG( );
  ...
}

Open your code in vim and do

:cfile debug.txt

References[]

Comments[]

In your vimrc file add:

ia prtf fprintf(fdebug, "%0s:%0d:\n", __FILE__, __LINE__);

to make this macro part of vim aliases in "insert" mode.


One disadvantage of having fprints scattered all over the code is, after debugging, you got to remove them. If you have defined a macro instead, you can easily (re)define it to null..
By the way, if you want to debug shell scripts/perl/tcl/whatever, consider getting yourself a generic preprocessor. I found filepp (http://www.cabaret.demon.co.uk/filepp/) pretty good.


The advantage of the alias is that you can modify the expanded alias to suite your need at point of insertion.

ie.

fprintf(fdebug, "%0s:%0d:\n >>> the variable i am interested = %d<<<", __FILE__, __LINE__, variable_in_question) ;

I work in embedded system and a lot of time our only debugging tool for code is printf. Not all of us have in circuit emulators available to us.

And like you said, this is debugging. The code does not go live with debugging statements, they are removed.


Advertisement