Do you ever feel like graphical user interfaces have gone too far?
Do you pine for the days when your entire desktop consisted of one console window?
Or do you simply want luxury and productivity when you ssh into a headless server?

This is Part 2 of a series of posts outlining my recipe for living comfortably in the terminal.

Part Two: Vim

I’m going to be careful not to start a holy war here. Every developer should be proficient in a text editor. It is as the knife is to the chef. I don’t have a strong opinion on which text editor developers should be proficient in, but I can at least explain why I use Vim.

Vim editing a python file, running in a byobu tab

Vim is powerful. Unlike most editors, you start in a mode in which keypresses are commands, not text input. There are commands for moving the cursor directionally (hjkl), or jumping words (w) or paragraphs (}) forward and backward. Commands for cutting (d for delete) and pasting (p), changing (c), inserting text like a normal editor (i)… The beauty is that these commands can be strung together like a language to achieve results. e.g. I want to cut 5 lines starting from my cursor: to cut is d, to act on a line is usually the command twice, so dd, and to do something 5 times is just a numerical prefix. Put it together: 5dd. Then I could move 4 paragraphs down with 4} and paste the lines I just cut: p. This composition of simple commands adds up to serious timesaving as you improve.

Vim is heavily customisable, in much the same way proper text editors are. It has the benefit of years of popularity behind it, such that there is a plugin to do almost anything. One’s .vimrc is a living document of settings and plugins and all sorts, chasing the perfect setup.

Vim is omnipresent. Its predecessor vi, created in 1976, is virtually guaranteed to come preinstalled on every unix/linux based system ever, and these days is usually symlinked to vim itself. It’s fully open source and cross-platform, it can be just as useful running graphically on Windows as it can be in a linux terminal. Which brings me on to the most relevant point to this series…

Vim is made to run in a terminal.
It can run as a typical windowed desktop program, but typing vim example.txt into a terminal will put you straight into a syntax-highlighted, full-featured editor that requires no mouse input and plays nicely over slow connections.

Crash Course on Vim

I won’t attempt to replicate vimtutor here, or repeat myself about simple speed-editing commands as described above, but here is the bare minimum to start comfortably with vim:

  • You start in Normal mode. Here every letter is a command, typing words will do unexpected things. You probably want to type words, so enter Insert mode by pressing i.

  • You can get back to Normal mode by pressing Esc.

  • Some commands take more than one key, these usually begin with :.

  • :w saves (‘writes’) the file. :w filename is ‘Save as’.

  • :q closes (‘quits’) the file. :q! quits discarding unsaved changes. :wq writes and then quits!

  • :e opens (‘edits’) a file.

  • :tabe opens a file in a new tab. Very handy.

  • gt switches to the next tab.

  • / begins a find. e.g. /hello

  • :%s/hello/world/g finds and replaces (‘substitutes’) hello for world in every occurence in the file. % here means ‘all lines’ and g means ‘all occurences in a line’.

  • u is undo a command (including an entire instance of Insert mode).

  • Ctrl + r is redo, for when you have undone more than you wanted to.

Here is a basic template for a ~/.vimrc to enable useful settings:

set nocompatible "Enable vim features that weren't in vi
filetype plugin indent on "Enable detection of filetypes
set expandtab "Use 4 spaces instead of tab
set tabstop=4
set shiftwidth=4
set softtabstop=4
set encoding=utf-8
set scrolloff=3 "Always have 3 lines above & below cursor visible
set showmode "Show which mode vim is in
set showcmd "Show commands as you type them
set wildmenu "Show possible completions of your command
set wildmode=list:longest
set visualbell "Flash instead of beep
set cursorline "Highlight current line
set ttyfast
set ruler "Show line:col numbers in status
set backspace=indent,eol,start "Make backspace behave as you expect
set laststatus=2 "Always show status line
set relativenumber "Show relative 'distance' to a line in Normal mode
set number "Show the current line no.  on current line
set modelines=0 "Don't run embedded macros like Office '95
syntax on "Enable syntax highlighting
set ignorecase "Case-insensitive find...
set smartcase "...unless the find string has an uppercase
set gdefault "add /g to find by default
set incsearch "find as you type after /
set showmatch "Flash matching bracket when you add one
set hlsearch "Highlight find results
nnoremap ; : "Use ; to mean :, no more shift!
set background=dark "Look pretty in a dark terminal! Remove for light terms.

When you get comfortable with the basics, check out Vundle, a way to bundle plugins (even things like colour schemes) into Vim directly from Github, or another git repository.

There is always more vim to learn. I did not know that * in Normal mode means ‘find the word under the cursor in this file’ until I pressed it by accident trying to emphasise some Markdown!

Now you have a multiplexed terminal and a powerful text editor, the command line world is your oyster.
Keep an eye out for the next post in this series on constructing your terminal paradise.