Configuring Vim to work with Python and Django

    Vim is an editor unique in its flexibility, which, if properly configured, can almost perfectly satisfy all the wishes of those working with it. True, this setting can last for months, or even years, which is both the advantage and the disadvantage of vim. There are many articles and tutorials on using Vim for development in Python and Django, I hope I can tell you something new. In my article, I will try to minimize the use of general-purpose plugins, such as NERDTree or surround.vim, and I will describe several more specialized extensions and settings that greatly simplified the process of working in Python.

    Configuring Vim under Django turned out to be rather difficult, unlike using it with the same Rails, for which rails.vimsolves 80% of all problems. But unfortunately Tim Pope did not write anything like this for python, so I had to collect everything in parts. In short, what will be discussed:
    • python-mode
    • tagbar
    • pydiction
    • neocomplcache
    Everything described below has been tested in gVim for Linux.

    Python mode

    Just a low bow to Kirill Klenov, the developer of this extension. It installs well with vundle, and through pathogen and works just as well.

    Python-mode sets its settings 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 to .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 code folding, I also do not need a pylint check every time I save the file (let g: pymode_lint_write = 0), instead you can use the command: PyLint by hanging some kind of hotkey on it. We will return to auto-completion later.

    The plugin provides several convenient hotkeys for moving around objects in the python code:
    Keyboard shortcutCommand
    KShow documentation
    gGo to definition
    \ rRun code
    [[Go to previous class or function
    ]]Go to the next class or function
    aC CPerform an action for the class. (vaC, daC, dC, yaC, yC, caC, cC)
    iCThe same, being inside the class. (viC, diC, yiC, ciC)
    aM MPerform an action for a function. (vaM, daM, dM, yaM, yM, caM, cM)
    iMThe same, being inside the function. (viM, diM, yiM, ciM)
    Separately, I would like to note the ability to use Rope, in particular the commands: RopeGoToDefinition,: RopeFindOccurrences and other features for searching for code and refactoring. To view the full list of Rope commands and their abbreviations, use: help RopeKeys. In addition, virtualenv support is enabled by default, which means that using the same: RopeGoToDefinition, you can safely go to the definition of the function of any library installed in the site-packages of your virtualenv.

    Code view

    Although the most popular plugin for this is Taglist, I like Tagbar, also based on ctags (which must be installed before using the plugin). We hang: TagbarToggle on some hotkey, for example:
    nnoremap  :TagbarToggle
    It looks something like this:

    A couple of additional 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 configure on which side the tagbar window will appear, what indents, icons, etc. will be. All of this can be found in: help tagbar.

    Auto completion

    One of the plugins I could recommend is Pydiction, which complements keywords based on a large dictionary file. This plugin has a lot of advantages:
    • Auto completion is performed by the Tab key, no need to remember all sorts of combinations with Ctrl
    • absolutely no conflicts, you can use Pydiction with omnicomplete at the same time
    • autocompletion does not affect other types of files
    • able to complement imported objects
    • quite easily you can add your own modules to the dictionary: python module_name
    By and large, there is only one minus, 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 Python functions 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#Complete

    Neocomplcache does a little worse with the addition of modules, but it supplements not only keywords from the current files, but also just strings. In addition, there is an option automatically popup add-on, which is enabled using the settings:
    let g:neocomplcache_enable_at_startup = 1

    Django Templates

    Vim supports the syntax of django templates and highlights them if the file type is htmldjango. Unfortunately, when you open Vim, it automatically detects this type only if there is a django tag at the beginning of the file. This can be solved, for example, using the following function:

    fun! DetectTemplate()
      let n = 1
      while n < line("$")
        if getline(n) =~ '{%' || getline(n) =~ '{{'
          set ft=htmldjango
        let n = n + 1
      set ft=html "default html

    which you need to run when opening a file with the extension .html:
    autocmd BufNewFile,BufRead *.html call DetectTemplate()

    If you use several template engines, it is not difficult to modify the function for them, though you should make the conditions for determining the type of template engine more stringent.

    Random settings

    • Python indents:
      autocmd FileType python setlocal ts=4 sts=4 sw=4

    • If you use NERDTree, you should add .pyc to the list of ignored files:
      let NERDTreeIgnore=['\.pyc$']

    • simple shorthand for inserting ipdb debugger into code
      iab ipdb import ipdb; ipdb.set_trace()

    • shortcut for specifying the encoding at the beginning of the file:
      iab utf! # -*- coding: utf-8 -*-

    • remove annoying popups with documentation for omnicompletion:
      set completeopt-=preview

    • pair of color schemes: darkspectrum , gruvbox , Lucius


    1. python-mode
    2. Tagbar
    3. Pydiction
    4. Neocomplcache

    Also popular now: