Development Python Applications in Vim. Part 2
Vim is an awesome piece of software. It is rather old, but still popular, and community keeps developing, so be sure, it stands the test of time.
It enables you to sculpt your own environment using any of the vast amount of available plugins. Plugins are usually quite configurable, and vim API is open for you to enhance your workflow even more.
On the other hand, Vim is just a tool, though handy and extensible. It will not make you better programmer or writer. There is an article on vim position in the world worthy of consideration - VIM: 8 Takeaways From One Year Of Typing.
Alternatives for Python development
The most notable IDE for professional Python development is PyCharm. This feature is really feature rich and supports a lot out of the box. The rest could be found in plugins repository.
There's also Sublime Text, which also has great support for python.
Vim is rather hard to tame, but once connection established, there is a plenty of information for both beginners and advanced users on the internet, here are some good resources:
- A vim Tutorial and Primer - good primer on movements and basic usage.
- VIM and Python - a Match Made in Heaven - points to some python plugins.
- Learn Vimscript the Hard Way - is a good place to familiarize yourself with vimscript
Plugin management systems
The power of Vim is in its plugin system. There are several ways to manage your plugins zoo.
Easy way is Vundle. You just list plugins in your vim config file and run
:PluginInstall. Advantage of this approach is that, you manage your Vim plugins from inside Vim. You store your plugins in config and change their state using Vim commands.
Other option is Pathogen. To install with it, you should
cd to your plugins directory and clone plugin repo with
All they do is copy certain files in certain places. You could as well copy and paste files by yourself. Vundle, Pathogen and others just make your life easier. For more discussion, see this reddit thread
This plugin provides enhanced highlighting feature compared to standard python.vim ones. To name a few (from plugin's github page):
- Added support for Python 3 syntax highlighting
- Added :Python2Syntax and :Python3Syntax commands which allow to switch between Python 2 and Python 3 syntaxes respectively without reloads/restarts
- Updated strings highlighting
- Enhanced special symbols highlighting inside strings
- Enhanced highlighting of numeric constants
Yapf (stands for "yet another python formatter") - is a tool for formatting python code. It "takes the code and reformats it to the best formatting that conforms to the style guide, even if the original code didn't violate the style guide" (from project README).
Several styles provided out of the box, including pep8, google and chromium styles. Also, you have a bunch of "knobs" to customize formatting.
In order to enable vim plugin, take a look at their yapf.vim plugin. You'll need to place this file in
~/.vim/autoload/ and add mappings to
~/.vimrc, so that you can call it with a few keystrokes.
As projects README states, yapf formats not only the code, that violates pep8, but tries to reformat code that does not look right according to some inner rules. In most cases, it does a good job, but there are some tasks, that are too difficult for yapf, for example formatting deeply nested structures. So do not format the whole file blindly and always check the result. Although code remains valid, your teammates may yell at you for bad formatting.
Also, there is alternative to yapf - tell-k/vim-autopep8. It binds to
<F8> by default and fixes pep8 violations in current file. It's less aggressive than yapf.
Syntastic is the framework to check the code against external validators and display errors. To enable syntax checking for python, you should install either
pylint python packages.
Among the features of this plugin there are the following:
- show errors in separate window with error text, code and line number;
- mark line with error in editor;
- add error balloons at the place where error occurs;
This is a great tool to check your code for both correctness and compliance with the pep8 styling.
Files/code navigation plugins
Fuzzy files/buffers search for vim. It adds
:CtrlP command, which, once issued enables fuzzy finder with additional options, like modes switching (files, buffers, mru), file creation and regex search.
There is alternative to ctrlp - command-t . It provides almost the same features, but requires ruby runtime and additional steps to install.
Also, there is great fzf which is system level fuzzy finder, but also provides vim plugin.
Nerdtree is useful when you need to familiarize yourself with the project. It adds directories tree as vim window, which enables handly files exploration, along with tree modifications facilities.
It is the matter of choice, which navigation style to use, but for me,
ctrlp is great when project is familiar, because you can find what you need with several keystrokes. But when you don't know the structure of the project, nerdtree is indispensable.
This tiny plugin adds contextual enabling vim relativenumber option, so you can use
13k to move 13 lines up or
28j to move 28 lines down, when you need navigation and see absolute line number when you need to see absolute numbers =)
Also, there are more plugins, that make developers’ life easier:
Missed the first part of this article? It’s here: Vim tutorial for beginners: Part 1