Vim:从函数调用 ex 命令(设置)?

发布于 2024-08-27 00:23:43 字数 860 浏览 1 评论 0原文

在这方面画一个空白,谷歌也没有帮助。

想要制作这样的功能:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼泪都笑了 2024-09-03 00:23:43

这不起作用的原因是您试图在表达式中运行命令 - 这些是不同的事情。您使用的 ? 构造只会导致 vim 回显选项的值;这与返回值的函数不同。需要明确的是:问题不在于您从函数调用 ex 命令 - 函数的每隔一行都是 ex 命令 - 问题在于您在表达式中调用 ex 命令。

但这不是执行您在这里尝试的任务的正确方法。感谢 Rampion 的评论,这是最短的方法:

set paste!

现在,如果您需要比反转布尔值更智能的东西,您可以使用 & 将选项名称转换为可用变量。以下是两种使用方法:

" still a function, good for taking extra action (here, printing notification)"
function TogglePaste()
    if (&paste)
        set nopaste
        echo "paste off"
    else
        set paste
        echo "paste on"
    endif
endfunction

" just set the variable directly"
let &paste = !&paste

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:

set paste!

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:

" still a function, good for taking extra action (here, printing notification)"
function TogglePaste()
    if (&paste)
        set nopaste
        echo "paste off"
    else
        set paste
        echo "paste on"
    endif
endfunction

" just set the variable directly"
let &paste = !&paste
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文