简体   繁体   English

对错误运行vim命令?

[英]Run vim commands on errors?

Ok so this question is vaguely related to one of my previous questions: 好的,所以这个问题与我以前的问题之一存在模糊的关系:

I want Vim to be able to save and close similarly to Photoshop in regards to buffers? 我希望Vim在缓冲区方面能够像Photoshop一样保存和关闭吗?

Basically the solution I found (or gets really close to what I want), is a plugin called BufOnly which basically closes all buffers that have not been modified. 基本上,我找到的解决方案(或非常接近我想要的解决方案)是一个名为BufOnly的插件,该插件基本上关闭了所有未修改的缓冲区。 So when I have a lot of buffers open and I want to close, I just run this, and then just care of everything that I haven't already. 因此,当我有很多缓冲区要打开并且要关闭时,我只运行它,然后照顾所有我还没有的东西。 It works well. 它运作良好。

But I'm greedy. 但是我很贪心。 I want this to execute automatically when I need it to. 我希望它在需要时自动执行。 Basically I would like it so that if I run qa, if qa runs into --> 基本上,我希望这样,如果我运行qa,如果qa遇到->

E73: No write since list change (add ! to override)

Then I want to run 那我要跑

:BufOnly<CR>:bd <cr>

Is there a way to do that? 有没有办法做到这一点?

you can write a function with vim's try-catch mechanism. 您可以使用vim的try-catch机制编写函数。 example: 例:

 function! Funk()
    try
        execute "qa"
        let yes = 1
    catch /^Vim\%((\a\+)\)\=:E37/   
            execute "BufOnly"
            execute "bd"
    endtry
endfunction

this will catch the error :E37 and do the command you want. 这将捕获错误:E37并执行所需的命令。 I don't have that plugin installed, I therefore didn't test with BufOnly. 我没有安装该插件,因此未对BufOnly进行测试。 I tested with " h gg ", it shows the help page of gg 我用“ h gg ”进行了测试,它显示了gg的帮助页面

to call the command, type :call Funk() , of course you could create mapping for that function call. 要调用该命令,请输入:call Funk() ,当然您可以为该函数调用创建映射。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM