英文:
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 ":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
答案1
得分: 1
替换
execute ":windo silent /" . a:pattern . "/|wincmd w"
为
call feedkeys(":windo silent /" . a:pattern . "/|wincmd w\n", "t")
这将字面上将命令键入命令行并执行它,因此命令将存储到 @:
。
英文:
Replace
execute ":windo silent /" . a:pattern . "/|wincmd w"
with
call feedkeys(":windo silent /" . a:pattern . "/|wincmd w\n", "t")
which will literally type the command to the command line and execute it, and so the command will be stored to @:
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论