Skip to content

Instantly share code, notes, and snippets.

@louisrli
Last active September 9, 2020 01:58
Show Gist options
  • Save louisrli/9084918 to your computer and use it in GitHub Desktop.
Save louisrli/9084918 to your computer and use it in GitHub Desktop.
vim -- automatic comment, header guard, and header inclusion
" Automatic C / C++ file headers
" (relies on automatic creation of new stars on 'o' )
function! s:insert_header()
let filename = expand("%:t")
execute "normal! i/* "
execute "normal! o" . filename
execute "normal! o"
execute "normal! oTODO: Description "
execute "normal! o"
execute "normal! o/"
normal! o
normal! o
endfunction
autocmd BufNewFile *.{h,c,hpp,cpp} call <SID>insert_header()
" Automatic C / C++ header guards
function! s:insert_gates()
let gatename = "__" . substitute(toupper(expand("%:t")), "\\.", "_", "g")
execute "normal! i#ifndef " . gatename
execute "normal! o#define " . gatename . " "
normal! o
execute "normal! Go#endif /* " . gatename . " */"
normal! k
endfunction
autocmd BufNewFile *.{h,hpp} call <SID>insert_gates()
" Automatic header file inclusion (foo.c includes foo.h)
function! s:insert_header_incl()
let filename = expand("%:t")
execute "normal! i#include " . "\"" . substitute(filename, "\\.c", "\\.h", "g") . "\""
normal! o
endfunction
autocmd BufNewFile *.{c,cpp} call <SID>insert_header_incl()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment