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
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
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.
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.