Warning: Parameter 1 to wp_default_scripts() expected to be a reference, value given in /home/public/wp-includes/plugin.php on line 571
MacVim as IDE with Code Completion and Syntax Highlighting for Python, PyQt4 and Twisted Development on Mac OSX Snow Leopard

MacVim as IDE with Code Completion and Syntax Highlighting for Python, PyQt4 and Twisted Development on Mac OSX Snow Leopard

// March 16th, 2011 // Posted in OS X, PyQT4, Python, Vim // Tagged as , , , , ,

This journal is about setting up MacVim on OSX as primary IDE for developing Python along with PyQt4 and Twisted framework. I would suggest you to go through the “Setting up Python, PyQt4 and Twisted on OSX 10.6 Snow Leopard” before proceed with this journal because it is very critical that all installed libraries and binaries of Python, PyQt4 and Twisted are compatible with the MacVim that you have.

For Python development on OSX I prefer to install everything from MacPorts including MacVim. First we need to make sure that our MacPorts is the latest. To do so from the Terminal type:

pietra@unix$ sudo port -v selfupdate

Then install MacVim with Python 2.6 support. This will take sometime as it will compiles from the source.

pietra@unix$ sudo port install macvim +cscope +python26

Code completion on Vim (and MacVim) is done by ctags. Basically it’s a small program that generates tags or names found in source codes. It supports C/C++ sources, Python, Perl and many other languages. We definitely need it so the next step is to install ctags using MacPorts

pietra@unix$ sudo port install ctags

We need to make sure that Python support is working properly with our MacVim. Launch MacVim then on “normal” mode type:

:python print "hello world"

The “:python” part tells MacVim to execute the python interpreter. If the command successfully executed that means your Vim is compiled with Python support. To test it even further you could try to import PyQt4 and print the installed version.

:python from PyQt4 import QtCore; print QtCore.PYQT_VERSION_STR;

If you see version number coming from that last command then all development environments are properly setup. We just need to customize MacVim to move forward.

We need to create a .vimrc file, a .vim directory and some other directories within it.

pietra@unix$ cd
pietra@unix$ touch ~/.vimrc
pietra@unix$ mkdir ~/.vim
pietra@unix$ mkdir ~/.vim/colors
pietra@unix$ mkdir ~/.vim/compiler
pietra@unix$ mkdir ~/.vim/doc
pietra@unix$ mkdir ~/.vim/ftplugin
pietra@unix$ mkdir ~/.vim/plugin
pietra@unix$ mkdir ~/.vim/tags

A quick overview of the file and directories layout here. “.vimrc” is your main Vim configuration file. “.vim/colors” is where you put your colorscheme. “.vim/plugin” is where the plugins go while “.vim/ftplugin” is for file-type-specific plugins. “.vim/doc” is usually for plugins documentation.

The directory “tags” is where I put my generated ctags tags ;). We will ask ctags to generate the tags from our MacPorts Python 2.6 installation path and save it in our .vim/tags. From the Terminal type:

pietra@unix$ cd
pietra@unix$ ctags -R -f ~/.vim/tags/python.ctags /opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/

The -R part simply tells ctags to go recursive with all the directories within that path. That should generate tags from Python and all the installed libraries such as Twisted and PyQt4. We also need to tell MacVim to look inside the .vim/tags path. More on this later on.

Now let’s go customizing the .vimrc. We’ll take a look at mine.

set nocompatible

I don’t think I need the old Vi compatibility mode hence I get rid of it using set nocompatible.

For indentation I use 4 spaces. It’s my preferred way of coding regardless of language but for Python it also complies with the Python’s PEP-8 (coding style guide).

set tabstop=4
set shiftwidth=4
set softtabstop=4
set expandtab

Many times when I aim for the ESC button I accidentally press the annoying F1 instead. Here’s how I remap the F1 key.

inoremap <F1> <ESC>
nnoremap <F1> <ESC>
vnoremap <F1> <ESC>

Some tweaks to the auto complete & ctags. In here I specify the path for tags.

filetype plugin on
set ofu=syntaxcomplete#Complete
autocmd FileType python set
set tags+=$HOME/.vim/tags/python.ctags

the set tags+=$HOME part tells MacVim/ctags to look for for generated tags within .vim/tags directory inside my HOME directory.

As for the syntax highlighting and color theme.

syntax on
set t_Co=256
set background=dark

It basically says I want syntax highlighting to be active (syntax on). Since I want to have colorscheme I want MacVim to use 256 colors mode instead of basic color mode (set t_Co=256). Note that the Terminal in OSX 10.6 doesn’t even support 256 colors mode. So if you launch Vim instead of MacVim in the Terminal then you’ll have a hard time getting the 256 colors mode. The set background=dark tells MacVim that I prefer to work in dark background.

MacVim with Zenburn colorscheme and Inconsolata fontMacVim with Zenburn and Inconsolata

When it comes to a long session of coding and spend a lot of time in front of the monitor, I prefer to have a low-contrast scheme because it less poking in the eye. I am a big fan of Zenburn colorscheme. I apply it in almost any editor/IDE that I use or ever use (KDE’s Kate on Linux, Editplus on Windows, and Komodo Edit / Komodo IDE on Linux/OSX/Windows).

For programming font in my opinion Inconsolata is the best coding font. I am also a big fan of it.

To download and install a colorscheme for MacVim (or Vim) simply put the scheme file (colorscheme.vim) into .vim/colors. In my case I just have to go to the Zenburn page, download the zenburn.vim file and put it inside my .vim/colors in my home directory. To install Inconsolata on OSX you just have to drag the font & drop it to the Font folder in the Library folder.

Here’s how I apply my colorscheme (Zenburn) and custom font (Inconsolata) on my .vimrc.

colorscheme zenburn
let g:zenburn_force_dark_Background=1
set guifont=Inconsolata:h18

the let g:zenburn_force_dark_Background=1 is just a quick fix for zenburn to use a darker background.

note: more to come on configuring vim.

Ads by Amazon.com:

Please share this journal with others:

2 Responses to “MacVim as IDE with Code Completion and Syntax Highlighting for Python, PyQt4 and Twisted Development on Mac OSX Snow Leopard”

  1. Rishan says:

    How did you hide the blank line indicators of MacVim ~ ?

    • pietra says:

      I don’t think there’s anyway to hide the blank line indicator (~) because that’s the default behavior in any vim be it GVim or terminal vim or MacVim. I guess what you can do is to use colorscheme that can make the “~” less visible.

Leave a Reply