Tip 514 Printable Monobook Previous Next

created July 21, 2003 · complexity basic · author Morten Fjord-Larsen · version 5.7

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

#ifndef FOO_H
#define FOO_H

/* Declarations. */


Placing the following snippet in your vimrc file, makes Vim insert these preprocessor gates automatically, when a new header file is created:

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
autocmd BufNewFile *.{h,hpp} call <SID>insert_gates()

Comments[edit | edit source]

Here's my variation.

Pull up foo.c or foo.h, hit your key sequence to trigger New_Class(), and bang, there's the class structure for you.

function New_Class_C(l_class_name, u_class_name)
#include "l_class_name.h"



function New_Class_H(l_class_name, u_class_name)
#if !defined(INCLUDED_u_class_name_H)
#define INCLUDED_u_class_name_H

class u_class_name {


class u_class_name;


function New_Class()
   let class_name = expand("%:r")
   let file_type = expand("%:e")
   let l_class_name = tolower(class_name)
   let u_class_name = toupper(class_name)

   if file_type =~# "c"
      call New_Class_C(l_class_name, u_class_name)
      call New_Class_H(l_class_name, u_class_name)

The following is from a proposed new tip that has now been deleted. What is required to make this work? It assumes you start with something?

Here is an alternative, intended to produce:

#ifndef __FILENAME_H
#define __FILENAME_H
#endif //__FILENAME_H

This will add this kind of block at the top of a new .h file at the press of a key. Add to your vimrc file:

nnoremap <F12> "%phr_I#ifndef __<Esc>gUwyypldwidefine <Esc>yypldwiendif //<Esc>O<Esc>

This is another alternative, also a mapping, intended to produce more verbosely-commented boilerplate but otherwise similar to the above:

:nnoremap <C-F12> "%phr_g0gUw<Esc>I#ifndef __<Esc>g$yiwo<Esc>pI#  define <Esc>gg<Esc>A /* Guard against multiple
 header inclusion error */<Esc>ggjo<Esc>I#endif /* end if-not-def <Esc>pA */<Esc>2O<Esc>

Produces when run on "Phoohaa.h" (must be in that file's working directory):

#ifndef __PHOOHAA_H /* Guard against multiple header instance error */
#  define __PHOOHAA_H

#endif /* end if-not-def __PHOOHAA_H */

Please note however that using double underscores anywhere in your include guard names is not recommended, and symbols starting with a double underscore including defines are reserved for use by the compiler. So in the above example it is best to use:

#ifndef FILENAME_H
#define FILENAME_H

For more information, see: http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier

Simplistic[edit | edit source]

function g:MyAddGuard(s)<br>
    call append(0, ["#ifndef " . a:s, "#define " . a:s, ""])
    call append(line("$"), ["#endif /*" . a:s . "*/"])

To add a guard to the current file, just do:

:call MyAddGuard("MACRONAME")

Choosing an appropriate name for the function will allow you to easily tab-complete this line.

See also https://github.com/drmikehenry/vim-headerguard/blob/master/plugin/headerguard.vim.

Community content is available under CC-BY-SA unless otherwise noted.