skip to Main Content

Ubuntu 16.04.6 LTS
Vim 7.4

Setting in ~/.vimrc file

if &cp | set noocp | endif

I’m curious about the function of the phrase
What is &cp and what function is |?
I’m also curious about the function of if…endif

Sorry, I’m a beginner If you give me an answer, it will help me a lot

2

Answers


  1. &cp — value of Vim option cp (:h 41.3, :h 'cp'), | — another command in same command line (:h :bar), if ... endif — conditional expression (:h :if).

    Login or Signup to reply.
  2. First, let’s fix that nagging typo:

    if &cp | set nocp | endif
    

    Now we can talk…

    The whole thing is a conditional:

    if <expression that evaluates to 1 ("true")>
      <do something>
    endif
    

    See :help 41.4.

    If you want to save space, you can put a sequence of commands on a single line, separated with |:

    if <expression that evaluates to 1> | <do something> | endif
    

    See :help :bar.

    &cp, or &compatible, is an expression that evaluates to 1 (true in vimscript) if the compatible option is set and 0 (false) if it is disabled:

    if &cp | <do something> | endif
    

    See :help 'compatible' and :help expr-option

    The statement between the :if and the :else is set nocp, which unsets compatible:

    if &cp | set nocp | endif
    

    So, that line tells Vim to check if compatible is set and, if yes, to unset it.


    FWIW, compatible is automatically unset when Vim encounters a vimrc at an expected location so &cp is always going to be evaluated to 0 in your ~/.vimrc, which, basically, makes that line useless outside of very specific use cases that you shouldn’t encounter if you have to ask that question.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search