How to understand this vim script?

Below is the vim script from the vim plugin:

The vim syntax is a bit weird:

  1. !exists("*s:SetVals") , why are they starmark before s: ::?
  2. function! why eat ! the character?
  3. &iskeyword , is it a variable, if so, where is it defined?
  4. what is s: and g: what is the difference between them?
  5. why let should be used? such as let &dictionary = g:pydiction_location , can I change it as &dictionary = g:pydiction_location ?

if! exists ("* s: SetVals")

  function! s:SetVals() " Save and change any config values we need. " Temporarily change isk to treat periods and opening " parenthesis as part of a keyword -- so we can complete " python modules and functions: let s:pydiction_save_isk = &iskeyword setlocal iskeyword +=.,( " Save any current dictionaries the user has set: let s:pydiction_save_dictions = &dictionary " Temporarily use only pydiction dictionary: let &dictionary = g:pydiction_location " Save the ins-completion options the user has set: let s:pydiction_save_cot = &completeopt " Have the completion menu show up for one or more matches: let &completeopt = "menu,menuone" " Set the popup menu height: let s:pydiction_save_pumheight = &pumheight if !exists('g:pydiction_menu_height') let g:pydiction_menu_height = 15 endif let &pumheight = g:pydiction_menu_height return '' endfunction 

Endif

+8
source share
3 answers

1. !exists("*s:SetVals") , why they are starmark before s:

An asterisk is a special syntax for an existence function, and this means that we check if an existing function called SetVals exists. The iskeyword could be checked with exists("&iskeyword") , and the ex echo command with exists(":echo")

See :h exists(

2. function! why eat! character?

An exclamation mark means that the function must be replaced if it already exists.

See :h user-functions

3. &iskeyword , is this a variable, if so, where is it defined?

This is a variant of vim. You can check if it is installed with :set iskeyword?

4. What is s: and g: what is the difference between them?

They determine the scope of the next character. s: means the character is local to the script, and g: means the character will be global.

See :h internal-variables and for s: see :h script-variable

5. Why use let ? such as let &dictionary = g:pydiction_location, can i change it to be &dictionary = g:pydiction_location ?

Vimscript is one of the languages ​​that require variable declarations with a keyword. I don’t think there is a way to declare variables easier than using let .

+20
source

I can answer a few of them, but I will start with a general comment inspired by your last questions.

The answers to most of your questions are very clearly stated in Vim, a fairly comprehensive documentation. If you are serious about using Vim, you should know how to use it. Start with :help and read carefully. It pays. Believe me.

You can find the answer to all of these subqueries in :help expression .

  • !exists("*s:SetVals") , why are they starmark before s: :?

    See :help exists() .

  • function! why there is a symbol ! ?

    Without an exclamation mark, Vim will not replace the previous definition if you re-place your script.

  • &iskeyword , is this a variable, if so, where is it defined?

    The way you check the value of the vim parameter in a script. See :help iskeyword .

  • what is s: and g: what is the difference between them?

    These are namespaces. See :help internal-variables

  • why let should be used? for example, let &dictionary = g:pydiction_location , can I change it as &dictionary = g:pydiction_location ?

    No, you cannot :let is how you define or update a variable. Get used to it.

+6
source

See :help eval.txt . It describes most of the vimscript syntax.

+3
source

Source: https://habr.com/ru/post/926424/


All Articles