sponsor Vim development Vim logo Vim Book Ad

basic Tip #512: Automatic insertion of C/C++ header gates

 tip karma   Rating 2/7, Viewed by 753 

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

created:   July 21, 2003 14:10      complexity:   basic
author:   Morten Fjord-Larsen      as of Vim:   5.7

C/C++ header files should be guarded against multiple inclusions using preprocessor directives, e.g.:

#ifndef FOO_H
#define FOO_H

/* Declarations. */

#endif

Placing the following snippet in your .vimrc file makes vim insert these preprocessor gates automatically:

function! s:insert_gates()
let gatename = substitute(toupper(expand("%:t")), "\\.", "_", "g")
execute "normal i#ifndef " . gatename
execute "normal o#define " . gatename . "   "
execute "normal Go#endif /* " . gatename . " */"
normal kk
endfunction

autocmd  BufNewFile *.{h,hpp} call <SID>insert_gates()

 rate this tip  Life Changing Helpful Unfulfilling 

<< key mapping for goto the absolute line number | Automatic insertion of C/C++ header gates >>

Additional Notes

anonymous, August 8, 2003 10:54
These commands could be shortened a bit with use of  :put ='text'
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 vim@vim.org after searching the archive. Help Bram help Uganda.
   
Sponsored by Web Concept Group Inc. SourceForge.net Logo