简体   繁体   English

使用vimscript注释掉代码

[英]Comment out code using vimscript

Hi Im trying to write my first vim script. 嗨,我正在尝试写我的第一个vim脚本。 I want to write a function that will comment out PHP code by the block or curly brackets. 我想编写一个函数,该函数将按块或大括号将PHP代码注释掉。

Here is what I've come up with, but I can't get it to work: 这是我想出的,但是我无法使它起作用:

: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
  • You shouldn't put a leading : on each line — unless you're writing the function in the Vim command line, Vim will add the : automatically for you. 您不应该在每行上加一个:除非您在Vim命令行中编写函数, 否则 Vim会自动为您添加: (It'd be better to write your script in a file, though; that way it's easier to modify and test.) (不过,最好将脚本编写到文件中;这样可以更轻松地进行修改和测试。)
  • Comments in Vimscript start with " (a double quote), not // . Vimscript中的注释以" (双引号)开头,而不是//
  • If you want to execute a normal mode command, like % or dd , you can use normal! % 如果要执行普通模式命令,例如%dd ,则可以使用normal! % normal! % or normal! dd normal! %normal! dd normal! dd . normal! dd
  • echo b,es%... won't work. echo b,es%...无效。 If you want to echo the text, try echo b.','.e.' s%^%//%' 如果您想echo显文本,请尝试echo b.','.e.' s%^%//%' echo b.','.e.' s%^%//%' . echo b.','.e.' s%^%//%'

Also, consider using echom instead of echo . 另外,考虑使用echom代替echo Because echom saves the message in the message history, you can re-read it later using :mess . 由于echom将消息保存在消息历史记录中,因此您可以稍后使用:mess重新阅读。

PS If your cursor is on an open { (I saw you're trying to use % in your script), you can comment the block using PS:如果您的光标位于开放{ (我看到您正在尝试在脚本中使用% ),则可以使用

ctrl-v%I//<esc>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM