学习 VimScript

发布于 2019-04-19 15:29:16

Install plugin manager

Install Plugin Manager:

curl -fLo ~/.config/nvim/autoload/plug.vim --create-dirs  https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim

The default location of config

The default config file location is: ~/.config/nvim/init.vim

If you’re using Windows 10, config file is located in: ~\AppData\Local\nvim\init.vim

neovim debug

Check Neovim Health: :CheckHealth

What features does vim need

Essentital features for vim: https://dougblack.io/words/a-good-vimrc.html

How to get help

Learn How To Use Help Press Ctrl-] to follow the link (jump to the quickref topic)

Resources to learn Vimscript:

变量

  • let命令用来对变量进行初始化或者赋值。
  • unlet命令用来删除一个变量。
  • unlet!命令同样可以用来删除变量,但是会忽略诸如变量不存在的错误提示。
  • set
    • set is for setting_options_, letfor assigning a value to a_variable_.
    • set wrap& 设置默认值
    • set nowrap unset
    • set wrap! toggle

默认情况下,如果一个变量在函数体以外初始化的,那么它的作用域是全局变量;而如果它是在函数体以内初始化的,那它的作用于是局部变量。同时你可以通过变量名称前加冒号前缀明确的指明变量的作用域: :help internal-variables

  • g:var – 全局
  • a:var – 函数参数
  • l:var – 函数局部变量
  • b:var – buffer 局部变量
  • w:var – window 局部变量
  • t:var – tab 局部变量
  • s:var – 当前脚本内可见的局部变量
  • v:var – Vim 预定义的内部变量
  • $:var - 环境变量
  • &:option - Vim 内部的设置值
  • @ - 寄存器内的值,:reg 查看所有寄存器值

字符串比较

  • <string>==<string>: 字符串相等
  • <string>!=<string>: 字符串不等
  • <string>=~<pattern>: 匹配 pattern
  • <string>!~<pattern>: 不匹配 pattern
  • <operator>#: 匹配大小写
  • <operator>?: 不匹配大小写
  • 注意:设置选项ignorecase会影响 ==!= 的默认比较结果,可以在比较符号添加 ? 或者 # 来明确指定大小写是否忽略。
  • <string>.<string>: 字符串连接

Operators

  • let var -= 2
  • let var += 2
  • let var .= ‘string’

String Functions

  • strlen(var)
  • len(var)
  • strchars(var)
  • split("one two three")
  • split("one,two,three", ",")
  • join(['a', 'b'], ",")
  • tolower('Hello')
  • toupper('Hello')

Functions

  • function! myplugin#hello()
  • call s:Initialize()
  • echo "Results: " . s:Initialize()

Commands

  • command! Save :set fo=want tw=80 nowrap
  • command! Save call <SID>foo()
  • -nargs=0,1,?,*,+

Execute a Command

  • execute "vsplit"
  • execute "e " . fnameescap(filename)

Key strokes

  • normal G
  • normal! G
  • execute "normal! gg/foo\<cr>dd"

Get file names

  • :help expand

Silencing

  • :help silent

Settings

  • set number
  • set nonumber
  • set number!
  • set numberwidth=5
  • set guioptions+=e

Echo

  • echo "hello"
  • echon "hello"
  • echoerr "hello"
  • echomsg "hello"
  • echohl

Built-ins

  • has("feature") :help feature-list
  • executable("python3")
  • globpath(&rtp, "syntax/c.vim")
  • exists("$ENV")
  • exists(":command")
  • exists("variable")
  • exists("+option")
  • exists("g:...")

Prompts

  • let result = confirm("Sure?")
  • execute "confirm q"
  • :help confirm

Mapping

  • [nvixso](nore)map (<buffer>) (<silent>) (<nowait>)

About <SID>

But when a mapping is executed from outside of the script, it doesn’t know in which script the function was defined. To avoid this problem, use “<SID>” instead of “s:”. The same translation is done as for mappings. This makes it possible to define a call to the function in a mapping.”

Boolean

  • 0 is false, 1 is true, str will be convert to number (like in JavaScript)
  • &&
  • ||
  • if <a> | <b> | endif
  • a ? b : c

Identify Operators

  • a is b
  • a isnot b

Lists

  • let mylist = [1, two, 3, "four"]
  • let first = mylist[0]
  • let last = mylist[-1]
  • let second = get(mylist, 1)
  • let second = get(mylist, 1, "NONE")
  • let shortlist = mylist[2:-1]
  • let shortlist = mylist[2:] " same as above
  • let shortlist = mylist[2:2] " one item
  • len(mylist)
  • empty(mylist)
  • sort(mylist)
  • let sortedlist = sort(copy(mylist))
  • let longlist = mylist + [5,6,7]
  • let mylist += [5,6,7]
  • add(mylist, 4)

Map, Filter

  • call map(files, "bufname(v:val)") " use v:val for value
  • call filter(files, 'v:val != ""')

Dict

let colors = {
  \ "apple": "red",
  \ "banana": "yellow",
}
echo colors["apple"]
echo get(colors, "apple")
remove(colors, "apple")
has_key(colors, "apple")
empty(colors)
keys(colors)
len(colors)
max(colors)
min(colors)
count(dict, 'x') ???
string(dict)
map(dict, '<>> " . v:val')

echo keys(g:)
let extend(s:fruits, {...})

Casting

  • str2float('3.14')
  • str2nr('3.14')
  • float2nr(3.14)

Numbers and Floats

  • 1000
  • 0xff
  • 0755
  • 3.14
  • 3.14e4

Math functions

  • sqrt(100)
  • floor(3.14)
  • ceil(3.14)
  • abs(-3.14)
  • sin cos tan
  • sinh cosh tanh
  • asin acos atan

另外

  • Vim 可以创建有序列表,无序hash表
    • let mylist = [1, 2, ['a', 'b']]
    • let mydict = {'blue': "#0000ff", 'foo': {999: "baz"}}
  • 没有布尔类型,整数 0 被当作假,其他被当作真。字符串在比较真假前会被转换成整数,大部分字符串都会被转化为 0,除非以非零开头的字符串才会转化成非零。
  • VimScript 的变量属于动态弱类型
  • let Myfunc = function("strlen") 函数引用
  • if..elseif..else..endif
  • for..in, 可以进行列表模式匹配解构变量
  • while..endwhile
  • try..catch..finally..endtry
  • function! 覆盖函数定义,否则会报错
  • function 定义的后面加 dict,可以将字典内部暴露为 self 关键字,这像是其他语言的单例模式
  • 使用 deepcopy 创建新的类实例
  • call <function> 调用函数
  • 强制创建一个全局函数(使用感叹号),参数使用...这种不定长的参数形式时,a:1 表示...部分的第一个参数,a:2 表示第二个,如此类推,a:0 用来表示...部分一共有多少个参数
  • 有一种特殊的调用函数的方式,可以指明该函数作用的文本区域是从当前缓冲区的第几行到第几行,按照1,3call Foobar()的格式调用一个函数的话,该函数会在当前文件的第一行到第三行每一行执行一遍,再这个例子中,该函数总共被执行了三次
  • 如果你在函数声明的参数列表后添加一个range关键字,那函数就只会被调用一次,这时两个名为a:firstlinea:lastline的特殊变量可以用在该函数内部使用

VIM Setting Articles:

comments powered by Disqus