pythonvim使用教程_用 Python 写 Vim 插件的套路是什么?

本文介绍了如何使用Python编写Vim插件,从确认Vim是否支持Python开始,通过一个简单的HelloVim插件实例,演示如何在Vim脚本中调用Python函数。接着,展示了如何利用vim-plugin-starter-kit生成插件目录结构,并编写一个将文本发送到Ubuntu Pastebin的插件。通过编辑`vim_ubuntu_pastebin.vim`文件,定义了一个`Pastebin`命令,允许用户在Vim中直接将当前文件内容贴到Ubuntu Pastebin。
摘要由CSDN通过智能技术生成

之前尝试过一个简单的小插件,还记录了一篇博客:

步骤如下:

看了一个youtube视频讲怎么用python写vim插件的,笔者以前的经验只限于使用vim,并不会vim脚本。现学现卖,同想写插件的可以看看,即使不用深入理解,照葫芦画瓢也不难。简单的插件从学习到实现大概只会花你半天到一天左右的时间,以后你就可以定制你的vim编辑器了。

Writing Vim plugins with Python

让vim支持python函数

首先确认你的vim编译时支持python或者python3,用vim --version | grep +python查看输出,否则你可能要重新编译安装vim。 笔者之前没有写过vim脚本,由于对python比较熟悉,现在用python实现。这里举个简单的例子,vim example.vim

function! HelloVim()

echo "Hello Vim"

endfunction

然后在vim里执行 :source %,之后在 :call HelloVim() 就能看到函数执行结果了。

怎么在vim脚本里使用python函数呢?这里有个简单的文档 Vim documentation: if_pyth

来用python实现一个类似的Hello函数

" vim sript function

function! HelloVim()

echo "Hello Vim"

endfunction

" use python function, vim compiled with +python or +python3

function! HelloPython()

python3 << endOfPython

print('Hello Python')

endOfPython

endfunction

我的vim使用+python3编译的,你可能需要把上边的python3换成python。然后在vim里执行 :source %,之后在 :call HelloVim() 就能看到函数执行结果了。

vim插件的目录结构

我们直接偷懒使用项目生成框架starter kit(就和我直接偷懒用flask-cookitecutter一样),项目仓库在这里vim-plugin-starter-kit 如果你有兴趣也可以看看这个项目的wiki,有一些教程。 直接克隆项目,进入文件夹以后执行python create_plugin_scaffold.py然后填写一下项目名称就好了,直接帮助你生成一个项目模板。我的项目树如下:

vim-ubuntu-pastebin

├── README.md

├── doc

│ └── vim-ubuntu-pastebin.txt

└── plugin

├── __init__.py

├── test.txt

├── tests

│ ├── __init__.py

│ └── vim_ubuntu_pastebin_tests.py

├── vim_ubuntu_pastebin.py

└── vim_ubuntu_pastebin.vim

编写简单的vim插件

我的需求有两个,一个是把当前的文本发送到 http://paste.ubuntu.com ,还有一个是把该功能集成到vim里。第一个功能我们使用python完成,功能也比较简单。我们去网站上随便粘贴一段文本用浏览器开发者工具观察发送的post请求就好了,之后用requests模拟提交(也可以用python内置的urllib,不过这里还要兼容python2和python3,所以直接用requests方便很多),编辑vim_ubuntu_pastebin_tests.py文件如下:

# -*- coding: utf-8 -*-

import os

import requests

import webbrowser

try:

from urllib import urlencode # py2

except ImportError:

from urllib.parse import urlencode # py3

