 |
Tip #125: Auto commenting for "}"
 |
 |
 |
 |
 |
 |
 |
tip karma |
 |
 |
Rating 23/11, Viewed by 930 |
 |
 |
 |
 |
 |
 |
 |
created |
 |
October 2, 2001 6:48 |
 |
complexity |
 |
basic |
 |
author |
 |
Long Truong |
 |
as of |
 |
6.0 |
 |
I always wanted a script that would auto-comment the end of a conditional block. So, I wrote one. This function searches for the previous matching "{", grabs the line, and inserts it as a comment after the "}". If there is no previous matching "{", it inserts nothing.
So...
if (test){
will generate:
} // if (test)
This is obviously not work if you use a different style. If you use
if (test)
{
then substituting 'getline(".")', use 'getline(line(".") - 1)' should work.
Put the following in your .vimrc:
au BufNewFile,BufRead *.c,*.cc,*.C,*.h imap } <ESC>:call CurlyBracket()<CR>a
function CurlyBracket()
let l:my_linenum = line(".")
iunmap }
sil exe "normal i}"
imap } <ESC>:call CurlyBracket()<CR>
let l:result1 = searchpair('{', '', '}', 'bW')
if (result1 > 0)
let l:my_string = substitute(getline("."), '^\s*\(.*\){', '\1', "")
sil exe ":" . l:my_linenum
sil exe "normal a //" . l:my_string
endif
endfunction
<<Number a group of lines |
how do I get rid of that bold stuff with my xterm? >>
Additional Notes
|