How to Make Vim Plugin with Python

Before discussing the steps or procedure of creating Vim plugin with python firstly we have to know that what is a vim plugin?


What's a Vim Plugin

Basically, it is a .vim script that defines various components such as syntax rules, commands, functions, mappings etc. Usually, a plugin consists of several functions mappings command definitions and event hooks. It is a complete piece of code with some specific functionality.

Everything outside the functions is written in VimL while writing vim plugin. In fact, VimL can be learned fast, but using python gives so much adaptability. This is the reason the greater part of the modules that work with web administrations are typically done in VimL+Python.


You should have vim compiled with +python support. You can check that by using the below command:

vim --version | grep +python

Vim package in Ubuntu and it's derivatives comes with +python support.

To Work - Vimmit.vim

Here I am going to start with a simple example. This is a plugin that will retrieve the homepage of Facebook and will show it in the current buffer.
As we are writing python code, it is good to check either Vim supports Python or not:
You can start by opening "vimmit.vim" file in vim.

if !has('python')
echo "Error: Required vim compiled with +python"

The above-written script will check if Vim has python support or it will end the script with an error message.

We proceed with the main function Facebook().
This is where we use Python and do the main functionality:

Vim comments start with a double quote.

" Function definition is VimL. We can mix VimL and Python in  
" function definition.  
function! Facebook()  
" We can start the python code like the next line.  
python << EOF  
# the vim module contains everything you need to interface with vim from  
# python. You need urllib2 for the web service consumer.  
import vim, urllib2  
# you 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 = ""  
# 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.  
except Exception, e:  
print e  
" Here the python code is closed. We can continue writing VimL or python again.  

Save the File

You can save the source file in vim (:source vimmit.vim) and call it by using
:call Facebook()
Now, the way you call the function is not so good. So we define a command:
command! -nargs=0 Facebook call Facebook()

We define the command :Facebook to call the function. After adding this, open a new buffer and do :Facebook . Home page will be loaded into the buffer.
The -nargs argument states how many arguments the command will take.

Guys, I wrote this code after lots of researches hope it will help you!

Related article



Boostlog is an online community for developers
who want to share ideas and grow each other.

Delete an article

Deleted articles are gone forever. Are you sure?