My Vim setup for Python programming

I have been using Vim now for many years, but usually with fairly vanilla setup as I did not want to depend on fancy features if had to work remotely on older machines. Those days are now gone and I can focus my hacking on somewhat recent Linux installations and using the latest Vim versions.

vimscreenshotThis screenshot shows the output of the vim-plug command :PlugUpdate and the 24 bit color scheme tender.

Vim-plug plugin manager

On a day-to-day basis I am editing Python 98% of my time, with some bash and LaTeX in the last 2%, so the plugins I have listed below are biased towards python. A major development in my Vim usage was the vim-plug plugin manager and the corresponding plugins. My full .vimrc is listed below, but can also be found on my github page: github.com/tjansson60/dotfiles. The most important change is the the plugin manager which needs a little more explanation.

This short snippet below is pure magic. On a complete blank Linux shell without a .vimrc or .vim folder this will download the vim-plug plugin manager and afterwards install all my plugins. All I have to do to set up my Vim environment is to download my .vimrc below and start vim. That’s it.

if empty(glob('~/.vim/autoload/plug.vim'))
  silent !curl -fLo ~/.vim/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
  autocmd VimEnter * PlugInstall | source $MYVIMRC
endif
call plug#begin('~/.vim/plugged')
Plug 'tpope/vim-sensible'
Plug 'scrooloose/syntastic'
Plug 'nvie/vim-flake8'
Plug 'scrooloose/nerdtree', { 'on': 'NERDTreeToggle' }
Plug 'ctrlpvim/ctrlp.vim'
Plug 'godlygeek/tabular'
Plug 'jacoborus/tender'
Plug 'ervandew/supertab'
Plug 'sirver/ultisnips'
Plug 'honza/vim-snippets'
call plug#end()

On the site github.com/junegunn/vim-plug the usage of the plugin manager vim-plug is describe. This most important command is in my opinion :PlugUpdate which updates all the plugins (git pull). The screenshot above illustrates the output of the PlugUpdate command.

The plugins I am using

The most popular Vim scripts and plugins and associated vim-plug line are listed on vimawesome.com. Below are short descriptions of the plugins I am currently using.

vim-sensible

Think of sensible.vim as one step above ‘nocompatible’ mode: a universal set of defaults that (hopefully) everyone can agree on.

syntastic

Syntastic is a syntax checking plugin for Vim that runs files through external syntax checkers and displays any resulting errors to the user. This can be done on demand, or automatically as files are saved.

vim-flake8

vim-flake8 is a Vim plugin that runs the currently open file through Flake8, a static syntax and style checker for Python source code.

nerdtree

The NERD tree allows you to explore your filesystem and to open files and directories. It presents the filesystem to you in the form of a tree which you manipulate with the keyboard and/or mouse.

ctrlp.vim

Full path fuzzy file, buffer, mru, tag, … finder for Vim.

tabular

Sometimes, it’s useful to line up text. Naturally, it’s nicer to have the computer do this for you, since aligning things by hand quickly becomes unpleasant.

tender

A 24bit colorscheme for Vim, Airline and Lightline

supertab

Supertab is a vim plugin which allows you to use for all your insert completion needs

ultisnips

UltiSnips is the ultimate solution for snippets in Vim. It has tons of features and is very fast.

vim-snippets

This repository contains snippets files for various programming languages. It is community-maintained and many people have contributed snippet files and other improvements already.

My .vimrc

The most updated version of my .vimrc can be found on my github page: github.com/tjansson60/dotfiles, but a snapshot from 3-feb-2018 can be found here:

" Thomas Jansson 2018
 
" VIM PLUG SETUP and some downloads, see http://vimawesome.com/
" Consider installing the following: 
"   sudo apt install curl vim exuberant-ctags git ack-grep pep8 flake8 pyflakes isort
"   sudo pip install pep8 flake8 pyflakes isort yapf build-essential cmake
"
if empty(glob('~/.vim/autoload/plug.vim'))
  silent !curl -fLo ~/.vim/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
  autocmd VimEnter * PlugInstall | source $MYVIMRC
endif
call plug#begin('~/.vim/plugged')
Plug 'tpope/vim-sensible'
Plug 'scrooloose/syntastic'
Plug 'nvie/vim-flake8'
Plug 'scrooloose/nerdtree', { 'on': 'NERDTreeToggle' }
Plug 'ctrlpvim/ctrlp.vim'
Plug 'godlygeek/tabular'
Plug 'ervandew/supertab'
Plug 'sirver/ultisnips'
Plug 'honza/vim-snippets'
Plug 'bling/vim-airline'
Plug 'airblade/vim-gitgutter'
Plug 'tpope/vim-fugitive'
Plug 'jacoborus/tender' " Color
Plug 'flazz/vim-colorschemes' " http://vimcolors.com/?utf8=%E2%9C%93&bg=dark&colors=term&order=newest&page=3
"Plug 'davidhalter/jedi-vim'
"Plug 'sirver/ultisnips'
" Read https://github.com/honza/vim-snippets/blob/master/UltiSnips/tex.snippets
" Read https://github.com/honza/vim-snippets/blob/master/UltiSnips/python.snippets
call plug#end()
 
