Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In gVim in windows, I want to map a command in the vimrc file that uses the current file (%). I write something like:

map <F5> !mycommand %

But then, when gVim first loads, it runs vimrc and says that it cannot find the current file, because it is not opened yet. How can I map this in the vimrc, but make it parse the % command only when I hit F5 (after I have opened a file)?


share|improve this question
up vote 2 down vote accepted

You could do

map <F5> !mycommand shellescape(expand('%'))<CR>

shellescape() makes the escapes the all the special character in the string (ex spaces)
expand() expands the string in this case the current file name.

However the map should have only been parsed when the map was executed. The might happen if you have an autocmd that runs <F5> or This might happen if you used map somewhere else and that got executed. It would be better to use the noremap versions of map unless you want recursive mappings.

share|improve this answer
The problem was I used a pipe when I shouldn't have, so it was executed anyway... – yoki Jul 28 '13 at 18:12

You must log in to answer this question.

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