I like to have comments like this in my C code:
/********************************************************
* Blah
********************************************************/
But I get tired of typing all those asterisks, and copy/pasting can also get annoying. I was wondering if I could possibly create a macro with Vim so that if I press (for example) CTRL+L
, it'll automatically insert that structure in my code and align the cursor in the middle (where the actual comment is written).
Any advice would be appreciated. Thanks!
Take a look at snipmate, a vim plugin for TextMate-like snippets.
- https://github.com/garbas/vim-snipmate
- 1min screencast: http://vimeo.com/3535418
snipMate.vim implements some of TextMate's snippets features in Vim. A
snippet is a piece of often-typed text that you can insert into your
document using a trigger word followed by a <tab>.
If you are planning to go on snipmate (which IMHO is a good choice!), you should consider using the version maintained by Garbas on github (pay attention to the required plugins in the readme file).
The version from msanders is no longer maintained.
Since I am new I can't add a comment to the answer above... Sorry
You can use cvim, and edit ~/.vim/c-support/templates/c.comments.template
to your liking.
Heh, just today I was beefing up my own support for this (as these headers are mandated by company coding standard).
iabbrev //== // (96 equal characters)<Enter>//<Enter>// (96 equal characters)<C-O>k
iabbrev //-- // (96 dashes)<Enter>//<Enter>// (96 dashes)<C-O>k
These allow me to type //== and when I press space the whole thing is entered and I'm left with the cursor "in the middle" where I want it.
So- for the OP, if you want to use Ctrl-L, do something like this:
inoremap <C-L> /*****<Enter> * <Enter>*****<C-O>k
(I like abbreviations myself, though...)
Since there are other points in our coding standard where we need to extend dashes and equals out to column 100, I whipped this up today:
iabbrev <expr> === InsertTo99Width('===')
iabbrev <expr> --- InsertTo99Width('---')
function! InsertTo99Width(insertion)
let l:line = getline('.')
let l:linelen = strlen(l:line)
let l:col = col('.')
let l:numchars = (99 - col)
let l:inserted = a:insertion
if (l:linelen <= l:col) && (l:numchars > 0)
let l:numchars += len(l:inserted)
while len(l:inserted) < l:numchars
let l:inserted = l:inserted . l:inserted
endwhile
return l:inserted[0 : l:numchars]
else
return l:inserted
endif
endfunction
(I'm sort of a vim scripting newb so there may be an easier way to make a string of N characters long, but this allows arbitrary strings of arbitrary length to be repeated so if you like dash-space-equal-space or something, it'll still look "nice".)
Anyway finally- you can use <expr>
in the inoremap too if you want, so you can make your asterisks go to a set width based on the output of a function. There's a lot of power in there, but it may take some fiddling to get it just the way you want...