How can I tell subprocess to stop escaping my quotes?


Question

I'm trying to call Vim using subprocess, and pass it an argument. For example:

subprocess.call(['gvim.exe', ''' "+map <F5> :echo 'Hello!'<cr>" '''])

This command works on the command line:

> gvim.exe "+map <F5> :echo 'Hello!'<cr>"

And then I hit F5 and it tells me hello.

The subprocess call doesn't work. When I look at the process in task manager, I see that my string is now:

"\" +map <F5> :echo 'Hello!'<cr>\""

Not at all what I expected, and I don't think it's what Vim expects, either. It looks like subprocess is somehow escaping my quotes, but I don't know why.

Is there any way I can get this to work like I expect?

1
5
1/31/2013 6:10:51 PM

Accepted Answer

The quotes aren't necessary since subprocess.call passes the arguments directly to the process without using the shell (unless you set shell=True).

So, subprocess.call(['gvim.exe', "+map <F5> :echo 'Hello!'<cr>"]) will suffice.

5
1/31/2013 6:12:30 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon