sponsor Vim development Vim logo Vim Book Ad

FormatComment.vim : Format-block (do a 'gq' format) on the comment the cursor is in

 script karma  Rating 4/1, Downloaded by 1099  Comments, bugs, improvements  Vim wiki

created by
Gary Holloway
 
script type
utility
 
description
" Format-block (do a 'gq' format) on the comment the cursor is in; the type of
" comment is determined by context (i.e., /*...*/, //..., #..., etc.).
"
" EXAMPLE SETUP
" map gqc :call FormatComment()<CR>

DEPENDENCIES:
   FormatBlock.vim ( vimscript #885 )

My *actual* setup is:

" gqc - Format comment; comment-type is determined by context (i.e., /*...*/,
"       //..., #..., etc.).
"
" NOTE: This will probably not work properly in C source for vim versions prior
"       to 6 because some of the search-patterns used are more advanced than
"       were available prior to version 6.
exe SILENT . 'map <unique> gqc :call FormatComment()<CR>'

Where SILENT is set to '' or 'silent! ', depending upon vim's version (does it have silent?)

There is some stuff in there to handle our organization's "standard function header" comments specially; that 'if' block should be easily located and removed, or modified to your tastes.  If left as-is, you probably wouldn't trigger it anyway (so I left it in there as an example/template).

Also, we use the convention that single-line 'C' comments should use the //-style, while multi-line comments should use the /*...*/-style; so that's built into the function.  i.e., If a long //-style comment gets formatted onto multiple lines, it will be converted to /*...*/-style; while a /*...*/-style comment that fits onto a single line will be converted to //-style.  If this is not to your liking, it should be easily remedied; there are function calls made to to the conversions, simply comment the call(s) out (or remove them).

This function allows quick entry/editing of comments without worrying about the line-break formatting.  Once finished, simply hit 'gqc' and the comment is reformatted.
 
install details
I put this in my autoload directory (~/vim/autoload), so the function is automagically loaded as needed via an autocommand:

autocmd FuncUndefined * exe 'runtime autoload/' . expand('<afile>') . '.vim'
 

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
FormatComment.vim 1.4 2004-01-28 6.0 Gary Holloway Initial upload
ip used for rating: 34.235.150.151

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
Vim at Github