Vim Tips Wiki
Advertisement

Previous TipNext Tip

Tip: #660 - Comment lines in different filetypes

Created: February 19, 2004 3:53 Complexity: intermediate Author: SmartDen&Firelex Version: 5.7 Karma: 3/4 Imported from: Tip#660

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


IMPORTANT: This function should be called with "autocmd BufEnter * call CommentIt ()" some where 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> 

  endif 

endfunction 

"... 

autocmd BufEnter * call CommentIt ()

Comments

Better yet, try [/scripts/script.php?script_id=23 vimscript#23]

Anonymous , February 19, 2004 8:54


you can also put filetyp depending stuff in your local syntax files, e.g. $HOME/vimfiles/after/syntax/tcl.vim (on Windows)

Anonymous , February 23, 2004 23:12


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

BTW, I (also) recommand EnhancedCommentify.

hermitte {at} free {dot} fr , February 25, 2004 13:23


Advertisement