Vim技能修煉教程(12) - Vim的腳本語言支持
Vim的腳本語言支持
本節開始,我們正式接觸vimscript這門古老的腳本語言。
首先要說明,vim支持的擴展語言很多,比如python, python3, ruby, lua,tcl等常見腳本語言都有很好的支持。既可以支持腳本內嵌在.vimrc中,也可以執行python等腳本語言的文件。
運行:version命令就可以看到當前的vim發行版本持哪些擴展語言:
VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Jun 22 2017 03:02:45)
MacOS X (unix) version
Included patches: 1-648
Compiled by travis@Traviss-Mac-712.local
Huge version with MacVim GUI. Features included (+) or not (-):
+acl +cmdline_compl +digraphs +folding +lambda +mouse +multi_lang +profile +statusline +timers +wildignore
+arabic +cmdline_hist +dnd -footer +langmap +mouseshape -mzscheme +python/dyn -sun_workshop +title +wildmenu
+autocmd +cmdline_info -ebcdic +fork() +libcall +mouse_dec +netbeans_intg +python3/dyn +syntax +toolbar +windows
+balloon_eval +comments +emacs_tags +fullscreen +linebreak -mouse_gpm +num64 +quickfix +tag_binary +transparency +writebackup
+browse +conceal +eval -gettext +lispindent -mouse_jsbterm +odbeditor +reltime +tag_old_static +user_commands -X11
++builtin_terms +cryptv +ex_extra -hangul_input +listcmds +mouse_netterm +packages +rightleft -tag_any_white +vertsplit -xfontset
+byte_offset +cscope +extra_search +iconv +localmap +mouse_sgr +path_extra +ruby/dyn -tcl +virtualedit +xim
+channel +cursorbind +farsi +insert_expand +lua/dyn -mouse_sysmouse +perl/dyn +scrollbind +termguicolors +visual -xpm
+cindent +cursorshape +file_in_path +job +menu +mouse_urxvt +persistent_undo +signs +terminfo +visualextra -xsmp
+clientserver +dialog_con_gui +find_in_path +jumplist +mksession +mouse_xterm +postscript +smartindent +termresponse +viminfo -xterm_clipboard
+clipboard +diff +float +keymap +modify_fname +multi_byte +printer +startuptime +textobjects +vreplace -xterm_save
比如我用的macVim,就支持python, python3, ruby, lua, perl等但是不支持tcl.
但是,不管是什麼樣的vim版本,都一定支持vimscript。我們先看看各種語言的用法,最後還是得學習vimscript
Python語言編寫vim插件
:python命令的用法
單行語句可以使用 :python {單行語句}的格式
多行語句使用
:python << EOF
多行python語句
EOF
也可以將python代碼寫到文件,然後通過:pyfile命令來加載。
首先需要引入vim包:
:python import vim
執行ex命令
通過vim.command函數執行ex命令:
:py vim.command(cmd) # execute an Ex command
例:
:py vim.command(":normal! 2j")
這個大家已經很熟悉了,模擬正常模式下的2個j.
窗口相關
- vim.current.window: 獲取當前窗口
- vim.windows[n]:獲取第n個窗口
:py w = vim.windows[n] # gets window "n"
:py cw = vim.current.window # gets the current window
獲取窗口對象之後,就可以針對窗口進行操作:
- height屬性是窗口高度
- width: 寬度
- cursor屬性設置光標在窗口中位置
- buffer: 窗口中的緩衝區
:py w.height = lines # sets the window height
:py w.cursor = (row, col) # sets the window cursor position
:py pos = w.cursor # gets a tuple (row, col)
緩衝區相關
所有的文本都是跟緩衝區相關的。
- vim.current.buffer可獲取當前緩衝區
- vim.buffers[n] : 獲取第n號緩衝區
:py b = vim.buffers[n] # gets buffer "n"
:py cb = vim.current.buffer # gets the current buffer
獲取緩衝區之後,我們就可以對文本進行操作了。
- name: 緩衝區名字,一般就跟文件名一樣
- append: 添加行
- mark: 標記一塊
- range: 獲取一個範圍
緩衝區獲取以後,就可以當成數組一樣的訪問,去處理其中的文本。
如下麵的例子:
:py name = b.name # gets the buffer file name
:py line = b[n] # gets a line from the buffer
:py lines = b[n:m] # gets a list of lines
:py num = len(b) # gets the number of lines
:py b[n] = str # sets a line in the buffer
:py b[n:m] = [str1, str2, str3] # sets a number of lines at once
:py del b[n] # deletes a line
:py del b[n:m] # deletes a number of lines
:py b.append("bottom") # add a line at the bottom
:py n = len(b) # number of lines
:py (row,col) = b.mark('a') # named mark
:py r = b.range(1,5) # a sub-range of the buffer
current對象
對於大部分情況,我們所操作的都是當前對象:
- vim.current.line: 當前行
- vim.current.buffer 當前緩衝區
- vim.current.window 當前窗口
- vim.currrent.range 當前選中的範圍
python3
python 3.x的支持是另外一個不同的模塊。其用法和python基本一致。
ruby語言編寫vim插件
來上個例子:
:ruby print VIM::Window.count
執行ex命令
VIM.command(cmd) # execute an Ex command
窗口相關
num = VIM::Window.count # gets the number of windows
w = VIM::Window[n] # gets window "n"
cw = VIM::Window.current # gets the current window
w.height = lines # sets the window height
w.cursor = [row, col] # sets the window cursor position
pos = w.cursor # gets an array [row, col]
緩衝區相關
num = VIM::Buffer.count # gets the number of buffers
b = VIM::Buffer[n] # gets buffer "n"
cb = VIM::Buffer.current # gets the current buffer
name = b.name # gets the buffer file name
line = b[n] # gets a line from the buffer
num = b.count # gets the number of lines
b[n] = str # sets a line in the buffer
b.delete(n) # deletes a line
b.append(n, str) # appends a line after n
line = VIM::Buffer.current.line # gets the current line
num = VIM::Buffer.current.line_number # gets the current line number
VIM::Buffer.current.line = "test" # sets the current line number
最後更新:2017-07-14 13:02:13