If you're a user of the text editor Vim, chances are you are already impressed with the number and power of its inbuilt features. If you've ever tried to add your own functionality to the editor but been turned off by its arcane Vimscript language, then you'll be pleased to know that Vim now supports internal Python scripting.
The following commands only work if your version of Vim has been compiled with the -python flag set, pre-built versions of Vim later than version 7.0 have this flag set by default, older versions may not. You can check if your Vim program has Python support by typing the following command:
:python print "hi"
If the status bar displays hi, then you're in business.
You can use the Python command whenever you need a little Python right away, but as you might have noticed from the previous example, its standard output goes to the status line, not to the document.
To do more complicated Python code, you'll want to start embedding your code into functions. There are a few different ways to do this, either you can embed a Python block in a Vimscript function or use a map or a macro to call a Python function. We'll show you the first.
We'll be writing a function that uses the urllib and BeautifulSoup libraries to download a Web page and insert it into the current window. If you're unfamiliar with using BeautifulSoup, you may want to look back over our guide to Web parsing in Python.
Open up a new text file, and call it pyextend.vim -- we'll be writing our functions in here, and then sourcing this file in our editor to interpret it. Once you're comfortable with your code, you could include the code in your .vimrc (or _vimrc, in Windows) file so that it is always interpreted whenever you start a Vim or gVim session.
Via (builderau.)
No comments:
Post a Comment