4

Vim is almost perfect for me. But I still want a line comment and block comment feature, I want to know how to write a vimrc to do this in python and javascript.

No Addons

guilin 桂林
  • 17,050
  • 29
  • 92
  • 146
  • By block comment feature, do you mean select a block with visual mode, and then place block comments around it? Or what? – alesplin Oct 27 '10 at 22:17
  • yes, select a block with visual mode, and then place block comments – guilin 桂林 Oct 28 '10 at 04:18
  • Edited my answer to include a function that will comment out visually selected blocks. – alesplin Oct 29 '10 at 19:38
  • Although the OP asked for line or block comments specifically, I think I should mention this *vimrc only* script which adds javadoc like comments to a function! http://stackoverflow.com/a/8026272/654789 – puk Mar 01 '12 at 23:21

3 Answers3

4

http://www.vim.org/scripts/script.php?script_id=23

http://www.vim.org/scripts/script.php?script_id=1218

sykora
  • 96,888
  • 11
  • 64
  • 71
3

If you want c-style line comments (which I believe are legal in javascript), you can set the following in your .vimrc, which will comment out the line the cursor (in normal mode) is currently on.

map \lo I/*<Esc>A*/<Esc>

If you want python comments, you can do the following:

map \lo I#<Esc>

If you want to only have one statement, you could do:

if match(expand("%:t"), ".py") != -1
  map \lo I#<Esc>
else
  map \lo I/*<Esc>A*/<Esc>
endif

which will use the # comment if you are editing a .py file, and otherwise use the /* ... */ comment.

EDIT: the following function will comment out a visually selected block with the appropriate style comments by checking the filetype. You can then map it to something easy like the vmap statement following the function.

  function! BlockComment(top,bottom)

    " deal with filetypes that don't have block comments 
    let fileName = expand("%:t")
    echo fileName

    if fileName =~ "\.py" || fileName =~ "\.sh" || fileName =~ "\.pl"
        execute "normal I# "
        return
    elseif fileName =~ "\.vim"
        execute 'normal I" '
        return
    endif

    " for c-style block comments (should work for javascript)
    let topLine = line("'<")

    " the + 1 is because we're inserting a new line above the top line
    let bottomLine = line("'>") + 1

    " this gets called as a range, so if we've already done it once we need to
    " bail
    let checkLine = getline(topLine - 1)
    if (checkLine =~ '\/\*')
        return
    endif

    let topString = "normal " . topLine . "GO/*"
    let bottomString = "normal " . bottomLine . "Go*/"

    execute topString
    execute bottomString

  endfunction

  vmap <Leader>bco<CR> :call BlockComment()<CR>

Ignore the wacky syntax highlighting. It appears that the syntax highlighter is not vimscript-aware.

alesplin
  • 1,332
  • 14
  • 23
1

tcomment provides an operator that integrates well enough with the way vim works: http://www.vim.org/scripts/script.php?script_id=1173

It supports fewer commenting styles than the already mentioned nerdcomment though.

lith
  • 929
  • 8
  • 23