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

Vim技能修炼教程(12) - Vim的脚本语言支持的更多相关文章

  1. Vim技能修炼教程(17) - 编译自己的Vim

    编译自己的Vim 前面我们已经对Vim有比较丰富的了解了.我们也知道Vim有很多编译时的选项,很多功能依赖于这些编译选项.其中最重要的就是脚本语言的支持,很多发行版本是不全的.为了支持我们所需要的功能 ...

  2. Vim技能修炼教程(3) - 语法高亮进阶

    语法高亮进阶 首先我们复习一下上节学到的三个命令: * syntax match用于定义正则表达式和规则的对应 * highlight default定义配色方案 * highlight link将正 ...

  3. Vim技能修炼教程(2) - 语法高亮速成

    语法高亮速成 我们继续在人间修行Vim技能之旅.上一次我们学习了如何通过vundle安装插件,这次我们迅速向写插件的方向挺进. 我们先学习一个最简单的语法高亮插件的写法. 语法高亮基本上是由三部分组成 ...

  4. Vim技能修炼教程(13) - 变量

    VimScript变量 上节我们介绍了Python和Ruby来编写Vim插件的方式. 不过,Python和Ruby并不是所有的Vim都支持的功能,如果以最小依赖的原则来说,还是原汁原味的Vimscri ...

  5. Vim技能修炼教程(11) - 代码折叠

    上一讲我们是程序员篇的第一讲,关于代码跳转.代码跳转是一个付出很少收获很大的功能.这一节我们开始一个收获很多,但是付出也相对多一点功能:代码折叠. 代码折叠 折叠的类型 折叠有下面几种类型: * Ma ...

  6. Vim技能修炼教程(1) - 使用vundle管理插件

    世界上有两个伟大的编辑器:一个是emacs,一个是vi.它们从诞生以来,一直在Unix/Linux世界得到最广泛的支持. 尽管过了几十年,在Windows平台上和跨平台上有层出不穷的后起之秀不断挑战它 ...

  7. Vim技能修炼教程(10) - 代码跳转

    程序员功能 前面我们用了5讲的篇幅来讲基本编辑的基本功:第4讲是基本操作,第5讲是操作符,第6讲行编辑ex命令,第7讲可视模式,第8讲多窗口,第9讲缓冲区和标签页. 从这一讲开始,我们从通用功能向程序 ...

  8. Vim技能修炼教程(15) - 时间和日期相关函数

    Vimscript武器库 前面我们走马观花地将Vimscript的大致语法过了一遍.下面我们开始深入看一下Vimscript都给我们准备了哪些武器.如果只用这些武器就够了,那么就太好了,只用Vimsc ...

  9. Vim技能修炼教程(8) - 多窗口

    多窗口 如果一个vim只能开一个窗口,那肯定是有点low.尤其是写代码的时候,打开多个文件是经常的需求. 速成教程 横着切成两个 :split 文件名 上下切换窗口 Ctrl-W加上上下键,可以实现上 ...

随机推荐

  1. Linux安装ftp组件vsftpd

    1 安装vsftpd组件 安装完后,有/etc/vsftpd/vsftpd.conf 文件,是vsftp的配置文件. [root@bogon ~]# yum -y install vsftpd 2 添 ...

  2. 移植gdb到海思3716板子的方法【转】

    本文转载自:https://blog.csdn.net/yuhengyue/article/details/78414403 一,移植方法 环境:ubuntu12.04 交叉编译工具链路径:/opt/ ...

  3. log4j2按日志级别输出到指定文件

    在项目中,可能会产生非常多的日志记录,为了方便日志分析,一般可以将日志按级别输出到指定文件,本次就先说说log4j2的实现吧: 1.先加入log4j2依赖包 2.写一个java类进行测试,类文件中仅仅 ...

  4. Xcode Missing file的解决方案

    因为没在工程里面删除文件,导致Xcode报了一大堆警告,都是Missing file的警告,研究了一下,下面是我的解决方案: Missing file出现的原因 原因就是你在文件里面删除了文件,但是在 ...

  5. linux网络连接的查看和端口的监听

    网络软件都是由客户端和服务端组成,由服务端在服务器上监听指定的端口处理接收到的数据,而客户端是向服务器端监听的端口发送数据,并由服务器端对该数据进行处理,然后将处理结果返回给客户端. 那么我们在lin ...

  6. selenium webdriver模拟鼠标键盘

    在测试使用Selenium webdriver测试WEB系统的时候,用到了模拟鼠标.键盘的一些输入操作. 1.鼠标的左键点击.双击.拖拽.右键点击等: 2.键盘的回车.回退.空格.ctrl.alt.s ...

  7. 在Windows下使用Navicat连接Linux下的MySql

    Linux下的Mysql安装可以参考菜鸟教程:https://www.runoob.com/mysql/mysql-install.html 安装完成后你会发现用Navicat链接MySql会失败,这 ...

  8. bzoj1879: [Sdoi2009]Bill的挑战(codevs2308)(luoguP2167) 状压dp

    唔...懒兔子来写博客了... 点我看题 这题的话...我想了很久但是都不是可行解 刚开始想预处理任意两个串是否可以匹配然后在乱搞,后来发现完全不会写... 然后按照惯例,我会看题解认真的思考... ...

  9. thinkphp3.2.3 + nginx 配置二级域名

    使用的是阿里云centOS.74 第一步: 配置urlpath server { listen ; server_name www.xxxx.com xxxx.com; root /data/www/ ...

  10. php http post发送失败的问题

    //params 是json对象private function HttpPost($url, $params){$data = http_build_query($params);      //写 ...