Python editor writes Vim script example

Source: Internet
Author: User
Tags comments eval function definition in python


The Vim plug-in is a. Vim script file that defines functions, mappings, syntax rules, and commands that you can use to manipulate windows, buffers, and rows. A general plug-in contains the command definition and event hooks. When you write a vim plug-in using Python, the function is written using VIML, although VIML is quick to learn, Python is more flexible, for example, you can use Urllib/httplib/simplejson to access certain WEB services, which is why a lot of the need to visit The plug-ins that ask Web services are written using VIML + Python.

Before you start writing plug-ins, you need to make sure that Vim supports Python by identifying the following commands:

Vim--version | grep +python
Let's go through a simple example to write a Vim plug-in in Python, which is used to get the Reddit home page information and display it on the current buffer.

First of all, in Vim, create a new Vimmit.vim file, we first need to determine whether to support Python, if you do not support the presentation message:

If!has (' Python ')
echo "error:required vim compiled with +python"
Finish
endif
The above code is written in Viml, and it checks to see if Vim supports Python.

Here is the Reddit () main function written in Python:

"Vim comments start with a double quote."
The 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 the API call. We don ' t want
# users to wait so much.
TIMEOUT = 20
URL = "Http://reddit.com/.json"

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 a item in the list. We can loop through them delete
# them, alter them etc.
# Here we are 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:
        # into the next few lines, we get th E post details
        post_data = post.get ("Data", {})
   & nbsp;    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 are append line through to the buffer.
# 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's the Python code is closed." We can continue writing viml or Python again.
Endfunction
Save the file with the following command

: Source Vimmit.vim

Then call the plug-in:

: Call Reddit ()

This command is not easy to use, so we define a command:

command! -nargs=0 Reddit call Reddit ()

We defined the command: Reddit to call this function. The-nargs parameter declares how many parameters are in the command line.

Questions about function Parameters:

Q: How do I access parameters in a function?

function! Somename (Arg1, arg2, Arg3)
"Get the" the ' 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")
You can use ... To handle a variable number of parameters to replace a specific parameter name, which can be accessed by positional or named arguments, such as: (Arg1, arg2, ...)

Q: How do I invoke the Vim command in Python? Vim.command ("[Vim-command-here]")

Q: How do I define global variables and access them in VIML and Python?

Global variables use a shape like g:. prefix, you should check whether the variable is defined before defining a global variable:

If!exists ("G:reddit_apicall_timeout")
Let g:reddit_apicall_timeout=40
endif
You then access the variable in Python using the following code:

TIMEOUT = Vim.eval ("G:reddit_apicall_timeout")
You can reassign a global variable in the following ways:

Let g:reddit_apicall_timeout=60
For more information on using Python to write Vim Plug-ins, see the official documentation.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.