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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With