Let’s explore a typical use case: write the current buffer, start the build, check the errors.
-
Writing the current buffer is done with
:write
, that we can shorten to:w
. -
Building is done with
:make
, that we can shorten to:mak
. Vim is suspended to leave room for the compiler’s output. -
Opening the quickfix window is done with
:cwindow
, that we can shorten to:cw
.
Now, repeating the :w
& :mak
& :cw
dance over and over doesn’t sound fun. Does it? Well, those commands can be chained with a |
:
:w|mak|cw
and recalled with <Up>
so that’s not such a big deal but there is still room for improvement, here:
-
we still have too many
<Enter>
to press, -
some kind of automation would obviously be better.
Let’s address the first issue by telling Vim to shut up with the :silent
command (or :sil
for short):
:w|sil mak|cw
A simple mapping would be a perfect alternative to all that typing. Let’s try with <F5>
, a shortcut often used in IDEs to compile the project, both in insert mode and normal mode:
inoremap <F5> <Esc>:write|silent make|cwindow<CR> nnoremap <F5> :write|silent make|cwindow<CR>
Hmm… It looks like Vim doesn’t like bars in mappings. That’s understandable, actually: bars are used to separate commands but inoremap <F5> <Esc>:write|silent make|cwindow<CR>
is one command and the parts between bars don’t really make sense on their own. So what can we do? Escape those bars?
Well yes:
inoremap <F5> <Esc>:write\|silent make\|cwindow<CR> nnoremap <F5> :write\|silent make\|cwindow<CR>
or we can use <Bar>
:
inoremap <F5> <Esc>:write<Bar>silent make<Bar>cwindow<CR> nnoremap <F5> :write<Bar>silent make<Bar>cwindow<CR>
Let’s see how it looks:
(gifcast)