Vim Tips Wiki
Tip 1504 Printable Monobook Previous Next

created 2007 · complexity basic · author Tim Keating · version n/a

If you want to execute an external command on Windows, you need to know one trick. Let's say you're building a command to check out the file you're working on (using Perforce as an example):

map <F6> :!p4 edit %

However, that will just populate the command line. To force the command to execute without having to hit Enter, you need to embed a CR (carriage return). On Linux, you do this with Ctrl+V Ctrl+M. On Windows, the default is that Ctrl+V is paste, and you have to use Ctrl+Q instead:

map <F6> :!p4 edit %Ctrl+Q Ctrl+M

Which will look like:

map <F6> :!p4 edit %^M


Fix the above tip to incorporate the good sense in the following comment (use "<CR>" rather than trying to embed an 0x0d byte). The old method of embedding ^M should be mentioned.

Furthermore, there is no reason for this to be a Windows only tip. Linux beginners would need to know to embed CR also, and should learn that "<CR>" is almost always the best procedure.

You could use <CR> (4 characters), which should work when your 'cpo' does not have < included. See

Even with < in 'cpo', you would be able to enter a mapping this way – you need to use <special> modifier. See

One more thing: Ctrl-V is mapped only when your _vimrc contains the following line:

source $VIMRUNTIME/mswin.vim

You can use :sil[ent][!] {command} to execute external commands silently on Windows.

To execute my Python scripts I use:

map <F5> <Esc>:!python %<CR>

We REALLY need to rename this tip. Something like "Mapping a key to run an ex command". Do we need to mention external commands at all in the title? Maybe two titles, one with :ex command, one with external command? Regardless, the tip has nothing to do with Windows and only a little bit to do with external commands. --Fritzophrenic 04:18, October 22, 2010 (UTC)

Yes. I would show <CR> and briefly mention that you might have to embed ^M if you have unwise cpo settings. Title might be "Running external commands" (we don't seem to have anything on that, although of course many tips do exactly that), and tip could be expanded in future with a few examples or see-alsos of running external commands. Then, there could be redirects mentioning "map" and "ex", perhaps: "Mapping a key to run an external command" and "Mapping a key to run an ex command". See also Execute external programs asynchronously under Windows. JohnBeckett 10:31, October 22, 2010 (UTC)