Vim对我来说几乎是完美的。但我仍然希望有行注释和块注释功能,我想知道如何编写一个vimrc文件,在python和javascript中实现这个功能。
不需要插件
Vim对我来说几乎是完美的。但我仍然希望有行注释和块注释功能,我想知道如何编写一个vimrc文件,在python和javascript中实现这个功能。
不需要插件
map \lo I/*<Esc>A*/<Esc>
map \lo I#<Esc>
如果你只想要一个语句,可以这样做:
if match(expand("%:t"), ".py") != -1
map \lo I#<Esc>
else
map \lo I/*<Esc>A*/<Esc>
endif
如果您正在编辑 .py 文件,则使用 # 注释,否则使用 /* ... */ 注释。
编辑:以下函数将通过检查文件类型以适当的样式注释来注释出一个可视选定的块。 然后,您可以将其映射到像下面的 vmap 语句这样简单的东西。
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>
请忽略奇怪的语法高亮。看起来语法高亮器不支持vimscript。
\bco
然后按下Enter
即可完成操作。 - alesplintcomment提供了一个运算符,与vim的工作方式很好地集成在一起: http://www.vim.org/scripts/script.php?script_id=1173
它支持比之前提到的nerdcomment更少的注释样式。