Pythonify vim


I been using vim editor for a while now . VIM is one of the highly configurable text editor, It can be used as IDE for bash,perl,python and many other. In this post i am going to provide some details about how to configure vim as a python IDE. I am going to keep the post as simple as i can , so that anyone with basic knowledge in Linux can configure it.

Softwares used

  • Operating system : Ubuntu 16.04 LTS
  • VIM :┬áVIM – Vi IMproved 7.4 (aptitude provide)
  • Python : Python – 2.7
  • GIT
  • YouCompleteme(vim plugin)
  • Syntastic(vim plugin)

vim has build-in auto complete feature . To use it simply in edit mode press ctrl+x and press ctrl+n , you will be shown the autocomplete words.But this is limited to the words already present in the file.

Step1: Open vimrc and add the following line at the end of file and save it.

vikki@trinity:~$ sudo vim /etc/vim/vimrc
syntax enable
set cursorline
set showmatch
let python_highlight_all = 1
filetype indent on
set wildmenu
set hlsearch

I am not going to explain the above lines one by one in details, as you can understand most of it by the name.

 

Step 2: Install plugin for Autocomplete python

At this point, you will be able to create python script which do automatic identataion and syntax highlight.
Now we are going to add autcomplete feature to vim for python development. This can be accomplished with vim plugin youcompleteme. Which can be installed using the vim plugin manager “vundle”

Step 2.1: Download vundle

Vundle is a plugin management tool for vim. If you don’t have git in your machine, install it by sudo apt-get install git.
Vundle can be installed using git

cd /etc/vim/
sudo mkdir plugins
cd plugins
git clone https://github.com/VundleVim/Vundle.vim.git

 

Step 2.2: Open vimrc file again and add the below line

sudo vim /etc/vim/vimrc
" Vundle vimrc
set nocompatible " be iMproved, required
filetype off " required
let g:ycm_autoclose_preview_window_after_completion=1

" set the runtime path to include Vundle and initialize
set rtp+=/etc/vim/plugins/Vundle.vim
call vundle#begin()
let g:ycm_confirm_extra_conf = 0
" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'
Plugin 'https://github.com/Valloric/YouCompleteMe.git'

" End configuration, makes the plugins available
call vundle#end()
filetype plugin indent on

 

Step 2.3: Install “youcompleteme” plugin

Create the below folder if doesn’t exist.

mkdir ~/.vim/bundle

Now open vim and type “PluginInstall” in ESC mode and press enter. This will download the YCM plugin from git which is almost 220MB in size

ycm_plugin_install

 

VIM should be compiled with python2 support for YCM plugin to work. But by default VIM in ubuntu16.0 is is not compiled with python2. So when you auto complete a python2 script using ctrl+space you will get the below error.

Error: Required vim compiled with +python
E117: Unknown function: pythoncomplete#Complete

This can be fixed by adding python2 support to VIM by installing vim-nox-py2

sudo apt install vim-nox-py2

That’s it, we are done. Now you can create any python script and the autocomplete works by pressing ctrl+space

optimised_auto_complete

 

Step 3: Syntax checking

Step 3.1: Install pylint

vikki@trinity:$ sudo apt-get install pylint

Step 3.2: Install syntastic

Add the below line to /etc/vim/vimrc file .

Plugin 'scrooloose/syntastic'

open vim and run “PluginInstall” in ECS mode. This will install the syntastic plugin to vim.

Add the below line to /etc/vim/vimrc file again

set statusline+=%#warningmsg#
set statusline+=%{SyntasticStatuslineFlag()}
set statusline+=%*
let g:syntastic_always_populate_loc_list = 1
let g:syntastic_auto_loc_list = 1
let g:syntastic_check_on_open = 0
let g:syntastic_check_on_wq = 0

Now open the script in vim and run “SyntasticCheck” in ECS mode . You will be able to see all syntax errors

syntax_check

Step 4: vim dictionary of python

We can also make use of the dictionaries to make better use of python.
The plugin “https://github.com/rkulla/pydiction.git” provide the dictionaries for python. Install it using vundle.

Configure set g:pydiction_location to the full path of where you installed complete-dict in vimrc

let g:pydiction_location = '/home/vikki/.vim/bundle/Pydiction/complete-dict' 

Now open vim and in insert mode press TAB to autocomplete