Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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)?

Thanks.

share|improve this question
add comment

1 Answer

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... –  ido Jul 28 '13 at 18:12
add comment

Your Answer

 
discard

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.