5

For some reason I can't for the life of me figure out how to pass single quotation marks to external tools when using vim. For example, I might want to call Vim's external grep to search for the text "foo (that is, a single quotation mark then the word foo)

So I might try the command:

:grep \"foo *.txt

But this results in the following error message:

!findstr /n \"foo *.txt >C:\Users\[username]\AppData\Local\Temp\VIeC244.tmp 2>&1
shell returned 1
E40: Can't open errorfile C:\Users\[username]\AppData\Local\Temp\VIeC244.tmp

Note that grep is changed to use 'findstr /n' since I'm on windows (and can be verified by running echo &grepprg). What I find is that I can easily execute the search I want if I do it myself from the command line by typing the following:

findstr /n \"foo *.txt

Which works exactly as I'd expect

Steve Vermeulen
  • 567
  • 4
  • 14
  • 1
    Seeing the error message might be helpful. Also, have you tried escaping the quotation mark *twice*, i.e. `\\\"`? I can only assume that vim unescapes the parameter(s) given to `:grep` and calls `findstr /n "foo *.txt` - which would of course be an invalid command and cause an error. – n.st Sep 14 '13 at 02:09
  • Right, sorry, I updated the question with the error message. I tried a whole bunch of variations on it, including escaping twice, but couldn't get it working – Steve Vermeulen Sep 14 '13 at 02:33
  • I'll also add that I've been using vanilla vim for my testing – Steve Vermeulen Sep 14 '13 at 02:33
  • 1
    Did you try `:!findstr /n \"foo *.txt`? If that fails, your working directory (try `:pwd`) might not be set to what you would expect, so `*.txt` won't find anything. (By default, Vim doesn't automatically change the working directory to the directory of the current file). – n.st Sep 14 '13 at 10:14
  • 1
    To automatically change the working directory to the directory of the current file/buffer, add the following line to your vimrc: `set autochdir` – n.st Sep 14 '13 at 10:20
  • "grep foo *.txt" works fine so I don't think it's the current working directory. I tried `!findstr /n \"foo *.txt` and got some interesting output in a command shell that popped up: `C:\Windows\system32\cmd.exe /c (findstr /n \^"foo *.txt) \n shell returned 1 \n Hit any key to close this window...` So it apparently adds a caret for some reason – Steve Vermeulen Sep 14 '13 at 12:08
  • Please edit the title of your question: you are running `findstr`, not `grep` and `:grep \"foo *.txt` works with `grep` anyway. I already took the liberty to add the `findstr` tag but a more precise title would certainly help you. – romainl Sep 14 '13 at 12:24
  • 1
    I posted to vim_dev about this...apparently it's a place where the default ( and even the extended "( value for shellxquote fails. https://groups.google.com/d/topic/vim_dev/sG2d6d3cPwg/discussion I posted a workaround that seems to function for this command as an answer. – Ben Sep 14 '13 at 19:45
  • Thanks Ben, that's really interesting. @romainl: I updated the title to mention that it's windows, but it is technically still using what vim calls external grep – Steve Vermeulen Sep 15 '13 at 00:47

1 Answers1

1

You can try temporarily (or permanently) setting 'shellxquote' to a single quote character, like :set shellxquote=\"

Note this will break some external commands using special characters like &, |, <, >, etc. (all the stuff in shellxescape by default), so you may need to not leave it at that value. But, it looks like this is one place where the default shellxquote value from Vim 7.3.450 is broken.

Ben
  • 2,249
  • 19
  • 19