Date: | 2017-01-04 14:30 |
---|---|
tags: | topic:vim |
category: | Code |
summary: | My current Vim setup for Python development. |
scm_path: | content/1701-vim-python.rst |
Below is a list of my Vim plug-ins and configurations.
My goal has been to make Vim more useful for (primarily) Python development.
This post refers to Vim 7, because I have not yet updated to Vim 8. I'm using
vim-plug to manage my packages, so
mentions of packages below will use the Plug
command.
All the commands and configuration below come from my vimrc file. You'll find that I don't have a large number of plug-ins or configuration lines compared to other more famous Vim users (cough) Drew (cough). That is a direct result of Kris Jenkins's Bare Bones Navigation Vim talk at Vim London. The main result of which has been that I have always run a very simple Vim setup.
My relatively recent use of FZF and Ctags listed below are a direct result of attending the most recent Vim London meetup and, if you're in the London area, I fully recommend joining and attending. Every meetup I attend, my Vim-fu improves.
The following are my .vimrc
lines for handling Python.
When searching for files with Vim, only load Python files:
set suffixesadd=.py
Ignore pyc
files when expanding wildcards:
set wildignore=*.pyc
Don't show pyc
in file lists:
let g:netrw_list_hide= '.*\.pyc$'
Keep "Pythonic" tabs using 4 white spaces:
set autoindent nosmartindent " auto/smart indent
set smarttab
set expandtab " expand tabs to spaces
set shiftwidth=4
set softtabstop=4
I really get frustrated with tabs that look like white spaces, so I ensure
they are visible by telling Vim to show all tabs as little arrows ▷
. This
line also ensures that end of lines are shown with a negation sign ¬
:
set listchars=eol:¬,tab:▷\ ,
A classic "Python tell" in Vim is the 79th or 80th character highlight:
set colorcolumn=80 " Show the 80th char column.
highlight ColorColumn ctermbg=5
My greatest recent revelation has been the integration of FZF to provide "quick" fuzzy searching. Most frequently I search for files in the current git repository, open buffers and tags.
Install FZF and get it working on your machine, then add it to your Vim setup using fzf.vim:
Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --all' }
Plug 'junegunn/fzf.vim'
I've mapped my most common FZF searches to leader commands:
imap <c-x><c-o> <plug>(fzf-complete-line)
map <leader>b :Buffers<cr>
map <leader>f :Files<cr>
map <leader>g :GFiles<cr>
map <leader>t :Tags<cr>
Keeping FZF's line completion on CTRL-x CTRL-o
means that I can keep
access to Vim's line completion which is bound to CTRL-x CTRL-l
by
default.
Ag results integration
with FZF is next on my list, I'm still using Ag
results on the command
line.
I was definitely slow to get on the Ctags bandwagon, only adding them to my workflow in the last couple of months, but along with FZF, they have been a revelation. I've been using Exhuberant Ctags as my index generator.
TPope has published a neat trick of stashing the ctags
script inside the
.git
folder, outlined in his blog post here. My version
of the script is inside my git hooks configuration
and works in combination with my ctags config.
As mentioned above, I have used <leader>t
to trigger an FZF-powered search
of tags:
map <leader>t :Tags<cr>
The default "jump to definition under cursor" is still the default CTRL-]
which, with "previous tag" CTRL-t
makes it really easy to traverse code.
The smartpairs plugin is fantastic for selecting text inside brackets, braces and parentheses and is excellent for all languages I work with, not just Python:
Plug 'gorkunov/smartpairs.vim'
In general, I've used external programs to provide linting of my Python code and so I run Vim with the current project's virtualenv active.
With Isort installed in the current
environment, sort the imports of the current file with <leader>i
or call it
with :Isort
command on a range of lines:
map <leader>i :Isort<cr>
command! -range=% Isort :<line1>,<line2>! isort -
With flake8 installed in the current
environment, lint the current file with F7
as provided by Vincent
Driessen's vim-flake8:
Plug 'nvie/vim-flake8'
Happy Vimming!
:xa