Vim

Vim is the humankind’s tool to fight ignorance, poverty, and alien invasion by writing and editing structured text.

Acknowledgement

Special thanks to Klutzy who enlightened me to this path of Vim madness mastery.

Random facts

As if to prove how profound the editor is, there are facts that shock even seasoned Vim users. I tried to list the most useful and less known ones below.

Plugins

Vimscript is known to terrify developers, but smart people somehow figured out and extended Vim to support incredible features.

The first thing to get is a plugin manager. The plugin manager position of Vim went through a Warring States period including pathogen, Vundle, and neobundle, but it seems I’ve found the resolution: junegunn’s vim-plug. It is lightweight, configurable, and lightning fast due to:

The plugins I have deployed are:

Tim Pope: The Vim Pope

tpope deserves a separate section. This person is amazing. He has “a perverse knowledge of Vim script so vast that many would consider it a symptom of mental illness.” Check out how he advertises his dispatch.vim (sound required). He authored a bewildering number of Vim plugins, most of which rock.

Noteworthy ones include:

The .vimrc

The .vimrc file is to a Vim user as a lightsaber is to a Jedi. Some good starting points:

Tips below are what I have set up, all of which I can highly recommend.

Recycling Ctrl-S

Ctrl-S freezes the terminal. Ctrl-Q resurrects it. It is hard to understand why such feature even exists at all. Turns out it’s a legacy of teletype. You can disable it in your shell configuration (~/.bashrc by default)

stty stop undef

and you should. After that this extra seat on the keyboard can be wired to a useful feature.

Saving the file is :w, but this is too much endeavor. ⇧ Shift, ;, w, and Enter: four keystrokes. If you are in the insert mode, that’s five.

Ctrl-S, with only two keystrokes, is actually an excellent mnemonic for this because it stands for “Save” and means exactly that in many other editors.

" Map Ctrl-S to saving in both normal and insert mode
nnoremap <C-s> :w<CR>
inoremap <C-s> <ESC>:w<CR>

Recycle complete! With only one line in .bashrc and two lines in .vimrc. Press Ctrl-S anytime (both in normal mode and in insert mode) to save the file. You won’t believe how convenient it is once you get used to it.

Post-save hook

It is common to have a task that needs to be run every time a file is saved. Linting source code, converting Markdown document to HTML, etc. Assuming such task is a command named build all:

:command! Build
    \ execute ':w'
    \ | execute ':silent !build all'
    \ | execute ':redraw!'

" Map Ctrl-S to the command in both normal and insert mode
nnoremap <C-s> :Build<CR>
inoremap <C-s> <ESC>:Build<CR>

If you want to enable this configuration only for certain file types, use

autocmd FileType yourtype,anothertype nnoremap <C-s> :Build<CR>
autocmd FileType yourtype,anothertype inoremap <C-s> <ESC>:Build<CR>

and change the yourtype,anothertype part to the correct file type. For example, markdown.

This saves you so many keystrokes AND mental process. Note that if the build all command takes long, fails often, and prints out something meaningful, dispatch.vim suits better.

Obviously the feature doesn’t have to be bound to Ctrl-S. If you prefer to have separate keys for saving files and running the command, I suggest F5 for the latter.

autocmd FileType yourtype,anothertype nnoremap <F5> :Build<CR>
autocmd FileType yourtype,anothertype inoremap <F5> <ESC>:Build<CR>

Restore the last editing position

" Restore the last editing position
au BufReadPost *
\ if line("'\"") > 0 && line("'\"") <= line("$") |
\ exe "norm g`\"" |
\ endif

Afterthought sudo

If you forget to open a file with sudo vim instead of vim and make changes, the result is frustrating.

$ vim /etc/hosts
...

:w
E45: 'readonly' option is set (add ! to override)
:w!
"/etc/hosts" E212: Can't open file for writing
what the !@#$%^&*

Open .vimrc and include the following:

" :w!! to save with sudo
cmap w!! %!sudo tee > /dev/null %

You can now

$ vim /etc/hosts
...

:w
E45: 'readonly' option is set (add ! to override)
:w!
"/etc/hosts" E212: Can't open file for writing
:w!!
[sudo] password for alice:

Hence the saying:

Violence Exclamation mark: If does not solve your problem, you are not using enough of it.

Delete without copy

Deleting pollutes the register by default. "_d is not quite memorable or easy to type. A brilliant solution by Hyeon Kim and Klutzy:

" Press backspace in visual mode to delete without copy
vnoremap <backspace> "_d

Backspace! Never realized this key was being wasted. “Backspace deletes without copy” makes perfect sense, doesn’t it?