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 the most plugin manager 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

" Thomas Jansson 2016
 
"""""""""""""""""""""""""""""""""""
" VIM PLUG SETUP and some downloads
" http://vimawesome.com/
"""""""""""""""""""""""""""""""""""
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()
 
""""""""""""""""""""""""
" Setup plugins settings
""""""""""""""""""""""""
syntax enable
syntax on
let python_highlight_all=1
 
let g:rehash256 = 1
let g:solarized_termcolors=256
set t_Co=256
set background=dark
colorscheme tender
 
" Snippets
let g:UltiSnipsExpandTrigger="<tab>"
let g:UltiSnipsJumpForwardTrigger="<c-b>"
let g:UltiSnipsJumpBackwardTrigger="<c-z>"
 
noremap <C-x> :CtrlP ~/QI<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>
 
set tabpagemax=50
map <F8> :tabp<cr>
map <F9> :tabn<cr>
 
" NERDtree
map <C-n> :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" }
 
""""""""""""""""""""
" My hacks
""""""""""""""""""""
" Remove trailing spaces
nnoremap <F4> :let _s=@/<Bar>:%s/\s\+$//e<Bar>:let @/=_s<Bar>:nohl<CR>
 
" Save and run the current script
nmap <F5> :w<CR> :! ./%<CR>
 
highlight Search term=standout ctermfg=3 cterm=standout
highlight Visual term=standout ctermfg=4 cterm=standout
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
 
command! Q  quit
command! W  write
command! Wq wq
 
""""""""""""""""""
"" 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" ]
let g:myLangList = [ "nospell", "en_us" ]
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
" http://vim.wikia.com/wiki/VimTip80
""""""""""""""""""""""""""""""""""""""""""""""""""
set viminfo='10,\"100,:20,%,n~/.viminfo
" when we reload, tell vim to restore the cursor to the saved position
augroup JumpCursorOnEdit
 au!
 autocmd BufReadPost *
 \ if expand("<afile>:p:h") !=? $TEMP |
 \ if line("'\"") > 1 && line("'\"") <= line("$") |
 \ let JumpCursorOnEdit_foo = line("'\"") |
 \ let b:doopenfold = 1 |
 \ if (foldlevel(JumpCursorOnEdit_foo) > foldlevel(JumpCursorOnEdit_foo - 1)) |
 \ let JumpCursorOnEdit_foo = JumpCursorOnEdit_foo - 1 |
 \ let b:doopenfold = 2 |
 \ endif |
 \ exe JumpCursorOnEdit_foo |
 \ endif |
 \ endif
 " Need to postpone using "zv" until after reading the modelines.
 autocmd BufWinEnter *
 \ if exists("b:doopenfold") |
 \ exe "normal zv" |
 \ if(b:doopenfold > 1) |
 \ exe "+".1 |
 \ endif |
 \ unlet b:doopenfold |
 \ endif
This entry was posted in Articles, Computer. Bookmark the permalink.

2 Responses to My Vim setup for Python programming

  1. Stine Kildegaard Rose says:

    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