Roland's homepage

My random knot in the Web

Switching to the vim editor

date: 2013-08-17
modified: 2016-12-11
reading time: 5 min.
category: howto
tags: vim editor

After having been an Emacs user for a long time, I was starting to grow disenchanted with it around version 24. My main problem is that it was starting to get really slow. Not just in starting up, but also in daily use, specially it you kept an instance open for along time. I tried switching off bidirectional rendering, which was helpful but not enough.

So I decided to install vim, and give that a try.

Since I spend most of my time in the X Window System, I compiled vim with X support using the GTK2 toolkit. The other option I enabled was Python support.

Vim is a modal editor. This takes some getting used to. Basically you can be inserting text (insert mode) or you can be executing commands (normal mode). See e.g. the documentation. The upside is that in normal mode, all the keys are available for commands, so you don’t have to rely on the mouse or on chords (different keys pressed simultaneously) to execute commands.

Another plus is that vim is fast. It starts up fast and doesn’t bog down even with a lot of file open. Of course with the speedy startup, it is not necessary to keep a window open all of the time. And when started in graphical mode from a terminal window (using gvim instead of plain vim) it automatically detaches from the terminal, which is nice.

Plugins

Like Emacs, vim is also programmable and extensible via plugins.

Everyone has his own list of favorite plugins. Mine (so far) are;

  • auto-pairs: Insert or delete brackets, parens, quotes in pair.
  • fugitive: Interface for git.
  • pathogen: Easy management of plugins. Since a lot of modules are available on github I use git to manage my .vim directory with most of the plugins as sub-modules. To use pathogen, the first command in my .vimrc file is: execute pathogen#infect().
  • syntastic: Syntax checking of source code whenever a file is saved. This gives immediate feedback when you are writing code. Very helpful in fixing code now rather than later.
  • ultisnips: Fast insertion of often used fragments. This is a real must-have in my opinion. The snipmate plugin is mentioned more often but Ultisnips is more powerful. It needs a vim compiled with Python support but as an upside you can use python code to generate snippets.
  • vim-airline: Cool status bar.
  • vim-colors-solarized: The dark variant is my preferred color scheme for gvim.
  • vim-dirvish: Alternative for netrw. I tried vim-vinegar but it tends to leave netrw buffers that are hard to close.
  • vim-surround: For changing “surroundings” (quotes, brackets et cetera).
  • tabular: For aligning text tables.

Configuration

You can download my complete vimrc if you want to. But I want to highlight some settings that I found interesting or difficult to figure out, in the hope it helps.

Source:vimrc
GPG signature:
——-BEGIN PGP SIGNATURE——-

iQIcBAABCAAGBQJWkqmLAAoJEED21dyjijPg4qsP/3hBsq5CO9db+3pVKhCH/Zwb
VuHc0Yq7NLC3kKsNiQ5tYw9oGsN//uVH+ayv73vtRsBJmMMqX0hLUIUwLQw1PhkQ
YNgILckQ0mq6jvDo30adpSoLGMI03g7o7VGa9Zb0HRvReHKQ0fIDmv25XwQj03z0
y4eG87e/TTrnYI+HmX/i5AAuwqC12kDzyLzWaDXVbQ4keIh8inQvQnxHEeynqvmc
mRkptLJzV9pnGPEbpqF3ABtR/pNp3oxPL40A6XOna/LA1vk9yaZcXV66EAvbI/Vf
4zB/c17jZeKwVx7GawbC08bWYd4Fyk1ZHsozAvnhoSYGPp12WnGSyiwik6MHfygD
pIHC/99Y8dGnh3hMpyOS/tIuhkmYsb2XiSDBTJZiBIqKWctmbRk+etQBrSFepQwo
cnVLr/Ae+IhAGG0JfwApAmqZAbp8jER96tGLLO3OaazZXHrcGnDNIUv/Rwf9pAUX
CI/lQGw8xQrfMJOxkCfxFFaDyXMythDAHjEQ1A/ta9HkkXYQWfi5WRIjRdaMBxt4
BUbG7MpSLSetISaR1a3jlHqAzNPTJecZFGVbJCcuFwnwHSqEfI3O3Sf4WdkpCbq1
0hYuBUll0r1GefjRpbdv1Cu+7PhWQxjBqnsXgKW8KycOZHZQOLvyX/7Xtue+s4VR
MsejAsZvt8l7OSMcCsGZ
=SP5Q
——-END PGP SIGNATURE——-
SHA256 hash:acb0fbd2fc4cff88f9891c8599f02974e7fd6f8d623c3e6860a4cacd7d3e7cd3

To make use of the pathogen plugin manager, the _first_ command in your vimrc should be:

execute pathogen#infect()

In normal mode, the <leader> key is used to basically get a whole new namespace to link commands to keys. By default this is mapped to the \ key. I find it more convenient to use the spacebar:

let mapleader = "\<Space>"

Compatibility with the old vi is not really important for me, so I’ve switched that off.

set nocompatible

Using UTF-8 encoding should be an easy decision these days for someone like me who writes mostly in English or Dutch (or one of several programming languages). Unicode is a widely-used standard (ISO 10646) for encoding text, and UTF-8 is an encoding that is backwards compatible with ASCII and Latin-1 (ISO 8859-1). It is not always straightforward to detect the encoding of a file, so it is good to be explicit about this.

set encoding=utf8

Since I keep stuff I care about under revision control with git, I switched off automatic backups:

set nobackup
set nowritebackup
set noswapfile

You can have vim automatically re-flow paragraphs when you insert or delete text. This does not always work well. E.g. in ReStructuredText it messes up headers. So I wrote the following function to toggle that behavior, complete with a message to the user and a key binding in normal mode:

function Toggle_autofill()
    if &fo =~ "a"
        set formatoptions-=a
        echo "Disabling automatic filling"
    else
        set formatoptions+=a
        echo "Enabling automatic filling"
    endif
endfunction
nnoremap <leader>a :call Toggle_autofill()<cr>

Additionally, I’ve set up a key binding to format the current paragraph:

nnoremap <leader>f gqap

In normal mode, vim can use the h,j,k,l keys to move the cursor (left, down, up, right. You can also use the arrow keys, of course). To move around windows, you can use the same letter keys but you have to press ctrl-w first. I decided to do some remapping to use ctrl+h,j,k,l to move around windows:

noremap <c-h> <c-w>h
noremap <c-j> <c-w>j
noremap <c-k> <c-w>k
noremap <c-l> <c-w>l

With regard to ultisnips, by default it uses ctrl-k for the backward jump trigger when inserting snippets. This is unfortunate because the default mapping for that key is to insert digraphs, which is a feature I often use. So I added the following code to my .vimrc to fix that:

let g:UltiSnipsExpandTrigger="<tab>"
let g:UltiSnipsJumpForwardTrigger="<tab>"
let g:UltiSnipsJumpBackwardTrigger="<s-tab>"

BTW, the default digraphs that vim uses are those from RFC1345.


For comments, please send me an e-mail.


Related articles


←  Nginx webserver setup Using Matroška containers for videos  →