PASTEBIN_SUPPORT_TYPE = frozenset([

'text', 'Cucumber', 'abap', 'ada', 'ahk', 'antlr', 'antlr-as', 'antlr-cpp', 'antlr-csharp', 'antlr-java', 'antlr-objc', 'antlr-perl', 'antlr-python', 'antlr-ruby', 'apacheconf', 'applescript', 'as', 'as3', 'aspx-cs', 'aspx-vb', 'asy', 'basemake', 'bash', 'bat', 'bbcode', 'befunge', 'blitzmax', 'boo', 'c', 'c-objdump', 'cfm', 'cfs', 'cheetah', 'clojure', 'cmake', 'coffee-script', 'common-lisp', 'console', 'control', 'cpp', 'cpp-objdump', 'csharp', 'css', 'css+django', 'css+erb', 'css+genshitext', 'css+mako', 'css+myghty', 'css+php', 'css+smarty', 'cython', 'd', 'd-objdump', 'delphi', 'diff', 'django', 'dpatch', 'duel', 'dylan', 'erb', 'erl', 'erlang', 'evoque', 'factor', 'felix', 'fortran', 'gas', 'genshi', 'genshitext', 'glsl', 'gnuplot', 'go', 'gooddata-cl', 'groff', 'haml', 'haskell', 'html', 'html+cheetah', 'html+django', 'html+evoque', 'html+genshi', 'html+mako', 'html+myghty', 'html+php', 'html+smarty', 'html+velocity', 'hx', 'hybris', 'ini', 'io', 'ioke', 'irc', 'jade', 'java', 'js', 'js+cheetah', 'js+django', 'js+erb', 'js+genshitext', 'js+mako', 'js+myghty', 'js+php', 'js+smarty', 'jsp', 'lhs', 'lighty', 'llvm', 'logtalk', 'lua', 'make', 'mako', 'maql', 'mason', 'matlab', 'matlabsession', 'minid', 'modelica', 'modula2', 'moocode', 'mupad', 'mxml', 'myghty', 'mysql', 'nasm', 'newspeak', 'nginx', 'numpy', 'objdump', 'objective-c', 'objective-j', 'ocaml', 'ooc', 'perl', 'php', 'postscript', 'pot', 'pov', 'prolog', 'properties', 'protobuf', 'py3tb', 'pycon', 'pytb', 'python', 'python3', 'ragel', 'ragel-c', 'ragel-cpp', 'ragel-d', 'ragel-em', 'ragel-java', 'ragel-objc', 'ragel-ruby', 'raw', 'rb', 'rbcon', 'rconsole', 'rebol', 'redcode', 'rhtml', 'rst', 'sass', 'scala', 'scaml', 'scheme', 'scss', 'smalltalk', 'smarty', 'sourceslist', 'splus', 'sql', 'sqlite3', 'squidconf', 'ssp', 'tcl', 'tcsh', 'tex', 'text', 'trac-wiki', 'v', 'vala', 'vb.net', 'velocity', 'vim', 'xml', 'xml+cheetah', 'xml+django', 'xml+erb', 'xml+evoque', 'xml+mako', 'xml+myghty', 'xml+php', 'xml+smarty', 'xml+velocity', 'xquery', 'xslt', 'yaml', ])

def post_buffer_to_ubuntu_pastebin(buffer, filetype, open_in_borwser=True):

""" post current file buffer content to http://paste.ubuntu.com/

and return url.

Args:

buffer (vim.current.buffer)

filetype (str)

Returns:

url (str)

"""

lines = os.linesep.join(list(buffer))

url = 'http://paste.ubuntu.com/'

data = urlencode(

{

'poster': os.environ.get('USER', 'anonymous'),

'syntax': filetype if filetype in PASTEBIN_SUPPORT_TYPE else 'text',

'content': lines

}

).encode('utf-8')

r = requests.post(url, data=data, allow_redirects=True)

if open_in_borwser:

webbrowser.open_new_tab(r.url)

return r.url

这样发送文本内容到 http://paste.ubuntu.com 的需求就完成了,还差一步就是怎么在vim里能够使用这段python代码。接下来编辑 vim_ubuntu_pastebin.vim文件,看着也挺简单:

" --------------------------------

" Add our plugin to the path

" --------------------------------

if !(has('python3') || has('python'))

echo "Error: Required vim compiled with +python or +python3"

finish

endif

if has('python3')

python3 import sys

python3 import vim

python3 sys.path.append(vim.eval('expand(":h")'))

function! Pastebin()

python3 << endOfPython

from vim_ubuntu_pastebin import post_buffer_to_ubuntu_pastebin

filetype = vim.eval('&filetype') " 获取文件类型

url = post_buffer_to_ubuntu_pastebin(vim.current.buffer, filetype)

print(url)

endOfPython

endfunction

endif

" 下边代码仅仅是'python3'替换成了'python'

if has('python')

python import sys

python import vim

python sys.path.append(vim.eval('expand(":h")'))

function! Pastebin()

python << endOfPython

from vim_ubuntu_pastebin import post_buffer_to_ubuntu_pastebin

filetype = vim.eval('&filetype')

url = post_buffer_to_ubuntu_pastebin(vim.current.buffer, filetype)

print(url)

endOfPython

endfunction

endif

" 定义一个命令Pastebin就可以在终端

command! -nargs=0 Pastebin call Pastebin()

我们直接在这个vim脚本里调用上边的python函数就可以了,使用list(vim.current.buffer)就能获取当前buffer里每行的内容,用换行符拼起来,之后把内容发送出去就好了。这样就实现了一个简单的vim插件了,是不是挺容易的。把当前项目推到github上,别人就可以使用Bundle等工具使用你的插件了。亲测有效,以后贴代码方便多了,在你的vim里头执行命令 :Pastebin 当前文件就自动贴到 paste.ubuntu.com 上并且输出url了,如果是桌面版操作系统还会自动帮你打开页面。

Ref:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值