Vim Tips Wiki

Comment lines in different filetypes

Redirected from VimTip660

1,624pages on
this wiki
Add New Page
Talk0 Share

Duplicate tip

This tip is very similar to the following:

These tips need to be merged – see the merge guidelines.

Tip 660 Printable Monobook Previous Next

created 2004 · complexity intermediate · author SmartDen/Firelex · version 5.7

This code snippet is a part of a vimrc file to set comments in various filetypes. The function CommentIt() decides itself, according to the current file's type which variant of comments to use.

This function should be called with autocmd BufEnter * call CommentIt() somewhere after its declaration to be invoked every time the user enters a new buffer.

function CommentIt ()
  if &filetype == "vim"
    vmap +# :s/^/"/<CR>
    vmap -# :s/^"//<CR>
  elseif &filetype == "tcl"
    vmap +# :s/^/#/<CR>
    vmap -# :s/^#//<CR>
  elseif &filetype == "c"
    vmap +# I/*<Esc>gv<End><Esc>a*/<Esc>
    vmap -# I<Esc>2xgv$<Esc>h2x<Esc>
  elseif &filetype == "cpp"
    vmap +# A<End><CR><Esc>gv:s/^/ *<CR>gvI<Esc>ko<Home>/*<Esc>gvA<Esc>ji */
    vmap -# :s/^..//<CR>gvI<Esc>ddgvA<Esc>dd
  elseif &filetype == "dosbatch"
    vmap +# :s/^/rem /<CR>
    vmap -# :s/^rem //<CR>
autocmd BufEnter * call CommentIt ()


Better yet, try script#23.

You can also put filetyp depending stuff in your local syntax files, e.g.

$HOME/vimfiles/after/syntax/tcl.vim (on Windows)

Not {rtp}/after/syntax/, use {rtp}/ftplugin/. This have nothing to do with syntax files.

I (also) recommend EnhancedCommentify.

Ad blocker interference detected!

Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

Also on Fandom

Random Wiki