解释Python编写vim插件

Vim 插件是一个 .vim 的脚本文件,定义了函数、映射、语法规则和命令,可用于操作窗口、缓冲以及行。一般一个插件包含了命令定义和事件钩子。当使用
Python 编写 vim 插件时,函数外面是使用 VimL 编写,尽管 VimL 学起来很快,但 Python 更加灵活,例如可以用
urllib/httplib/simplejson 来访问某些 Web 服务,这也是为什么很多需要访问 Web 服务的插件都是使用 VimL + Python
编写的原因。

在开始编写插件之前,你需要确认 Vim 支持 Python,通过以下命令来判别:

vim --version | grep +python

接下来我们通过一个简单的例子来学习用 Python 编写 Vim 插件,该插件用来获取 Reddit 首页信息并显示在当前缓冲区上。

首先在 Vim 新建 vimmit.vim 文件,我们首先需要判断是否支持 Python,如果不支持给出提示信息:


1

2

3

4


if !has(‘python‘)

  echo "Error: Required vim compiled with +python"

  finish

endif

上面这段代码就是用 VimL 编写的,它将检查 Vim 是否支持 Python。

下面是用 Python 编写的 Reddit() 主函数:


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62


" Vim comments start with a double quote.

" Function definition is VimL. We can mix VimL and Python in

" function definition.

function! Reddit()

  

" We start the python code like the next line.

  

python << EOF

# the vim module contains everything we need to interface with vim from

# python. We need urllib2 for the web service consumer.

import vim, urllib2

# we need json for parsing the response

import json

  

# we define a timeout that we‘ll use in the API call. We don‘t want

# users to wait much.

TIMEOUT = 20

URL = "codego.net"

  

try:

  # Get the posts and parse the json response

  response = urllib2.urlopen(URL, None, TIMEOUT).read()

  json_response = json.loads(response)

  

  posts = json_response.get("data", "").get("children", "")

  

  # vim.current.buffer is the current buffer. It‘s list-like object.

  # each line is an item in the list. We can loop through them delete

  # them, alter them etc.

  # Here we delete all lines in the current buffer

  del vim.current.buffer[:]

  

  # Here we append some lines above. Aesthetics.

  vim.current.buffer[0] = 80*"-"

  

  for post in posts:

    # In the next few lines, we get the post details

    post_data = post.get("data", {})

    up = post_data.get("ups", 0)

    down = post_data.get("downs", 0)

    title = post_data.get("title", "NO TITLE").encode("utf-8")

    score = post_data.get("score", 0)

    permalink = post_data.get("permalink").encode("utf-8")

    url = post_data.get("url").encode("utf-8")

    comments = post_data.get("num_comments")

  

    # And here we append line by line to the buffer.

    # First the upvotes

    vim.current.buffer.append("↑ %s"%up)

    # Then the title and the url

    vim.current.buffer.append("  %s [%s]"%(title, url,))

    # Then the downvotes and number of comments

    vim.current.buffer.append("↓ %s  | comments: %s [%s]"%(down, comments, permalink,))

    # And last we append some "-" for visual appeal.

    vim.current.buffer.append(80*"-")

  

except Exception, e:

  print e

  

EOF

" Here the python code is closed. We can continue writing VimL or python again.

endfunction

使用如下命令保存文件

:source vimmit.vim

然后调用该插件:

:call Reddit()

这个命令用起来不那么方便,因此我们再定义一个命令:

command! -nargs=0 Reddit call Reddit()

我们定义了命令:Reddit来调用这个函数。-nargs 参数声明命令行中有多少个参数。

关于函数参数的问题:

问:如何访问函数中的参数?


1

2

3

4

5

6

7

8

9

10

11

12

13

14


function! SomeName(arg1, arg2, arg3)

  " Get the first argument by name in VimL

  let firstarg=a:arg1

  

  " Get the second argument by position in Viml

  let secondarg=a:1

  

  " Get the arguments in python

  

  python << EOF

  import vim

  

  first_argument = vim.eval("a:arg1") #or vim.eval("a:0")

  second_argument = vim.eval("a:arg2") #or vim.eval("a:1")

你可以使用 ... 来处理可变个数参数来替换特定的参数名,可通过位置或者命名参数来访问,如:(arg1, arg2, ...)

问:如何在 Python 中调用 Vim 命令?

vim.command("[vim-command-here]")

问:如何定义全局变量,并在 VimL 和 Python 中访问?

全局变量使用形如 g:. 的前缀,定义全局变量前应该检查该变量是否已定义:


1

2

3


if !exists("g:reddit_apicall_timeout")

  let g:reddit_apicall_timeout=40

endif

然后你通过下面代码在 Python 中访问这个变量:


1

TIMEOUT = vim.eval("g:reddit_apicall_timeout")

可通过下面的方法来对全局变量进行重新赋值:


1

let g:reddit_apicall_timeout=60

更多关于使用 Python 编写 Vim 插件的说明请看官方文档。

codego.net代码节选

时间: 2024-08-02 17:00:54

解释Python编写vim插件的相关文章

[Misc] python 开发vim 插件初步测试

