Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim boolean function

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!
like image 651
alfredodeza Avatar asked Aug 01 '11 16:08

alfredodeza


People also ask

What is let G in Vimrc?

l: local to a function. g: global. :help internal-variables. Follow this answer to receive notifications.

How do you call a function in Vim?

Calling A Function Vim has a :call command to call a function. The call command does not output the return value. Let's call it with echo . To clear any confusion, you have just used two different call commands: the :call command-line command and the call() function.

How do I use echo in Vim?

Try out echo by running the following command: :echo "Hello, world!" You should see Hello, world! appear at the bottom of the window.

How do I run a script in Vim?

You can always execute Vimscript by running each command in command mode (the one you prefix with : ), or by executing the file with commands using a :source command. Historically, Vim scripts have a . vim extension. Here, :so is a short version of :source , and % refers to the currently open file.


1 Answers

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.
like image 149
sidyll Avatar answered Sep 29 '22 09:09

sidyll