
Vim is unique in its flexibility editor, which, with proper configuration, can almost perfectly satisfy all the wishes of working with him. True, this adjustment can last for months, or even years, which is both an advantage and a disadvantage of vim. There are many articles and tutorials on how to use Vim for development in Python and Django, I hope I can tell you something new. In my article, I will try to minimally mention general purpose plugins, like NERDTree or surround.vim, and describe several more specialized extensions and settings that have significantly simplified the process of working in Python.
Configuring Vim for Django turned out to be quite difficult, unlike using it with the same Rails, for which
rails.vim solves 80% of all problems. But unfortunately Tim Pope didn’t write anything like that for a python, so I had to put everything in pieces. In short, what will be discussed:
- python-mode
- tagbar
- pydiction
- neocomplcache
Everything written below was tested in gVim for Linux.
Python-mode
Just a low bow to Cyril Klyonov, the developer of this extension. It is perfectly installed using vundle, and through pathogen and works just as well.
')
Python-mode sets its preferences for python files. If this does not suit you (for example, I do not need the set number that he adds), add the necessary settings in .vimrc. Here is an example of my settings:
let g:pymode_options = 0 let g:pymode_lint_write = 0 “ let g:pymode_folding = 0 “ - let g:pymode_rope_vim_completion = 0 “ rope
As you can see, I do not use automatic folding of the code, I also do not need pylint checking every time I save a file (let g: pymode_lint_write = 0), instead of which you can use the command: PyLint by hanging some hot key on it. Back to auto completion.
The plugin provides several convenient hot keys for navigating objects in the python code:
Keyboard shortcut | Team |
---|
K | Show documentation |
<Cc> g | Go to definition |
\ r | Run the code |
[[ | Go to previous class or function |
]] | Go to the next class or function |
aC C | Perform an action for the class. (vaC, daC, dC, yaC, yC, caC, cC) |
iC | The same being inside the class. (viC, diC, yiC, ciC) |
aM M | Perform an action for a function. (vaM, daM, dM, yaM, yM, caM, cM) |
iM | The same being inside the function. (viM, diM, yiM, ciM) |
Separately, I would like to mention the possibility to use Rope, in particular, the commands: RopeGoToDefinition,: RopeFindOccurrences and other features for code search and refactoring. You can view a full list of Rope commands and abbreviations for them using: help RopeKeys. In addition, virtualenv support is enabled by default, which means that using the same: RopeGoToDefinition, you can easily move on to defining the function of any library installed in your virtualenv site-packages.
View code
Although the most popular plugin for this is Taglist, I like Tagbar more, also based on ctags (which need to be installed before using the plugin). We hang: TagbarToggle on some hotkey, for example:
nnoremap <F8> :TagbarToggle<CR>
It looks like this:

A couple of advanced settings:
let g:tagbar_autofocus = 1 let g:tagbar_sort = 0 "tagbar shows tags in order of they created in file let g:tagbar_foldlevel = 0 "close tagbar folds by default
In addition, you can customize from which side the tagbar window will appear, what will be the indents, icons, etc. All this can be found through: help tagbar.
Autocompletion
One of the plugins that I could recommend is Pydiction, which complements keywords based on a large dictionary file. This plugin has a lot of advantages:
- autocompletion takes place on the Tab key; no need to memorize any combinations with Ctrl
- absolutely no conflicts, you can use Pydiction with omnicomplete at the same time
- autocompletion does not affect other file types
- can add imported objects
- it's pretty easy to add your own modules to the dictionary: python pydiction.py module_name
Minus by and large only one, but very significant in my opinion - Pydiction does not pick up keywords from the current file, and this is often much more necessary than the functions of Python that you already know.
Another way is to use neocomplcache. In addition to installing the plugin, you need to set omnifunc for Python files:
autocmd FileType python setlocal omnifunc=pythoncomplete
Neocomplcache copes with the addition of modules a little worse, but it complements not only keywords from current files, but simply strings. In addition, there is an option automatically pop-up add-on, which is enabled by setting the settings:
let g:neocomplcache_enable_at_startup = 1
Django templates
Vim supports the jango template syntax and highlights them if you specify the file type htmldjango. Unfortunately, when opening Vim, it automatically detects this type only if there is a jango tag at the beginning of the file. You can solve this for example using this function:
fun! DetectTemplate() let n = 1 while n < line("$") if getline(n) =~ '{%' || getline(n) =~ '{{' set ft=htmldjango return endif let n = n + 1 endwhile set ft=html "default html endfun
which should be run when opening a file with .html extension:
autocmd BufNewFile,BufRead *.html call DetectTemplate()
If you use several template engines, it is easy to modify the function for them; however, you should make the template type definition conditions more stringent.
Random settings
- Python indents:
autocmd FileType python setlocal ts=4 sts=4 sw=4
- If you are using NERDTree, add .pyc to the list of ignored files:
let NERDTreeIgnore=['\.pyc$']
- a simple shortcut to insert debugger ipdb into the code
iab ipdb import ipdb; ipdb.set_trace()
- shorthand for specifying the encoding at the beginning of the file
iab utf!
- remove annoying omnicompletion popup windows:
set completeopt-=preview
- a couple of color schemes: darkspectrum , gruvbox , Lucius
Additional links
1. python-mode
github.com/klen/python-mode2. Tagbar
github.com/majutsushi/tagbar3. Pydiction
github.com/rkulla/pydiction4. Neocomplcache
github.com/Shougo/neocomplcache