sponsor Vim development Vim logo Vim Book Ad

intermediate Tip #351: Using quickfix in a different way

 tip karma   Rating 42/21, Viewed by 2174 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   October 24, 2002 6:11      complexity:   intermediate
author:   Karthick Gururaj      as of Vim:   6.0

I'm a software developer and I find vim's quickfix (:help 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 the 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 a ":cfile debug.txt"

 rate this tip  Life Changing Helpful Unfulfilling 

<< when 'formatoptions' has o easily enter a non commented line: go/gO mappings | disabling cabbrev >>

Additional Notes

Anonymous, November 6, 2002 7:21
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.
Anonymous, November 6, 2002 7:33
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..
Btw, 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.

No vim stuff.. anyways..
- Karthick
Anonymous, November 6, 2002 16:12
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.


Anonymous, November 7, 2002 1:02
Sigh.
A bug.

You'll have to print as <filename>:<linenumber>: (the second colon after <linenumber> was missing in the original post). Or you'll have to change 'errorformat' I think.. but why would you want to do that?
So we have,

#define DEBUG_MESG( ) fprintf(fdebug, "%0s:%0d:\n", __FILE__, __LINE__)

and

ia prtf fprintf(fdebug, "%0s:%0d:\n", __FILE__, __LINE__) ;
- Karthick.
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to [email protected] after searching the archive. Help Bram help Uganda.
   
Sponsored by Web Concept Group Inc. SourceForge.net Logo