开发者

Vim boolean function

开发者 https://www.devze.com 2023-03-24 15:26 出处:网络
I am trying to implement a boolean function in Vim and having some trouble and I am sure there is something I\'m missing.

I am trying to implement a boolean function in Vim and having some trouble and I am sure there is something I'm missing.

Just to be clear, I'm looking to implement a function that when called with ! it will do the开发者_运维知识库 opposite.

Vim has plenty of boolean functions, like list and paste. In my case, if I have a function that say, opens a buffer, like:

:call MyFunction()

Then I would like this to close the buffer when is called with a !:

:call MyFunction()!

Not sure if this is even possible, and I am not looking to find out how to open or close a buffer, but the actual boolean implementation.

Edit:

It seems that this is way more feasible if we talk about a user-defined command, like:

:MyCommand action

That can also be called as:

:MyCommand action!


When creating your command, give it the -bang option and then use the <bang>, which will expand to a bang or nothing. Then, to redirect this to your function create a special argument and analyze it to see whether it contains a bang or not. Something like this: (including what ZyX suggested)

function! Bang(bang)
    echo "With".((a:bang)?"":"out")." bang."
endfunction

command! -bang Bg call Bang(<bang>0)

Of course, I'm not doing the correct tests here to check if a:bang is really a bang, but you got the idea.

:Bg
Without bang.  

:Bg!
With bang.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号