Vim:从函数调用ex命令(set)? [英] Vim: Call an ex command (set) from function?

查看:175
本文介绍了Vim:从函数调用ex命令(set)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里画一个空白,而谷歌没有帮助。



想要制作一个这样的功能:

 函数JakPaste )
让tmp =:设置粘贴?
if tmp ==paste
set nopaste
else
set paste
endif
endfunction

map< F2> ; :调用JakPaste()< CR>

然而这不起作用。我已经隔离了这条虚线:

pre $ 函数JakPaste()
let tmp = set paste?
endfunction

map< F2> :调用JakPaste()< CR>

按F2会导致此错误:

 处理函数中检测到的错误JakPaste:
第1行:
E121:未定义变量:set
E15:无效表达式:set paste?
点击ENTER或输入命令以继续

如何调用ex命令(set)从vim函数?

这个似乎有点相关但是我仍然不明白。

解决方案

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



但是,这不是执行你在这里尝试的任务的正确方法。这是最简单的方法,感谢rampion的评论:

 设置粘贴! 

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

 仍然是一个函数,适合采取额外的操作(这里是打印通知)
函数TogglePaste()
if(& paste)
set nopaste
echopaste off
else
set paste
echopaste on
endif
endfunction

直接设置变量
let& paste =!& paste


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.

解决方案

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

这篇关于Vim:从函数调用ex命令(set)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