I recently began scripting in Python using the VIM editor; my editor of choice. In what became a failing effort to keep my sanity I forewent customization of the VIM settings on my personal machines. You see, I’m often tasked with editing files on servers whose VIM settings I can’t customize. I feared that if I were to become overly accustomed to any custom settings then I’d likely blurt obscenities when forced to use a vanilla VIM.
Without some tweaking of my vimrc I end up having to manually indent code in Python. Talk about a loss of productivity, having to use the space bar to indent Python code is the surest path to insanity. Mimicking the mindless repetition that’s better suited to steam powered machinery is a less than efficient use of my time. I’ve since submitted defeat and tailored my VIM settings to Python. I may occasionally blurt an obscenity when using VIM on somebody else’s machine but it’s a calculated loss. Below is a breakdown of my VIM settings. I hope others will find it useful.
Veirify you have the latest version of VIM 7 that’s available through your pkg system.
RedHat based distros: rpm -qa | grep vim
Debian based: dpkg --get-selections | grep vim
FreeBSD: pkg_info | grep vim
Verify VIM plugins are installed (installed by default in most pkgs).
Inside VIM type :echo $VIM and verify the VIM plugins exist under that path. On my machine that’s /usr/share/vim. So..
$ find /usr/share/vim -name \*python\* /usr/share/vim/vim70/syntax/python.vim /usr/share/vim/vim70/ftplugin/python.vim /usr/share/vim/vim70/indent/python.vim /usr/share/vim/vim70/autoload/pythoncomplete.vim
Verify VIM is compiled with auto-completion and Python support.
$ vim --version | grep -Eo '\+python|\+autocmd' +autocmd +python
If you cannot find the plugins or VIM was not compiled with both Python and autocmd you may wish to find a more recent package for your distro (or compile VIM with these options manually *ugh*).
Enable plugins and configure indenting to follow PEP8 by adding the following to your ~/.vimrc file:
" Set syntax on syntax on " Higlhight search set hls " enter spaces when tab is pressed: set expandtab " break lines when line lenght > 79 (PEP8) set textwidth=79 " user 4 spaces to represent a tab set tabstop=8 set softtabstop=4 " number of space to use for auto indent " you can use >> or << keys to indent current line or selection " in normal mode. set shiftwidth=4 " Copy indent from current line when starting a new line. set autoindent " makes backspace key more powerful. set backspace=indent,eol,start " shows the match while typing set incsearch " case insensitive search set ignorecase " show line and column number set ruler "set columns=80 " show some autocomplete options in status bar set wildmenu set smarttab " set filetype on filetype on filetype indent on filetype plugin on
Now VIM should load the plugins for Python when opening a python file. You can verify this several ways, the easiest being to run vim -V2 file.py and verify the Python plugins are sourced and loaded. Alternatively within VIM type :filetype and :set ft?.
At this point you should have intelligent code indentation, syntax highlighting, a reminder not to exceed 80 in width, and an ounce of sanity.
You may want to look into code and file completion in VIM. A good introduction to the types of completion available exists here. That’s the best breakdown of completion I’ve found, but if you have others please let me know in the comments.
Once the thrill of efficient Python code completion wears off, however unlikely, you may want to look into configuring additional completion plugins.
Supertab – Allows the use of the tab key for completion.
autocomplpop.vim – displays completion suggestions automatically