vim脚本笔记

date: 2017.03.21; modification:2018.03.30

目录:

1 变量

1.1 变量前缀的含义

前缀 含义
函数内: 局部于函数; 否则: 全局
b: 局部于当前缓冲区.
w: 局部于当前窗口.
t: 局部于当前标签页.
g: 全局.
l: 局部于函数.
s: 局部于 :source 的 Vim 脚本.
a: 函数参数 (只限于函数内使用).
v: Vim 预定义的全局变量.

2 函数

2.1 函数概览

2.1.1 窗口(window)操作

tip: 获取当先显示的所有窗口:

let oldWin = winnr() " 记录当前窗口序号, 便于遍历后再返回当前窗口.
let allWins = []
windo call add(allWins, [winnr(), bufname('%')]) " 遍历所有窗口, 并加入列表.
exec oldWin.'wincmd w'                           " 返回之前的窗口

2.1.2 操作当前缓冲区(buffer)

buffer自身操作

增加

删除

修改

查找

2.1.3 字符串操作

2.1.4 列表(List)处理

2.1.5 字典处理

2.1.6 浮点数计算

2.1.7 其它计算

2.1.8 变量

为用户提供某些配置:

if exists("g:xx_xxx") && g:xx_xx
    ...
endif

2.1.9 光标和位置标记位置

2.1.10 系统调用及文件操作

2.1.11 日期和时间

2.1.12 缓冲区及参数列表

2.1.13 命令行

2.1.14 quickfix 和位置列表

2.1.15 插入模式补全

2.1.16 折叠

2.1.17 语法和高亮

2.1.18 拼写:

2.1.19 历史记录

2.1.20 交互

2.1.21 GUI

2.1.22 Vim 服务器

2.1.23 窗口大小和位置

2.1.24 映射

2.1.25 杂项

2.2 常用函数

将元素加入list:

call add(matches, {"word": trigger, "menu": s:snippets[scope][trigger]})

也可以:

matches += [{"word": trigger, "menu": s:snippets[scope][trigger]}]

自动补全:

call complete(col, matches)

内置函数分类列表:

FUNCTIONS

内置函数字母顺序表:

Builtin Functions

3 Python

3.1 插入Python脚本:

function! IcecreamInitialize()
python << EOF
import vim
class StrawberryIcecream:
    def __call__(self):
        print 'EAT ME'
EOF
endfunction

3.2 执行Python脚本:

:pyfile myscript.py

3.3 python的vim对象

vim.tabpages                      *python-tabpages*

vim.eval(str)
vim.bindeval(str)                 *python-bindeval*
vim.strwidth(str)                 *python-strwidth*
vim.foreach_rtp(callable)         *python-foreach_rtp*
vim.chdir(*args, **kwargs)        *python-chdir*
vim.fchdir(*args, **kwargs)       *python-fchdir*
vim.error                         *python-error*
    try:
        vim.command("put a")
    except vim.error:
        # nothing in register a

vim.current                       *python-current*
    vim.current.line        The current line (RW)           String
    vim.current.buffer      The current buffer (RW)         Buffer
    vim.current.window      The current window (RW)         Window
    vim.current.tabpage     The current tab page (RW)       TabPage
    vim.current.range       The current line range (RO)     Range

vim.vars                          *python-vars*
vim.vvars                         *python-vvars*
vim.options # 只能操作全局配置    *python-options*
vim.command("ls")               # vim command, NOT linux command. 相当于执行 `:ls<cr>`

3.4 Buffer

b = vim.current.buffer          # gets the current buffer
b = vim.buffers[n]              # gets buffer "n"

3.4.1 属性

name            = b.name        # gets the buffer file name
num             = b.number
num             = len(b)        # gets the number of lines
(row, col)      = b.mark('a')   # named mark
b.vars["foo"]   = "bar"         # assign b:foo variable
b.options["ff"] = "dos"         # set fileformat
del b.options["ar"]             # same as :set autoread<
b.valid
b.options

3.4.2 操作

增:
  b[0:0] = [ "a line" ]         # add a line at the top
  b.append(str)                 # Append a line to the buffer
  b.append(str, nr)             # Idem, below line "nr"
  b.append(list)                # Append a list of lines to the buffer
  b.append(list, nr)            # Idem, below line "nr"
删:
  del b[n]                      # deletes a line
  del b[n:m]                    # deletes a number of lines
改:
  b[n]   = str                  # sets a line in the buffer
  b[n:m] = [str1, str2, str3]   # sets a number of lines at once
查:
  line   = b[n]                 # gets a line from the buffer
  lines  = b[n:m]               # gets a list of lines
  r      = b.range(n, m)        # return a vim.range object

3.5 Window

w = vim.current.window       # gets the current window
w = vim.windows[n]

3.5.1 属性

number (read-only)      Window number. 从1开始数. 如果0则表示无法确定
height (read-write)     The window height, in rows. hsplit时可用.
width (read-write)      The window width, in columns. vsplit时可用.
w.cursor = (row, col)  
(row, col) = w.cursor
valid (read-write)      True or False. becomes invalid when corresponding window is closed.
vars
options
row, col (read-only)    On-screen window position in display cells. First position is zero.
buffer (read-only)
tabpage (read-only)     Window tab page.

3.6 Range

vim.current.range       The current line range (RO)     Range

3.6.1 属性

r.start     Index of first line into the buffer
r.end       Index of last line into the buffer

3.6.2 操作

r.append(str)
r.append(str, nr)
r.append(list)
r.append(list, nr)

4 snippets

4.1 在分割的窗口打开buffer

如下代码在一个分割窗口(vim window)打开一个指定名称(g:bufferName)的buffer, 其具备功能:

  1. 如果该buffer不存在, 则打开一个新的(在新的分割window).
  2. 如果该buffer已经存在:
    1. 如果已经在打开的window, 则跳转到该window.
    2. 如果未在已打开的window(后台buffer), 则用split在新window将其打开.
    
    let listBufNum = bufnr(g:bufferName)
    if listBufNum == -1                         " Has no buffer
        exe "new " . g:bufferName
    else                                        " Already has buffer
        let listWinNum = bufwinnr(listBufNum)
        if listWinNum != -1                     " Has win ...
            if winnr() != listWinNum            " ... but but current win, then jump to it.
                exe listWinNum . "wincmd w"
            endif
        else                                    " Has no win, then open it by split.
            exe "split +buffer" . listBufNum
        endif
    endif