[Misc] python 开发vim 插件初步测试 今日雨夹雪, 晚上闲来没事突然想了解下用python试试VIM插件开发. 于是写了个初步测试. 总体来说相当简单, 几个相当设置语法后, import vim 后就进入python世界了. (相当操作命令vim内:help py了解更多) 测试代码, 放到.vim/plugin目录下, *.vim vim内: Helloworld 和 : Helloname abeen 调用测试方法. 测试如下: 1 " vim plugin test 2

python学习-vim插件安装

centos7上自带python2.7,我们需要优化一下python环境. 一.使用豆瓣源加速软件安装 pip install -i   flask    #使用-i 选项 mkdir ~./pip && vim pip.conf        #修改pip的配置文件 [global] index-url = https://pypi.douban.com/simple/ 二.修改.vimrc文件 主要增加一些配置选项,例如显示行号,一键执行等 vim .vimrc set nocompa

nagios3 添加Python编写的插件

Nagios 系统提供了一个插件NRPE.Nagios 通过周期性的运行它来获得远端服务器的各种状态信息.它们之间的关系如下图所示: Nagios 通过NRPE 来远端管理服务 1. Nagios 执行安装在它里面的check_nrpe 插件,并告诉check_nrpe 去检测哪些服务. 2. 通过SSL,check_nrpe 连接远端机子上的NRPE daemon 3. NRPE 运行本地的各种插件去检测本地的服务和状态(check_disk,..etc) 4. 最后,NRPE 把检测的结果传

Vundle管理vim插件实现python自动补全

vim使用方便,如果安装了插件就更完美了. 下面介绍vundle管理vim插件,配置python环境. 示例图 下载安装Vundle,修改~/.vimrc 配置文件,写python程序能够实现自动补全,提示等. " Source a global configuration file if available "if filereadable("/etc/vim/vimrc.local") " source /etc/vim/vimrc.local &qu

PyDev的安装(编写Python的Eclipse插件)

题目:poj 2912 Rochambeau(带权并查集 + 暴力) 题目大意:题目给出三个团队和一个裁判,这三个团队和裁判一起玩剪刀石头布,然后规定每个团队必须出一样的,只有裁判可以任意出.然后给出关系,x > y 代表 x 赢y , x < y代表 y 赢 x , 相等则出的一样.问这样的关系可以推出裁判是哪个吗?可以需要说明从第一条到第几条推出来的,不可以也要说明是不可能出现这样的关系,还是裁判不唯一. 解题思路:这题重点是裁判在里面会扰乱关系,并且n * m 才 100000,完全可以

vim插件详细安装过程

1 写在前面   Linux下编程一直被诟病的一点是: 没有一个好用的IDE, 但是听说Linux牛人, 黑客之类的也都不用IDE. 但是对我等从Windows平台转移过来的Coder来说, 一个好用的IDE是何等的重要啊, 估计很多人就是卡在这个门槛上了, "工欲善其事, 必先利其器"嘛, 我想如果有一个很好用的IDE, 那些Linux牛人也会欢迎的. 这都是劳动人民的美好愿望罢了, 我今天教大家把gvim改装成一个简易IDE, 说它"简易"是界面上看起来&quo

跟我一起学习VIM - vim插件合集

2016-06-14 15:04 13333人阅读 评论(0) 收藏 举报 分类: Linux(104)  目录(?)[+]  前两天同事让我在小组内部分享一下VIM,于是我花了一点时间写了个简短的教程.虽然准备有限,但分享过程中大家大多带着一种惊叹的表情,原来编辑器可以这样强大,这算是对我多年来使用VIM的最大鼓舞吧.所以分享结束之后,将这篇简短教程整理一下作为我2014年的第一篇Blog. 目录写在前面:Life Changing Editor什么是VIM为什么选VIM为什么选其它为什么犹豫

【转载】跟我一起学习VIM - vim插件

目录 写在前面:Life Changing Editor 什么是VIM 为什么选VIM 为什么选其它 为什么犹豫选择它们 VIM >= SUM(现代编辑器) 如何学习VIM 一秒钟变记事本 VIM的基本用法 VIM进阶:插件 插件管理神器:Vundle 配色方案 导航与搜索 自动补全 语法 其它 终极配置: spf13 与其它软件集成 一些资源 写在最后 搭完网站之后的第一篇文章有些兴奋,先变身话痨简单回顾一下我是如何接触到VIM的,不感兴趣的同学可以直接跳过这一部分:-) 写在前面:Life

[vundle]利用vundle工具来管理vim插件

转自:http://os.51cto.com/art/201507/484174.htm Vim是Linux上一款用途广泛的轻量级文本编辑工具.虽然对普通的Linux用户来说开始学用起来难度相当大,但鉴于它具有的种种好处,完全值得一学.至于功能方面,Vim可以通过插件实现全面定制.不过由于其高级配置,你可能需要在其插件系统上花一番时间,才能够高效地对Vim进行个性化定制.幸好,我们有几个工具可以简化我们使用Vim插件.Vundle就是本人每天使用的一款工具. 1. Vundle简介 Vundle