First off, you should always use
inoremap rather than
imap (with a small number of exceptions that are not relevant here).
The difficulty here is that as soon as
% is put after
\, the backslash goes from path component to escaping character. The only way I could prevent that is to use an expression (
<expr> on lhs of the mapping) so I could use
expand("%") in it:
inoremap <expr> <f4> '<ESC>:w<CR><ESC>:!clang -std=c++11 -o output\' . expand("%") . '.exe ' . expand("%") . '<CR>'
inoremap <expr> <f5> '<ESC>:!output\' . expand("%") . '.exe<CR>'
Update : Actually I found a way to do this without
<expr>. The second "Note" under
:help E809 says that double quotes can be used to isolate
% while retaining its special meaning. It looks a little strange on the
cmd.exe command line but it works in my tests.
inoremap <f4> <ESC>:w<CR><ESC>:!clang -std=c++11 -o output\"%".exe %<CR>
inoremap <f5> <ESC>:!output\"%".exe<CR>