如何理解这个vim脚本?

hug*_*eow 8 vim editor

以下是vim插件的vim脚本:

vim的语法有点奇怪:

  1. !exists("*s:SetVals"),为什么他们之前是明星s:
  2. 功能!,为什么有一个!角色?
  3. &iskeyword,这是一个变量,如果是,它定义在哪里?
  4. 什么是s:g:,它们之间有什么区别呢?
  5. 为什么要使用?比如let &dictionary = g:pydiction_location,我可以改变它&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     
Run Code Online (Sandbox Code Playgroud)

万一

Hei*_*ski 20

1. !exists("*s:SetVals"),为什么他们在s之前是一个明星:?

星号是存在函数的特殊语法,它意味着我们正在检查是否存在一个名为SetVals的现有函数.该选项iskeyword可以与检查exists("&iskeyword")和ex命令echoexists(":echo")

看到 :h exists(

2. function!,为什么有一个!字符?

感叹号表示如果函数已经存在则要替换该函数.

看到 :h user-functions

3. &iskeyword,这是一个变量,如果是,它定义在哪里?

这是一个vim选项.您可以检查它是否已设置:set iskeyword?

4.什么是s:g:它们有什么区别?

这些定义了以下符号的范围.s:表示符号是脚本的本地符号,同时g:表示符号是全局的.

:h internal-variables,s::h script-variable

5.为什么let要使用?如let &dictionary = g:pydiction_location, can i change it to be &dictionary = g:pydiction_location

Vimscript是需要使用关键字声明变量的语言之一.我认为没有办法比使用更容易声明变量let.


rom*_*inl 6

我可以回答一些问题但我会从你最近的问题启发的一般性评论开始.

大多数问题的答案在Vim非常详尽的文档中非常清楚地列出.如果你认真使用Vim,你必须知道如何使用它.从头开始,:help仔细阅读.它付出了代价.相信我.

您可以在中找到所有这些子问题的答案:help expression.

  • !exists("*s:SetVals"),为什么他们之前是明星s:

    :help exists().

  • function!,为什么有一个!角色?

    如果没有感叹号,如果您重新获取脚本,Vim将不会替换先前的定义.

  • &iskeyword,这是一个变量,如果是,它定义在哪里?

    这就是你在脚本中测试vim选项值的方法.见:help iskeyword.

  • 什么是s:g:,它们之间有什么区别呢?

    这些是命名空间.看到:help internal-variables

  • 为什么let要用?比如让&dictionary = g:pydiction_location我可以改变它&dictionary = g:pydiction_location吗?

    不,你不能,:let是你如何定义或更新变量.习惯它.


Xav*_* T. 3

:help eval.txt。它描述了大部分 vimscript 语法。

  • @hugemeow 请参阅 `:h :helpgrep`,`:helpgrep` 是搜索所有文档文件的命令。 (2认同)