3

I am (slowly) switching to Vim for everyday Ruby on Rails coding (Linux Cinnamon 64bit). Being a newbie, I recently came across something I don't understand.

I want to use the :vimgrep and :grep commands for searching a pattern in a file tree from Vim. I am able to do so without problems with the :vimgrep command:

:vimgrep /User/gj **/*.rb

However, when I try to use the same command with :grep (which should use the system grep), no result is returned:

:grep User **/*.rb

Googling around, I found that the globstar options of my bash shell was not enabled:

$shopt globstar
globstar        off

It was easy to turn on:

$shopt -s globstar

I added the above line to my .vimrc file, and I made sure the option was permanently enabled in my bash.

With the new option, I could execute grep this way and get the desired results:

$grep User **/*.rb

Unfortunately, the :grep command from inside Vim is still not working with the '**'. And executing:

!shopt globstar

from inside Vim, the result is still off.

So the question is: can I propagate my bash's globstar option to my Vim external command environment? Otherwise, can I somehow set my grepprg option in Vim in a way that will allow me to use the globstar properly?

Thanks in advance to anyone who will help

  • Which version of vim? What does :help starstar-wildcard say? – muru Dec 9 '14 at 9:59
  • 2
    :set shell=/bin/zsh or :set shell=/bin/bash\ -O\ globstar, or :let $BASHOPTS="globstar" – Stéphane Chazelas Dec 9 '14 at 10:03
  • vim version is VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Jan 2 2014 19:39:32), I think the :help starstar-wildcard is relative to the internal commands (correct me if I am wrong) as the ** was already working with :vimgrep – nerfologist Dec 9 '14 at 10:38
5

The shell options don't inherit to forked shells, but that's what Vim does when you use the :! command: it launches a new shell.

You can influence those forked shells in Vim via the 'shell' option:

:set shell+=\ -O\ globstar

or

:set shell=/bin/bash\ -O\ globstar
  • this worked like a charm. I also added set shell+=\ -O\ globstar to my .vimrc. Thanks a lot! – nerfologist Dec 9 '14 at 10:40
  • Thanks; glad I could help. Don't forget to accept the answer by clicking on the outlined checkmark next to it! – Ingo Karkat Dec 9 '14 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.