I am running OS X. On vim, if you do the :sh
command, you can drop to a shell to execute commands. I constantly forget whether I am in this shell or not.
Is there any way to check if I am in the shell coming from vim?
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.
Sign up to join this communityenv | grep vim
lists environment variables that vim passes to your shell. I doubt VIMRUNTIME
is defined if you haven't started your shell from vim.
{vim}
in my PS1 if VIMRUNTIME
is defined. Thanks!
I type ps
(without any options) and see if vim is listed.
ps
alone should be sufficient because by default it shows only those processes associated with the current terminal. At least that's the case on the Unix systems I use.
You can look at the command name of the shell's parent process: ps -o comm= -p $PPID
. You could for example change your prompt to include the parent process id.
Furthermore, if you only want to change your prompt in shells that are not running directly inside a terminal emulator, you can test this by checking whether the shell's controlling terminal is the same as its parent process's. If the parent is a terminal emulator, it won't have its own terminal as a controlling terminal.
For example, if you put the following lines in your ~/.kshrc
or ~/.bashrc
or ~/.zshrc
(pick the one appropriate for your shell), your prompt will begin with {vim}
if the shell is running under vim
:
parent_command=$(ps -o comm= -p $PPID)
parent_command=${parent_command##*/}
if [ "$(ps -o tty= -p $$)" = "$(ps -o tty= -p $PPID)" ]; then
# Not running directly under a terminal emulator
PS1="{$parent_command}$PS1"
fi
You may also be interested in some of the discussion on How to know the “level” of shells I am in?.
{%cpu}
instead of {vim}. And If I'm not in vim, I get errors at login.
-o cmd=
means not to display a header line, but getting %cpu
means that ps
did show a header line). However ps
differs a lot between platforms, maybe yours isn't 100% compliant. What OS is it?
Oct 17, 2010 at 17:41
comm
column is standard (and documented on OSX) but cmd
isn't. You might still need $(ps -o comm= -p $PPID | tail -n 1)
on OSX ≤10.4.
Oct 17, 2010 at 18:27
{}
instead of {vim}
.
You could use MacVim (http://code.google.com/p/macvim/). Whether or not your shell came from a vim instance becomes pretty obviously since your shell is in a MacVim window and not a Terminal window.