Vim在函数内部发出命令时无法识别先前的命令。

huangapple go评论52阅读模式
英文:

Vim does not recognize previous command when issued within a function

问题

我可以在Vim中执行以下命令,然后输入@:来重复执行该命令。这能正常工作:

:windo silent/foo/|wincmd w
@:

然而,当我在一个函数中执行相同的命令时,@:会显示一个错误消息,说没有前一个命令。如何让函数中的命令被注册为@:的命令呢?

function! Find_In_Multiple_Windows(pattern)
  if !empty(a:pattern)
    execute ":windo silent /" . a:pattern . "/|wincmd w"
  endif
endfunction
nnoremap <silent> fw :call Find_In_Multiple_Windows(input("Search for:"))<CR>
fw
Search for: foo
@:
E30: No previous command line
英文:

I can execute the following command in Vim and then enter @: to repeat the command. This works fine:

:windo silent/foo/|wincmd w
@:

However, when I execute the same command within a function, @: displays an error message saying there is no previous command. How can I get the command within the function to be registered as a command for @:?

function! Find_In_Multiple_Windows(pattern)
  if !empty(a:pattern)
    execute &quot;:windo silent /&quot; . a:pattern . &quot;/|wincmd w&quot;
  endif
endfunction
nnoremap &lt;silent&gt;fw :call Find_In_Multiple_Windows(input(&quot;Search for:&quot;))&lt;CR&gt;
fw
Search for: foo
@:
E30: No previous command line

答案1

得分: 1

替换

execute &quot;:windo silent /&quot; . a:pattern . &quot;/|wincmd w&quot;

call feedkeys(&quot;:windo silent /&quot; . a:pattern . &quot;/|wincmd w\n&quot;, &quot;t&quot;)

这将字面上将命令键入命令行并执行它,因此命令将存储到 @:

英文:

Replace

execute &quot;:windo silent /&quot; . a:pattern . &quot;/|wincmd w&quot;

with

call feedkeys(&quot;:windo silent /&quot; . a:pattern . &quot;/|wincmd w\n&quot;, &quot;t&quot;)

which will literally type the command to the command line and execute it, and so the command will be stored to @:.

huangapple
  • 本文由 发表于 2023年6月16日 02:30:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76484554.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定