3

My vimrc sets the shell option:

let &shell='/bin/bash -i'

which I confirm using

echo &shell

The -i makes the shell interactive. I want to do this so that it runs ~/.bashrc, which contains the setting

shopt -s extglob

However, extglob was not being set because ~/.bashrc was not being run, which in turn seems to be because the shell is not interactive. I determined this by putting the vim cursor on each the following lines

echo $-
echo $PS1

and issuing the normal mode command !!bash. According to the Bash manual, the string returned by the first command should include the letter i if the shell is interactive, while the string returned by the second command should include the text and control characters for the prompt that is shown at the bash command line (as opposed to nothing). Both tests show the shell to be not interactive.

Why would the -i flag in the shell option be ignored? What further troubleshooting steps can I take?

6

Your test is flawed. If you run !!bash, you're calling just the bash command explicitly. Why would you expect bash to be interactive in that case?

!! is shorthand for :.!, so you're running :.!bash, which would become something like: bash -i -c 'bash'. So the first bash might have extglob set, but the second won't.

Run :!echo $- for a better test:

himBHc

Press ENTER or type command to continue

In this case, the command run by Vim would look like: bash -i -c 'echo $-'.

If you do want to test with !!, use the pstree command (pstree -psa $$) to see what the parent process is. In my case, the relevant part is:

  `-vim,13387
      `-bash,13527 -i -c (bash) < /tmp/vAaJ5gB/7 >/tmp/vAaJ5gB/8 2>&1
          `-bash,13530
              `-pstree,13531 -psa 13530

As you can see, Vim did call it with -i.

Or just run :!shopt nullglob:

nullglob        on

Press ENTER or type command to continue

By the way, the Vim way to add an option to the shell being used to run :! commands is shellcmdflag. From :h :!:

On Unix the command normally runs in a non-interactive
shell.  If you want an interactive shell to be used
(to use aliases) set 'shellcmdflag' to "-ic".

From the Bash manual,

Invoked non-interactively

When Bash is started non-interactively, to run a shell script, for example, it looks for the variable BASH_ENV in the environment, expands its value if it appears there, and uses the expanded value as the name of a file to read and execute. Bash behaves as if the following command were executed:

if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi

but the value of the PATH variable is not used to search for the filename.

So a simple way to affect invocations of !!bash, would be to set BASH_ENV=~/.bash_extra:

let $BASH_ENV="~/.bash_extra"

where ~/.bash_extra would contain (among any other settings you need):

shopt -s nullglob

Using an interactive shell has undesirable effects, like the output being cluttered with your prompt. This avoids that.

  • Hi, muru: Yes, what you said was beginning to dawn on me. I put in a test of extglob in my bashrc, then did !!bash when the vim cursor was on shopt extglob. I got conflicting extglob statuses, one on the vim command line, and another replacing the cursor line. So the shelling out was interactive, but the invocation of bash in !!bash was not. Would you be able to complete your answer with an indication of how one might replace a bunch of contiguous lines in the vim buffer with the result of the execution of those lines by an interactive bash shell? – user2153235 Apr 4 '16 at 1:38
  • :. w !bash doesn't do it (as an example of applying this to the single-line range .,.. P.S. At this point, I only want the interactiveness to apply when shelling out from vim. I don't want to figure out the rat's nest of possible side-effects if I always have bash start interactively. – user2153235 Apr 4 '16 at 1:41
  • @user2153235 why not just !!bash -i? – muru Apr 4 '16 at 1:42
  • Tried it. There is some weird side effect where I get 11 repetitions of prompt control characters followed by $Use "exit" to leave the shell.. If this is the only way, I have some troubleshooting ahead of me. – user2153235 Apr 4 '16 at 1:45
  • 1
    Works like a charm, muru. Thank you for that, and for the hint about setting shell options for :! commands. – user2153235 Apr 4 '16 at 2:12

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.