简体   繁体   中英

Comment out code using vimscript

Hi Im trying to write my first vim script. I want to write a function that will comment out PHP code by the block or curly brackets.

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. (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 // .
  • If you want to execute a normal mode command, like % or dd , you can use normal! % normal! % or normal! dd normal! dd .
  • echo b,es%... won't work. If you want to echo the text, try echo b.','.e.' s%^%//%' echo b.','.e.' s%^%//%' .

Also, consider using echom instead of echo . Because echom saves the message in the message history, you can re-read it later using :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

ctrl-v%I//<esc>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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