My question is about mapping a multi-part command to a single key combo.

In my .vimrc, I've got the following line:

nnoremap <leader>ss :w\|:silent !execute_external_script > output_of_script.txt\|:redraw!<cr>

As you can see, I've got three commands I want to execute when i press <leader>ss. First, write the file, then execute my external script (the script runs a unit test). Unfortunately, my terminal doesn't redraw Vim after executing the shell command. So I tried appending that :redraw! command, apparently that command is getting passed to the shell, not to Vim, because I'm getting this error:

bash: :redrawecho: command not found.

How do I write the command so that the script executes in the shell, then redraw executes in Vim? (Also, as a bonus, could anyone explain what <cr> does?)


1 Answer 1


Yes, the :! command takes the remainder of the command-line as arguments. To concatenate other Vim commands, you can wrap the command with :execute:

nnoremap <leader>ss :w\|:silent execute '!execute_external_script > output_of_script.txt'\|:redraw!<cr>

Alternatively, you can just issue multiple command-lines, separated by <CR>; after all, a mapping is just a fixed macro:

nnoremap <leader>ss :w\|:silent !execute_external_script > output_of_script.txt<cr>:redraw!<cr>

Another note: Instead of escaping the \| command separator, the usual way is to use the special <Bar> keycode instead; see the full list at :help keycodes; this also explains that <CR> stands for carriage return.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .