Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim: change prompt on shell escape

Tags:

shell

vim

I often use :sh while editing a file in vim so that I can perform small tasks like git commits before getting back to editing the file. However, sometimes I get confused whether my shell was started by my terminal emulator or it was started as a vim subshell, so typing exit at the prompt always runs the risk of closing the terminal emulator by accident rather than going back to my vim editing session. Is there a way to have vim modify my prompt, perhaps by the $PS1 environment variable, when I start a shell from vim so that I know whether I'm in a subshell started by vim or not?

like image 307
jayhendren Avatar asked Dec 19 '22 23:12

jayhendren


1 Answers

When you do :sh, a few additional Vim-specific shell variables are available to you. On this machine, I have:

$MYVIMRC
$VIM
$VIMRUNTIME

You can use $VIM, for example, in your *rc file like this:

if [ $VIM ]
then
  # set your vim-specific PS1 here
else
  # set your normal PS1 here
fi

Bonus: in GVim/MacVim the pseudo terminal you get when you do :sh is incapable of displaying colors. because Vim exports it as dumb, you can use the same logic as above to have a monochrome prompt when in GVim/MacVim and a color prompt in your shell:

if [ $TERM == 'dumb' ]
then
  # no colors
else
  # colors
fi
like image 104
romainl Avatar answered Jan 20 '23 11:01

romainl