Useful vimrc snippets

These are some cool things that I discovered over time. They either save some time or add some nice functionality. You can just put them into your .vimrc file.

ASCII Boxes

To get some nice ASCII boxes from within the visual mode in Vim, I use this:

vnoremap ,bs !boxes -a c -s 79 -d shell<CR>
vnoremap ,bc !boxes -a c -s 79 -d c<CR>
vnoremap ,bl !boxes -a c -s 79 -d latex<CR>

This needs the boxes command.

jk

Instead of going to the escape key to leave the insert mode, I sometimes just type jk. I also mapped the other way around, so that I just need to hit both keys at the same time. This does the trick:

inoremap jk <Esc>
inoremap kj <Esc>

License Texts

Often I need license texts, like the MIT license or the GPL. So I just type ,lm and get the MIT license into the text.

nnoremap ,lb :read ~/Vorlagen/BSD.rst<CR>
nnoremap ,lg2 :read ~/Vorlagen/GPLv2+.rst<CR>
nnoremap ,lm :read ~/Vorlagen/MIT.rst<CR>

You will have to adjusts the paths, of course.

Generate HTML With Markdown

Writing HTML can be quite a lot of work. So I usually write the text I have in Markdown, select it in visual mode and type ,m and get it converted into HTML. This needs the markdown program.

vnoremap ,m !markdown<CR>

Sort

I like to have my imports or keyword lists or other things like that sorted. So I can either select them in visual mode and press ,s instead of typing :sort i. And if the lines I want to sort are in a paragraph, separated by blank lines, I can just hit ,p and have the whole paragraph sorted.

vnoremap ,s :sort i<CR>
nnoremap ,p Vip :sort i<CR>

Trailing Whitespace

Having whitespace at the end of lines is usually a mistake, so I have it marked as error in Vim. With a quick ,t I can remove all excess whitespace in the whole file. Be careful with languages like Markdown, where trailing whitespace can be important.

match Error /\s\+$/

nnoremap ,t :%s/\v\s+$//<CR>