使用vimscript注释掉代码

Bre*_*ett 1 vim

嗨,我正在尝试写我的第一个vim脚本。我想编写一个函数,该函数将按块或大括号将PHP代码注释掉。

这是我想出的,但是我无法使它起作用:

:function Mycom()
    :let b = line(".")
    :echo "this is "b
    // trying to grab the matching bracket, not sure wheather this is ok
    :%
    //keeps missing and going to end og file
    :let e = line(".")
    :echo "here is end" e
    //here is where i want to comment out the block
    :echo b,e s%^%//%
:endfunction
Run Code Online (Sandbox Code Playgroud)

Ken*_*ent 5

  • 您不应该:在每行上都加引号- 除非您在Vim命令行中编写函数,否则 Vim会:为您自动添加。(不过,最好将脚本编写到文件中;这样可以更轻松地进行修改和测试。)
  • Vimscript中的注释以"(双引号)开头,而不是//
  • 如果要执行正常模式命令(例如%或)dd,则可以使用normal! %normal! dd
  • echo b,e s%...将无法正常工作。如果您想echo输入文字,请尝试echo b.','.e.' s%^%//%'

另外,请考虑使用echom代替echo。由于echom将消息保存在消息历史记录中,因此您以后可以使用重新阅读:mess

PS:如果您的光标位于开放位置{(我看到您正在尝试%在脚本中使用),则可以使用

ctrl-v%I//<esc>