sponsor Vim development Vim logo Vim Book Ad

basic Tip #435: remarks to script c.vim : statement oriented editing of  C / C++ programs

 tip karma   Rating -3/3, Viewed by 1370 

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

created:   March 6, 2003 23:11      complexity:   basic
author:   Thomas      as of Vim:   6.0

Seems to be a nice, powerful addition for C-programmers.

The screenshots show a really dangerous C++ trap:
     NEVER add C++ comments after a '#define' statement !
The Preprocessor will substitute the defined text literally,
so all of your code behind the defined text will be commented out.

I used abbreviations for C-constructs like 'cfor' for empty for loops, 'cif' for if then elses, etc.,
so typing in insert mode has not to be interupted. The cursor will be positioned for entering the following code.

Another thing is standard C-comments for file/function headers. I used a command line tool (perl script)
to get a personalized version from a standard template and read in the output with ':r !<mytool> <options>'.
This is a solution for workgroups where no one HAS to use VIM.
I had autocommands for automatically read in the comment headers when opening new header or source files.


Thomas

 rate this tip  Life Changing Helpful Unfulfilling 

<< Autogroup commands for C/C++ editing - inserting skeletons etc (long post!) | Accidently typed control-u and lost your input? >>

Additional Notes

Anonymous, September 15, 2003 14:30
It would be nice if you shared the definition of your abbreviations and such, rather than just talked about them.  It would be much more helpful.
Anonymous, November 12, 2004 6:02
Sorry,
this abbreviations are tens of years old. I thought everybody who needs it, has done something like that already.
Well I have im my gvimrc:

" some useful abbreviations for C-programming
abb cmain int main (const int argc, const char* argv[])^M{^Mreturn 0;^M} /* endmain */^M^[?{^Mo
abb cfor for (i = 0 ; i < 0; i++)^M{^M} /* endfor */^M^[?for (^Mw
abb cif if ( 1 == 1 )^M{^M} /* endif */^M^[?if (^Mwwh
abb cel if ( 1 == 1 )^M{^M} else^M{^M} /* endif_else */^M^[?if (^Mwwh
abb csw switch (1)^M{^Mcase 1: /* */^M{^M}  break;^M^Mdefault: ;^M} /* endswitch */^M^[?switch (^Mwwcw
abb cwh while ( 1 == 1 )^M{^M} /* endwhile */^M^[?while (^Mwwh
abb cdo do^M{^M} while ( 1 == 1 ); /* enddo_while */^M^[?do$^Mjo

abb dotlin Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…^M
abb cdotlin /*Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…Â…*/^M
abb fdotlin •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••^M
abb cfdotlin /*•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••*/^M
abb dlin =======================================================================^M
abb cdlin /*===================================================================*/^M
abb lin -----------------------------------------------------------------------^M
abb clin /*-------------------------------------------------------------------*/^M
abb ulin _______________________________________________________________________^M
abb culin /*___________________________________________________________________*/^M
abb Ulin ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯^M
abb cUlin /*¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯*/^M

Substitute this <caret><M> here with hitting <ctrl-v><ctrl-m>,
and <caret>[ with <ctrl-v><esc> in insert mode.

Thomas
Anonymous, November 22, 2004 3:59
>The screenshots show a really dangerous C++ trap:
>     NEVER add C++ comments after a '#define' statement !
>The Preprocessor will substitute the defined text literally,
>so all of your code behind the defined text will be commented out.

What compiler are you using ?!?

Any *standard* C/C++ compiler will tokenize *before* processing any #define statement.
Tokenizing includes replacing each comment with a space character.

If preprocessing worked the way you described, the following should work:
#define BEGIN_NOTE /*
#define END_NOTE */

BEGIN_NOTE
   Testing
END_NOTE

Anyone know of a *standard* C/C++ compiler where that works?
Anonymous, December 1, 2004 23:40
Well, this behaviour is not a standard, but was not well defined (or implemented in all existing compilers).
I had compilers (probably for embedded systems, I don't remember exactly) with this behaviour.
Thomas
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 the maillist. Help Bram help Uganda.
   
Sponsored by Web Concept Group Inc. SourceForge.net Logo