6
4
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?
6
4
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?
17
env | 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.
5
I type ps
(without any options) and see if vim is listed.
at least use pstree, if there is more than one instance of vim. – Benoit – 2010-10-17T17:16:56.607
@Benoit: 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. – garyjohn – 2010-10-17T17:29:53.363
0
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?.
Whenever I do this, and I am in the vim shell, I get {%cpu}
instead of {vim}. And If I'm not in vim, I get errors at login. – Wuffers – 2010-10-17T17:35:59.300
@Mr. Man: What I wrote should work under any POSIX-compliant OS (-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? – Gilles 'SO- stop being evil' – 2010-10-17T17:41:04.870
I am running Mac OS X. And, if it matters, I am using the provided Terminal.app. – Wuffers – 2010-10-17T18:02:06.543
@Mr. Man: My mistake, the 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. – Gilles 'SO- stop being evil' – 2010-10-17T18:27:38.663
Thanks, that fixed the errors, but now when I am in the vim shell, it only shows {}
instead of {vim}
. – Wuffers – 2010-10-17T18:32:58.987
0
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.
4Awesome, this worked. I simply set it up to display
{vim}
in my PS1 ifVIMRUNTIME
is defined. Thanks! – Wuffers – 2010-10-17T17:39:54.957