22

I have this oneliner in my vimrc but it does not work :

if has("patch-7.4.710") | set listchars+=space:· else | set listchars+=trail:· | endif

VIM says :

E518: Unknown option: else

Can you help me ?

SebMa
  • 2,968
  • 2
  • 12
  • 15

1 Answers1

32

You need to put another | before else otherwise vim will think your command is set listchars+=space:· else instead of two separate commands. That's why you got the error message: else is an unknown option for the command set.

The correct syntax is the following:

if has("patch-7.4.710") | set listchars+=space:· | else | set listchars+=trail:· | endif
statox
  • 49,782
  • 19
  • 148
  • 225
  • I was struggling to where would I place the |, before or after the else ? Now, I now it's both :-D. Thanks a lot :) ! – SebMa Sep 11 '17 at 09:52
  • 2
    @SebMa: Think of how vim would parse your code if it was written on several lines: instruction 1 if has("patch-7.4.710"), instruction 2 set listchars+=space:·, instruction 3 else, instruction 4 set listchars+=trail:· and last instruction endif. As the | are used to separate instructions it is only logical to put one before else and one after :) – statox Sep 11 '17 at 09:58
  • 1
    It's logical as long as one knows the language. In C, we use ; to separate instructions . In C else is not an instruction, otherwise there would be a ; after it :) – SebMa Sep 11 '17 at 10:02
  • Yup that's a good point. VimL can be tricky sometimes. – statox Sep 11 '17 at 10:03
  • 1
    The semantics of VimScript are a lot closer to shell scripting than C @SebMa. – Martin Tournoij Sep 11 '17 at 10:31
  • @Carpetsmoker In shell, here is the syntax of a oneliner if else fi statement : if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ]... [ else COMMANDS; ] fi As you can see, there is no instruction separator right after the else statement like in VimL – SebMa Sep 11 '17 at 11:52
  • 1
    @SebMa No, but the semantics of "(almost) everything including control structures are a command" is similar to VimScript (and different from C). – Martin Tournoij Sep 11 '17 at 12:23