Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to understand this vim script?

Tags:

vim

editor

The following is vim script from a vim plugin:

vim's syntax is a bit strange:

  1. !exists("*s:SetVals"), why their is a starmark before s:?
  2. function!, why there is a ! character?
  3. &iskeyword, is this a variable, if yes, where it is 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 to be &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's 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

like image 812
hugemeow Avatar asked Sep 27 '12 15:09

hugemeow


People also ask

What is in Vimscript?

Vim's scripting language, known as Vimscript, is a typical dynamic imperative language and offers most of the usual language features: variables, expressions, control structures, built-in functions, user-defined functions, first-class strings, high-level data structures (lists and dictionaries), terminal and file I/O, ...

What is let G in Vimrc?

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

Is vim easy to learn?

Over the years, the popular mythology around vim has become that it's insanely difficult to learn; a task to be attempted by only those with the thickest of neck-beards. I've heard dozens of times from folks who are convinced it will take them months to reach proficiency.

How do I practice commands in Vim?

The best way to learn is practice. Take a few minutes to try Vim out. If you're on a Linux system right now, open up a terminal and type vim filename. Enter insert mode and type a bit (or copy some of the text from this article into Vim) and then hit Escape to start practicing movement around the file.


2 Answers

1. !exists("*s:SetVals"), why their is a starmark before s:?

The asterisk is special syntax for exists function, and it means that we are checking if there's an existing function called SetVals. The option iskeyword could be checked with exists("&iskeyword") and the ex command echo with exists(":echo")

See :h exists(

2. function!, why there is a ! character?

The exclamation point means that the function is to be replaced if it already exists.

See :h user-functions

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

That is a vim option. You can check if it's set with :set iskeyword?

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

These define the scope of the following symbol. s: means that the symbol is local to the script, while g: means that the symbol will be global.

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

5. why let should be used? 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 variables to be declared with a keyword. I don't think there's a way to declare variables more easily than with let.

like image 127
Heikki Naski Avatar answered Oct 17 '22 07:10

Heikki Naski


I can answer on a few of those but i'll start with a general comment inspired by your recent questions.

The answers to most of your questions are laid out very clearly in Vim's awesomely exhaustive documentation. If you are serious about using Vim you must know how to use it. Start with :help and read carefully. It pays. Trust me.

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

  • !exists("*s:SetVals"), why their is a starmark before s:?

    See :help exists().

  • function!, why there is a ! character?

    Without an exclamation mark, Vim won't replace the previous definition if you re-source your script.

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

    That's how you test the value of a vim option 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? such as let &dictionary = g:pydiction_location, can I change it to be &dictionary = g:pydiction_location?

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

like image 24
romainl Avatar answered Oct 17 '22 06:10

romainl