Wikia

Vim Tips Wiki

Comment Lines according to a given filetype

Talk0
1,613pages on
this wiki
Revision as of 10:39, October 12, 2011 by JohnBot (Talk | contribs)

Tip 355 Printable Monobook Previous Next

created 2002 · complexity intermediate · author Luis Mondesi · version 6.0


There is probably an easier way to do this, but, if I cannot find an easy solution for a given problem, I just devise one that works for the meantime -- which usually becomes permanent.

This function comments out lines according to file type. So if a file is .sh, it uses # to comment lines. And if a file is type .c it will start the comments with /* and end them with */.

Put these lines in your vimrc file:

" comment out highlighted lines according to file type
" put a line like the following in your ~/.vim/filetype.vim file
" and remember to turn on filetype detection: filetype on
" au! BufRead,BufNewFile *.sh,*.tcl,*.php,*.pl let Comment="#"
" if the comment character for a given filetype happens to be @
" then use let Comment="\@" to avoid problems...
function CommentLines()
  "let Comment="#" " shell, tcl, php, perl
  exe ":s@^@".g:Comment."@g"
  exe ":s@$@".g:EndComment."@g"
endfunction
" map visual mode keycombo 'co' to this function
vmap co :call CommentLines()<CR>

In filetype.vim you can add things such as:

au BufRead,BufNewFile *.inc,*.ihtml,*.html,*.tpl,*.class set filetype=php
        \ | let Comment="<!-- " | let EndComment=" -->"
au BufRead,BufNewFile *.sh,*.pl,*.tcl let Comment="#" | let EndComment=""
au BufRead,BufNewFile *.js set filetype=html | let Comment="//" | let EndComment=""
au BufRead,BufNewFile *.cc,*.php,*.cxx let Comment="//" | let EndComment=""
au BufRead,BufNewFile *.c,*.h let Comment="/*" | let EndComment="*/"

All set, now whenever you are editing a file of those you have defined in your filetype.vim script, you can just go into Visual mode, highlight what you want to comment out, and type "co". Simple.

See also

Comments

I was used to using 'boxes' as an external filter to comment/decomment, but pushing to the program and back is more 'expensive' then using a simple script, but boxes really is better then EnhancedCommentify.vim (at least I haven't found a way yet to make it behave just like I want it to.).

But I would prefer a maintained thing any day.


The first filetype autocmd uses set filetype=php instead of setf php, which could cause it to replace the filetype (depending on where in the runtime path and file it's placed). It's also the only one which changes a filetype (and it does so a bit overzealously, not all .inc or .class files are php).

I think the setf can be removed altogether, or moved into a different tip, at least.

In fact, these would probably be better in vimrc autocmds as

au FileType php  let Comment="<!-- " | let EndComment=" -->"

et al. Let the pre-flow evaluate the filetype, and the post-flow define the comment fields.

Or probably even after/ftplugin/<filetype>.vim syntax modification files.

--JeremyBarton 07:42, 30 September 2008 (UTC)


Around Wikia's network

Random Wiki