Sunday, July 19, 2015

Vim as a IDE

I have been using vim as my main editor and development tool for more than ten years.  I love its lightweight and speed. I especially love its idea of editing with having your hands leaving the keyboard. It greatly improves my efficiency.
I used to use a bare vim without any plugins. Since I have a few development environments, I do not like to install a lot of dependencies before I can start editing freely. However, bare vim does lack some advantages provided by modern IDE, I took a weekend to investigate some.
Recently I mainly do development in python environment, thus my research foci on python.

.vimrc settings

The settings in .vimrc is very important to make a comfortable development environment.
The following settings does the following tweaks to the development vim environments:
  1. Turn on syntax highlight
  2. Expand tabs to spaces
  3. Show line number on each line
  4. Enable auto indentation
  5. Set the colouring to be friendly to a dark background
  6. Highlight the search keywords
syntax on
set expandtab
set shiftwidth=4
set softtabstop=4
set tabstop=4
set number
set cindent 
set autoindent
set bg=dark
set hls 

The plugin for plugins - Vundle

Before installing plugins, a tool for managing the plugins will make the job much more easier.
Virtually all blogs or tutorials would recommend either Vundle or Pathogen.
Both plugins can help you organize the vim plugins in a centralized way. But Vundle does it in a more elegant way.
To install Vundle, we simply clone the git repository to the vim plugin directory, by:
git clone https://github.com/gmarik/Vundle.vim.git ~/.vim/bundle/Vundle.vim
After that, add the following lines to the top of the .vimrc file.
set nocompatible              " be iMproved, required
filetype off                  " required

" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')

" let Vundle manage Vundle, required
Plugin 'gmarik/Vundle.vim'

" All of your Plugins must be added before the following line
call vundle#end()            " required
filetype plugin indent on    " required

Then Vundle will manage other plugins, including installation and update. To install a plugin, we just add a line under "Plugin 'gmarik/Vundle.vim'", and run ":PluginInstall" in vim.
This command support different formats of git repositories, including github, other git hosts and local git repositories, etc. You may check the Vundle github page for more details.

Python-mode

Python-mode is a plugin that adds a lot of IDE-like features to vim.
To install python-mode, we simply add this line to .vimrc:
Bundle 'klen/python-mode' 
And then type :BundleInstall in vim to let vundle install it.
After installation, you may need to change some settings to make it most useful for yourself. Type :h pymode in vim to get the full documentation.
If you have no idea what to change, you may check some references such as this.

Some of the most useful features include code checking, autofix PEP8 errors and go to definition, etc.

One thing I cannot make it working is the autocompletion. The shortcut for autocomplete is Ctrl-Space. However, in Mac, it will bring Spotlight instead. I have searched for a while but seems no one else is having this problem.

NERDTree

NERDTree is a filesystem explorer that let people look for and open files.
To install Nerd, add this to .vimrc:
Bundle 'scrooloose/nerdtree'

And More

After trying for a few plugins, I think they do help to increase productivity. They are more to try. I will introduce other interesting findings here in the future.

References:

  • Pathogen vs Vundle: http://lepture.com/en/2012/vundle-vs-pathogen
  • Vim as a Python IDE: http://unlogic.co.uk/2013/02/08/vim-as-a-python-ide/
  • Vundle homepage: https://github.com/VundleVim/Vundle.vim
  • Pymode homepage: https://github.com/klen/python-mode
  • Some more useful vim plugins: http://vimawesome.com/?q=tag:python

No comments:

Post a Comment