Vim config

published: 2017-06-04 05:23:00, updated: 2017-06-04 05:30:37
vim;
A description of vim config options and plugins

dotfiles available here

Vim is important to know because of the position it enjoys of being pre-installed on (nearly all) servers. For quick text editing, there's nothing better than it. Plus, it doesn't take up a lot of memory or CPU time, works well and consistently out of the box, and is very very powerful.

In this post, I'll detail my vim configuration that has been gathered up over the years.

Colorscheme

Vim contains several pre-installed colorschemes. However, I like to use Solarized with the background set to dark. This matches my terminal colors, and allows me to work well into the dark night. Sometimes during the day, I use the light variant as it has a better contrast against the hard sunlight.

set background=dark
colorscheme solarized

Formatting

Whitespace

Whitespace formatting is important because literring a file with a mix of tabs and spaces is a cardinal sin. I like to use a consisting tab of 4 spaces across all editors I use. Vim can automatically expand tabs to spaces, which is great.

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

rulers and margins

Vim can show a visual line at a certain column to indicate text spilling over. It is also possible to automatically (hard or soft) wrap lines at that particular column. Usually, this is at 80 or 120 characters wide. I usually like to keep it to 80 columns.

set colorcolumn=80
set ruler

folding

Folding means hiding certain text - say a function or a class and then displaying the minimum required information such as class name or function definition. In vim, it is possible to restrict folds at certain levels or to set how the folds are calculated. Since I mostly work in python, I've set up the folding based on indent. I've also remapped folding to the space key to make it super-easy to open and close folds.

set foldmethod=indent
set foldlevel=99
" enable folding with spacebar
noremap <space> za

Filetype

Vim can configure its settings according to the file type. Since I mostly work with Python, which has indent-based syntax, vim knows to work with the languge blocks in terms of indents.

set autoindent
set fileformat=unix
syntax enable
let python_highlight_all=1
filetype indent on
set encoding=utf-8
set autochdir   " Change working directory to open buffer
set nocompatible
filetype off

Line numbers

I cannot emphasise the importance of line numbers enough. Often when I'm helping students in the lab debug their programs, the compiler is kind enough to mention the line number the error occurs at, and the student does not notice it because they do not have line numbers turned on.

ALWAYS TURN LINE NUMBERS ON

They not only help in debugging, but are also a great way to quickly point out a specific line to someone.

set number

Relative line numbers

Recently, I found a nifty thing vim is (now) capable of doing - showing relative line numbers. Plain old simple line numbers begin numbering the first line from 1 and then 2 and so on to the end of file. Relative line numbers are relative to the line the cursor is at. So the current line is line 0 and the one above it and one below it are line 1 and so on. What this does in vim, is it makes motion super easy to go to. In large files, where lines are in hundreds, going above to a particular function on screen means typing out the entire line number. With relative line number, one only needs to type out the relative number with the motion, such as 5j to go 5 lines below the current one. Combined with the normal line number option, it shows the actual line number on the current line, and relative line numbers on all other lines.

set relativenumber

Other options

Spellcheck

Vim can highlight spelling errors in various languages (depending on the language installed and available to use).

set spell

Search and highlight matches

In vim, searches can be highlighted as they are typed. This makes it easy to see what the matches are, and is a behviour consistent with other editors.

set showmatch
set incsearch
set hlsearch

Plugins

Solarized

Solarized comes as a handy plugin, available at Github.

Ale

Ale is an asynchronous linting engine, which helps catch linting errors by showing them visually in vim. It is quite fast, lightweight, and works with most of the popular languages.

Airline

Airline is a statusbar plugin that adds colors to the tagbar (at bottom). It enables a visual mode to distinguish between normal, replace, visual, and insert mode. Additional themes are available at Airline Themes.

CtrlP

CtrlP allows opening files quickly using Ctrl+P through character matching in the filename. For e.g. to open a file at path doc/abc/xyz.txt, I can type daxyz and the file will come up in the listing. It is a fast and easy way to open files or buffers.

Fugitive

Fugitive is a git wrapper that allows executing git commands over the currently opened file. It is very extensive, and allows a plethora of git commands. Plus, it is authored by Tim Pope.

Surround

Surround allows manipulation of surrounding brackets, quotes, tags, pretty much everything.

Tagbar

Tagbar displays a sidebar consisting of CTags found in the file. Requires exuberant ctags. Selecting a tag allows quick movement to wherever it is defined in the file. For easier manipulation, map the sidebar to show/hide using F8.

Commentary

Commentary allows quick comment/uncomment using gc. Can be used with motion, making it possible to quickly comment an entire paragraph or visual selection.

Multiple cursors

Multiple Cursors allows inserting multiple cursors just like Sublime Text, though it is not as simple or as elegant to do it in Vim.

EasyMotion

EasyMotion allows quickly moving ahead by word or through the next few lines using a single-letter motion. It is surprisingly simple to use and makes short motion easy.

Signature

Signature allows interacting with vim marks, which are like bookmarks in a file. It allows alphabetical traversal, removing marks, and all sorts of things which make navigating back and forth through files a little easier.