blog

Photo of python by David Clode on Unsplash

Vim for Python Development

by | Jun 26, 2013 | Developer Blog | 0 comments

A raring, tearing, slick, awesome, fast software development editor. Here’s my vimrc.

I’m sick of Sublime Text 2. It’s probably my fault. I wish it behaved like vim, which it nearly does but actually doesn’t in enough ways that I find myself glowering at it when I open it in the mornings and that’s not the kind of relationship I want to have with the thing I stare at for hours a day. No sir.

youcompleteme

But you know what does behave quite a lot like vim? vim. Obvious when you think about it really. But! Sublime Text 2 has a lot of features that are actually really nice, let’s make sure we don’t lose any of those in our search for perfection.

What I want:

  • Syntax highlighting (almost too obvious to be mentioned).
  • Code navigation: searching, go to definition, go back.
  • Project navigation: easy to find and open files within your project.
  • Mouse navigation: this is much faster when following code around, clicking, scrolling, into and out of definitions.
  • Auto complete.
  • Linter: to tell us when we’ve made a syntax error etc.

Briefly:

  1. Syntax highlighting is built in and easy to turn on.
  2. Code Navigation: searching is built in, go to definition and back are built in as long as there are tag files.
  3. Project Navigation: I really really like the ‘Command-T’ plugin, it allows you to quickly and easily open any file whose name you know. It also allows you to navigate between buffers the same way.
  4. Auto Complete: The ‘You Complete Me’ plugin is the best I’ve tried, it’s really fast and excellent.
  5. Linter: Syntastic will do everything we want.

In the following section I’ll discuss each bit in slightly more detail including various steps needed to install the plugins or configure the behavior and some brief info on how I use them.

It should be noted that if you’re following along at home ‘You Complete Me’ requires installing a newer version of vim than comes with Ubuntu by default at the moment so you may want to start with that.

Also getting a bit meta here I like Vundle to install the plugins, it’s what ‘You Complete Me’ suggests and has caused me no trouble at all, you just add the git repo of the plugins to vimrc and BundleInstall and BundleUpdate at the appropriate times.

Syntax Highlighting

We almost don’t need to do anything for this, in the .vimrc: syntax on

Code Navigation

Search across files: This wiki tip is helpful, which I have set up like:

nnoremap <C-f> :execute "vimgrep /" . expand("<cword>") . "/j **" <Bar> cw<CR>
nnoremap <Leader>k :cn<CR>
nnoremap <Leader>j :cp<CR>

because I don’t use <C-f> for page down ever, I use <C-u> and <C-d> which does about half a page at a time. So I can have the cursor on a word, hit <C-f> and it’ll search the project for me, and open the ‘quickfix’ list with the results, then I can use <Leader>j and <Leader>k to go to next / previous hit.

Mouse Navigation

Mouse navigation allows you to scroll and click where you want the cursor to be. Click drag will highlight things in visual mode, so if you want to right-click -> copy you need to remember to shift+click drag. Turn it on like so:set mouse=a

Tags

This post describes a good way to set up tags. Tags allow you to ‘go to definition’ and we need to make sure we’re regenerating the tag files while we’re editing which I’m doing like this:autocmd BufWritePost :!ctags-proj.sh where ctags-proj.sh is the script from the link. You also need this line in your .vimrc:set tags=./tags;~ as the link explains. Now you can navigate to definition with <C-]> and back again with <C-t> or navigate to definition with <C-left click> and back again with <C-right click>

Project Navigation

Command-T, this is almost solving a problem you didn’t know you had. There absolutely are hierarchical tree plugins you can use that allow you to browse your project and open files, and when it comes right down to it it’s not like :e whatever/file.py or :!ls whatever/ are painfully onerous. But (but!) command-T is so nice to use that for me it replaces thinking about where my buffers are, before I would have a mental model of file x is 2 buffers previous and file y is 1 buffer next. But with command-T I don’t really use :bn and :bp anymore but instead just <C-g> (<C-t> is for something else) and type bits of the path I want to open and hit enter, it’s great.

I found that (as the readme on github tells you) I needed to:
in vimrc: Bundle 'git://git.wincent.com/command-t.git'
in vim: <:BundleInstall
on command line:

cd ~/.vim/bundle/command-t/ruby/command-t
ruby extconf.rb
make

add shortcuts to vimrc

nnoremap <silent> <C-g> :CommandT<CR>
nnoremap <silent> <C-b> :CommandTBuffer<CR>

and restart vim.

Auto Complete

You Complete Me, coding without auto complete is just too much typing. Some autocomplete plugins are pretty good. You Complete Me is amazing. If you’re on Ubuntu or for some other reason whichever vim you have isn’t up to date enough to include Python support you’re going to need to install a new vim. Which sounds way worse than it is, how to do that, and install and use the plugin is described in easy cut and paste detail in the github readme so I won’t duplicate the effort here.

Linter

Syntastic Although you can just add Bundle 'git://github.com/scrooloose/syntastic.git' to vimrc and you’re good in fact.

And there you have it, I failed to mention snippets, because I haven’t actually spent any time using ulti snips which is what I’m planning on using yet. It looks good from the screencast (which has the best music).

What do you think? Am I missing anything? Doing anything wrong?

+ more

Accurate Timing

Accurate Timing

In many tasks we need to do something at given intervals of time. The most obvious ways may not give you the best results. Time? Meh. The most basic tasks that don't have what you might call CPU-scale time requirements can be handled with the usual language and...

read more