Recently in vim recipes Category
This is a vim recipe for creating a function header automatically on a keystroke. For example, if you have a c function:
int foo (char *bar1, int bar2, struct uber bar3)
{
...
}
Running this with the cursor at or above the function will generate this header for you:
/*******************************************************************************
* FUNCTION: foo
*
* ARGUMENTS: char *bar1, int bar2, struct uber bar3
*
* DESCRIPTION:
*
*******************************************************************************/
Actual recipe:
function! FuncHdr()
"Save current position
let line_no = line(".")
"Find the function
normal ^
"execute "norm! /^\\(\\w\\+[ :&*]\\{1,2}\\)\\+\\w\\+\\s*(\<CR>"
execute "norm! /^[^(]*(.*\<CR>"
execute "norm! /(\<CR>b"
"echo getline(".")let fun_name = expand("<cword>")
"echo fun_name
let line_buff = ""
while match(line_buff,")") == -1
let line_buff = line_buff . getline(".")
normal! j
endwhile
"echo line_buff
"remove before bracket
let args_list = substitute(line_buff, ".*(","","")
"remove after bracket
let args_list = substitute(args_list, ").*","","")
"remove whitespaces
let args_list = substitute(args_list, '\s\+'," ","g")
"remove comments
let args_list = substitute(args_list, '\/\*.*\*\/',"","g")
"echo args_list
execute "normal! :".line_no."\<CR>"
execute "normal! A\<CR>\/\<Esc>"
execute "normal! 79A*\<Esc>"
execute "normal! A\<CR> FUNCTION: ".fun_name."\<Esc>"
execute "normal! A\<CR>\<CR>ARGUMENTS: ".args_list."\<Esc>"
execute "normal! A\<CR>\<CR>DESCRIPTION: \<Esc>"
execute "normal! A\<CR>\<CR>\/\<Esc>h78i\*\<Esc>kkA"endfunction
Lately, I was working on a new project, and had to create many .c and .h files. All the header files needed the #ifndef/#define/#endif boilerplating.
Being a lazy typist, following vim macro helped in generating the boiler-plating lines in the header files in a keystroke.
function! CHeader()
"save the line location. This is for already existing files.
let line_no = line(".")
"Get the name of the file
let fname = bufname(bufnr("%"))
"get the name portion out
let fname_root = toupper(fnamemodify(fname, ':r'))
"get the extension
let fname_extn = toupper(fnamemodify(fname, ':e'))
if fname_extn != 'H'
echo "Only applicable to header files"
else
"Create the macro name. This is what you need to change if you
"want a different format.
let define = "__".fname_root."_H"
" Go to first line 0th column"
execute "norm! :1\<CR>0"
" Insert the #ifndef and #define
execute "norm! i#ifndef ".define."\<CR>#define ".define."\<CR>\<Esc>"
"Go to the last line column 0
execute "norm! :$\<CR>0"
"Insert the #endif statement
execute "norm! i\<CR>#endif \/\*".define."\*\/\<Esc>"
"Since we added two lines at the top, go to save line_no + 2
let line_no = line_no + 2
execute "normal! :".line_no."\<CR>"
endif
endfunction