" Colors
let g:rehash256 = 1
let g:solarized_termcolors=256
set t_Co=256
set background=dark
colorscheme tender
set t_BE= "Avoid 0~ and 1~ when copy pasting
 
" Setup plugins settings
setlocal foldmethod=manual
let g:airline#extensions#tabline#enabled = 1
let g:pymode_rope = 0
syntax enable
syntax on
let python_highlight_all=1
 
"JEDI -  Remember to have a working PYTHONPATH
"let g:jedi#use_tabs_not_buffers = 1
 
" Snippets
let g:UltiSnipsExpandTrigger="<tab>"
let g:UltiSnipsJumpForwardTrigger="<c-b>"
let g:UltiSnipsJumpBackwardTrigger="<c-z>"
let g:ultisnips_python_style=0x3
 
noremap <C-l> :CtrlP ../<CR>
noremap <C-x> :CtrlP ~/code<CR>
let g:ctrlp_prompt_mappings = {
    \ 'AcceptSelection("e")': ['<c-t>'],
    \ 'AcceptSelection("t")': ['<cr>', '<2-LeftMouse>'],
    \ }
 
"Only tabularize the first = 
map <S-F5> :Tabularize /^[^=]*\zs=<cr>
"Align all the , in alist of dicts or tupples 
map <S-F6> :Tabularize /,\zs<cr>       
"Tabularize elements of a dict 
map <S-F7> :Tabularize /:\zs<cr>       
"Tabularize elements of a CSV 
map <S-F8> :Tabularize /;\zs<cr>       
 
set tabpagemax=50   
" Move between tabs
map <F8> :tabp<cr> 
map <F9> :tabn<cr>
 
" NERDtree
map <F3> :NERDTreeToggle<CR>
let g:NERDTreeIgnore = ['\.pyc$']
let g:NERDTreeDirArrowExpandable = '+'
let g:NERDTreeDirArrowCollapsible = '-'
let g:python_version_2 = 1
 
" Syntastic
let g:syntastic_python_checkers = ['pyflakes']
let g:syntastic_always_populate_loc_list = 1
let g:syntastic_auto_loc_list = 0
let g:syntastic_check_on_open = 1
let g:syntastic_check_on_wq = 0
let g:syntastic_quiet_messages = { "type": "style" }
 
" Remove trailing whitespaces from all lines 
nnoremap <F4> :let _s=@/<Bar>:%s/\s\+$//e<Bar>:let @/=_s<Bar>:nohl<CR> 
nmap <F5> :w<CR> :! ./%<CR>
command! Q  quit
command! W  write
command! Wq wq
 
highlight Search term=standout ctermfg=3 cterm=standout
highlight Visual term=standout ctermfg=4 cterm=standout
if v:version > 74338
    set breakindent
endif
set hlsearch
set backspace+=start,eol,indent
set backupdir=./.backup,.,/tmp
set directory=.,./.backup,/tmp
set expandtab
set ignorecase
set noerrorbells
set novisualbell
set nowrap
set shiftwidth=4
set showmatch
set smartcase
set smarttab
set softtabstop=4
set tabstop=4
set textwidth=0
set virtualedit=all
set wildignore=*.swp,*.bak,*.pyc,*.class
set ttyfast
set textwidth=120 " Not quite PEP8, but more readable on modern machines.
set colorcolumn=120
 
"" Filetypes
filetype on
filetype plugin on
filetype indent on
filetype plugin indent on
autocmd BufRead,BufNewFile *.tex set spell
autocmd BufRead,BufNewFile *.tex setlocal spell spelllang=en_us
 
" Spell checker
highlight clear SpellBad
highlight SpellBad term=standout ctermfg=1 term=underline cterm=underline
highlight clear SpellCap
highlight SpellCap term=underline ctermfg=1 cterm=underline
highlight clear SpellRare
highlight SpellRare term=underline ctermfg=1 cterm=underline
highlight clear SpellLocal
highlight SpellLocal term=underline ctermfg=1 cterm=underline
 
"switch spellcheck languages
let g:myLang = 0
let g:myLangList = [ "nospell", "en_us", "da" ]
function! MySpellLang()  "loop through languages
  let g:myLang = g:myLang + 1
  if g:myLang >= len(g:myLangList) | let g:myLang = 0 | endif
  if g:myLang == 0 | set nospell | endif
  if g:myLang == 1 | setlocal spell spelllang=en_us | endif
  if g:myLang == 2 | setlocal spell spelllang=da | endif
  echo "language:" g:myLangList[g:myLang]
endf
map <F7> :call MySpellLang()<CR>
imap <F7> <C-o>:call MySpellLang()<CR>
 
" Tell vim to remember certain things when we exit, see http://vim.wikia.com/wiki/VimTip80
set viminfo='20,\"300,:20,%,n~/.viminfo
 
" when we reload, tell vim to restore the cursor to the saved position
if has("autocmd")
  au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
endif

2 thoughts on “My Vim setup for Python programming”

  1. It is great Thomas, thanks for sharing! I had some trouble with the UltiSnips plugin. It turns out that Ubuntu does not install vim with python bindings per default!

Leave a Reply