Vim:从函数调用 ex 命令(设置)?
在这方面画一个空白,谷歌也没有帮助。
想要制作这样的功能:
function JakPaste()
let tmp = :set paste?
if tmp == "paste"
set nopaste
else
set paste
endif
endfunction
map <F2> :call JakPaste()<CR>
但这不起作用。我已经隔离了断线:
function JakPaste()
let tmp = set paste?
endfunction
map <F2> :call JakPaste()<CR>
Pressing F2 results in this error:
Error detected while processing function JakPaste:
line 1:
E121: Undefined variable: set
E15: Invalid expression: set paste?
Hit ENTER or type command to continue
How should I call an ex command (set) from a vim function?
这似乎有点相关,但我仍然不明白。
Drawing a blank on this, and google was not helpful.
Want to make a function like this:
function JakPaste()
let tmp = :set paste?
if tmp == "paste"
set nopaste
else
set paste
endif
endfunction
map <F2> :call JakPaste()<CR>
However this does not work. I have isolated the broken line:
function JakPaste()
let tmp = set paste?
endfunction
map <F2> :call JakPaste()<CR>
Pressing F2 results in this error:
Error detected while processing function JakPaste:
line 1:
E121: Undefined variable: set
E15: Invalid expression: set paste?
Hit ENTER or type command to continue
How should I call an ex command (set) from a vim function?
This seems somewhat relevant however I still don't get it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不起作用的原因是您试图在表达式中运行命令 - 这些是不同的事情。您使用的
?
构造只会导致 vim 回显选项的值;这与返回值的函数不同。需要明确的是:问题不在于您从函数调用 ex 命令 - 函数的每隔一行都是 ex 命令 - 问题在于您在表达式中调用 ex 命令。但这不是执行您在这里尝试的任务的正确方法。感谢 Rampion 的评论,这是最短的方法:
现在,如果您需要比反转布尔值更智能的东西,您可以使用
&
将选项名称转换为可用变量。以下是两种使用方法:The reason this doesn't work is that you're trying to run a command in an expression - those are different things. The
?
construct you used just causes vim to echo the value of the option; this isn't the same as a function returning the value. To be clear: the problem isn't that you're calling an ex command from a function - every other line of the function is an ex command - it's that you're calling the ex command in an expression.But that's not the right way to carry out the task you're attempting here. Here's the shortest way, thanks to rampion's comment:
Now, if you ever need something smarter than just inverting a boolean, you can use
&
to turn an option name into a usable variable. Here are two ways to use that: