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

4
  • 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, 2010 at 22:17
  • yes, select a block with visual mode, and then place block comments Oct 28, 2010 at 4:18
  • Edited my answer to include a function that will comment out visually selected blocks.
    – alesplin
    Oct 29, 2010 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! stackoverflow.com/a/8026272/654789
    – puk
    Mar 1, 2012 at 23:21

3 Answers 3

4

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

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

1
  • The NERD commenter is IMHO the best way to go.
    – dsummersl
    Oct 30, 2010 at 13:16
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.

4
  • you means comment on the cursor line, but how to select a visual block, and comment it. Oct 28, 2010 at 4:18
  • I'll have to do some research on that... :)
    – alesplin
    Oct 29, 2010 at 15:51
  • i added map to my vimrc, but how do I use it?
    – chovy
    Nov 9, 2012 at 22:30
  • Use visual mode to select a block of text, then use <Leader>bco<CR> to call the function. <Leader> usually maps to <kbd>\</kbd>, while <CR> is <kbd>Enter</kbd>. So when you have the text selected, typing \bco then pressing Enter should do it.
    – alesplin
    Nov 9, 2012 at 23:22
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.