Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm a relatively recent convert to Vim, so I don't know a whole lot. 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?)

share|improve this question
According to: swaroopch.com/notes/vim_en-introduction <CR> means "a ‘c’arriage ‘r’eturn, i.e., the enter key." –  ramonovski Jun 6 '13 at 20:52

1 Answer 1

up vote 4 down vote accepted

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.