Vim Script

Vimscript编程参考

pechorin/any-jump.vim

vim 插件开发入门之 MRU

LeaderF - python3插件及C模块加速

如何使用 Python 编写 vim 插件

Writing Vim plugin in Python

Template - plugins written in Python3


vim help | usr_41 write a vim script | usr_41 中文 | vim 内建函数分类 | vim 内建函数

vimscript入门与进阶 | vim-脚本代码规范

quickmenu | popmenu | dict

https://github.com/skywind3000/writemdict

https://github.com/skywind3000/thousandTrading

g:var   -全局
a:var   -函数参数
l:var   -函数局部变量
b:var   -buffer 局部变量
w:var   -window 局部变量
t:var   -tab 局部变量
s:var   -当前脚本内可见的局部变量
v:var   -Vim 预定义的内部变量

<string> == <string     :字符串相等
<string> != <string>    :字符串不等
<string> =~ <string>    :匹配 pattern
<string> !~ <string>    :不匹配 pattern
<string> =~# <string>   :匹配大小写 pattern
<string> =~? <string>   :不匹配大小写 pattern

let data = readfile('config')
if writefile(data, 'config')
    echomsg 'write error'
endif
字串转list
let S = '[''123'', ''456'', ''789'']'
let L = eval(S)"

if type(getdata) == 4
  for key in keys(getdata)
    let query[key] = getdata[key]
  endfor
endif

if type(l:foo_list) == v:t_list

for [v1, v2] in [[1,2], [3,4]]
endfor

function! b:RangeSize() range
   echo a:lastline - a:firstline
endfunction

let MyClass = {'foo': 'Foo'}
function MyClass.print() dict
   echo self.foo
endfunction
let = instance = deepcopy(MyClass)
call instance.print()
let instance.foo = 'Bar'
call instance.print()
:help usr_41
:help function-list
:help syn-keyword
:help syn-match
:help iskeyword

取选中的文本
function! s:get_visual_selection()
    let [line_start, column_start] = getpos("'<")[1:2]
    let [line_end, column_end] = getpos("'>")[1:2]
    let lines = getline(line_start, line_end)
    if len(lines) == 0
        return ''
    endif
    let lines[-1] = lines[-1][: column_end - 2]
    let lines[0] = lines[0][column_start - 1:]
    return join(lines, "\n")
endfunction

你运行set filetype=derp,vim会查找~/.vim/ftplugin文件。如果这个文件存在的话,vim就会执行它。
如果derp是文件夹,~/.vim/ftplugin/derp/目录下的所有*.vim文件
设置新文件类型:
au BufNewFile,BufRead *.pn set filetype=potion

设置to和time为关键字,并设置高亮
syntax keyword potionKeyword loop  times to while
syntax keyword potionKeyword if elsif else
syntax keyword potionKeyword class return
highlight link potionKeyword Keyword
高亮函数:
syatax keyword potionFunction print join string
highlight   link  potionFunction Function

定义#为注释部分:
syntax match potionComment "\v#.*$"
highlight link potionComment Comment

正则表达式是以\v开始的,这个是在告诉vim用“very magic”模式

高亮操作符:
syntax match potionOperator "\v\*\="     
     syntax match potionOperator "\v/\="
     syntax match potionOperator "\v\+\="
     syntax match potionOperator "\v-\="
     syntax match potionOperator "\v\*"
     syntax match potionOperator "\v/"
     syntax match potionOperator "\v\+"
     syntax match potionOperator "\v-"
     syntax match potionOperator "\v?"
高亮字符串:
syntax region potionString start=/\v"/ skip=/\v\\./ end=/\v"/
highlight link potionString String

外部命令:
:echo system("ls")
:slient !echo Hello, world.

运行:set ft=potion来重新加载它,然后使用b映射。Vim会在屏幕的底部输出字节码,一旦你确定这段代码能够正常工作,就可以吧echom这一行给删除了。

动加载的文件autoload/myplugin/somefile.vm。并且文件内部的函数的定义需要带上全路径:

function myplugin#somefile#Hello()
    " ...
endfunction

Learn vimscript in Y minutes

:setlocal wrap
:autocmd BufNewFile,BufReadFile *.html :write
:autocmd FileType python nnoremap  c I#
d/return   
function Varg(...)
    echom     a:0
    echom     a:1
    echo     a:000
endfunction
call Varg("a","b")
output: 
a:0 = 2
a:1 = a
a:2 = b
a:000 = list of all arguments

:echo split("one two three")
:echo split("one,two,three",",")
:echo join(["foo","bar"],"...")
:echom tolower("Foo")
:echom toupper("Foo")
:execute "echo 'hello'"
需要用双斜杠来表示单斜杠

function! QuickfixToggle()
     if g:quickfix_is_open:
           cclose
           let g:quick_fix_is_open = 0
     else
           copen
           let g:quick_fix_is_open = 1
     endif
endfunction
相对路径和全局路径
:echom expand('%')
:echom expand('%:p')
:echom fnamemodify('foo.txt',':p')
列出当前目录所有文件
:echo globpath('.','*')
:echo split(globpath('.','*.txt'),'\n')

VIM学习笔记 脚本-自定义命令

command! -nargs=1 Dict exec('py youdaoDict(<f-args>)')

-nargs=0    No arguments are allowed (the default)
-nargs=1    Exactly one argument is required
-nargs=*    Any number of arguments are allowed (0, 1, or many)
-nargs=?    0 or 1 arguments are allowed
-nargs=+    Arguments must be supplied, but any number are allowed