gpt4 book ai didi

vim - 使用 vimscript 注释掉代码

转载 作者:行者123 更新时间:2023-12-02 01:11:53 32 4
gpt4 key购买 nike

您好,我正在尝试编写我的第一个 vim 脚本。我想编写一个函数,通过 block 或大括号注释掉 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

最佳答案

  • 你不应该把 :在每一行上 — 除非您在 Vim 命令行中编写函数,Vim 将添加 :自动为您服务。 (不过,最好将脚本写入文件;这样更容易修改和测试。)
  • Vimscript 中的注释以 " 开头(双引号),不是 // .
  • 如果你想执行普通模式命令,比如%dd , 你可以使用 normal! %normal! dd .
  • echo b,e s%...不会工作。如果你想echo正文,试试echo b.','.e.' s%^%//%' .

另外,考虑使用 echom而不是 echo .因为echom将消息保存在消息历史记录中,您可以稍后使用 :mess 重新阅读它.

P.S. 如果您的光标位于打开的 { 上(我看到你试图在你的脚本中使用 %),你可以使用

ctrl-v%I//<esc>

关于vim - 使用 vimscript 注释掉代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16599031/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com