📜 ⬆️ ⬇️

Configure Vim to work with Python and Django



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:
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 shortcutTeam
KShow documentation
<Cc> gGo to definition
\ rRun the 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 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:
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#Complete 

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



Additional links

1. python-mode
github.com/klen/python-mode
2. Tagbar
github.com/majutsushi/tagbar
3. Pydiction
github.com/rkulla/pydiction
4. Neocomplcache
github.com/Shougo/neocomplcache

Source: https://habr.com/ru/post/173473/


All Articles