Pak
Pak

Reputation: 736

Prevent vim from escaping parantheses when using %:p

I have created a command in vim to open the current file in Explorer using the following:
command! E silent !start explorer /select,"%:p"

If I use this command to try to open my _vimrc file (located at: C:\Program Files (x86)\Vim\_vimrc) it opens the wrong directory. It appears that %:p is escaping the parentheses around "x86" when it starts Explorer which results in it trying to navigate to C:\Program Files \(x86\)\Vim\_vimrc

The part that I find weird is that if in vim I type the following command:
:echo expand("%:p")
it prints the path without escaping the parentheses.

Also, if I try hard coding the path in the command it open correctly. This leads me to believe that it is some weird quirk with vim resolving %:p when using the ! command.

Does anyone know of a way to prevent %:p from escaping the parentheses when using the ! command? I read through the help for both %:p and :!, but wasn't able to find anything to address this.

Upvotes: 2

Views: 126

Answers (1)

ZyX
ZyX

Reputation: 53644

I don’t know whether there is a way to do so, but using !...% in any script is a bad idea as it is impossible (at least on POSIX systems, AFAIK it is not so on windows where " is forbidden in file name). In case you need to pass a filename to a shell command use :execute+shellescape()+expand():

command! E :execute 'silent !start explorer /select,'.shellescape(expand('%:p'), 1)

Upvotes: 1

Related Questions