28

This is probably an incredibly simple question, but I did not find any answer so far (I must lack the right sources, and I don't know where to search in vim's help).

I have a condition and I would like it to include 'AND', like

if (condition1 .AND. condition2)
   "do what I want you to do
endif

but I couldn't find the syntax. Same thing for 'OR'.

  • 8
    Heh, .AND. and .OR. is Fortran syntax, I haven't seen that used outside Fortran code in a long, long while. :) Vim uses C-like syntax instead, && for AND and || for OR. See :h expression-syntax for more. – lcd047 Jun 15 '16 at 10:30
  • 1
    I felt compassion in your comment, with *I haven't seen that used outside Fortran code in a long, long while. :) *: thank you for that. As for your comment, if you post it as an answer I'll gladly accept it ! – Feffe Jun 15 '16 at 10:44
  • 2
    I find it confusing how the question title puts "and" and or and "or" next to each other like that. (jk) – Ben Millwood Jun 15 '16 at 13:47
  • 2
    I could have written "and" and/or "or". But I did not dare ! – Feffe Jun 15 '16 at 13:48
29

As @lcd047 said in his comment, vimscript use C-like operators && and ||.

You can find description of their usage on :h expr2. Some important points mentioned by the doc are the following

You'll find that the operators can be concatenated and && takes precedence over ||, so

&nu || &list && &shell == "csh"

Is equivalent to

&nu || (&list && &shell == "csh")

Also once the result is known, the expression "short-circuits", that is, further arguments are not evaluated. This is like what happens in C.

If you use:

if a || b

The expression will be valid even is b is not defined.

  • 2
    Note that I made this answer because @lcd047 didn't wrote one and I thought the note about the doc would be interesting but if lcd047 produces an answer I would gladly delete this one to let him fairly get the credit – statox Jun 15 '16 at 11:22
  • 1
    I doubt it. Sometime ago lcd047 told me they won't be posting answers. – muru Jun 15 '16 at 11:26
  • @muru: I also remembered seeing that somewhere but I wasn't 100% sure it were him and not another user. – statox Jun 15 '16 at 11:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.