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?

4 Answers 4


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.

  • 4
    Awesome, this worked. I simply set it up to display {vim} in my PS1 if VIMRUNTIME is defined. Thanks!
    – Wuffers
    Oct 17, 2010 at 17:39

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
    Oct 17, 2010 at 17:16
  • @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
    Oct 17, 2010 at 17:29

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)
if [ "$(ps -o tty= -p $$)" = "$(ps -o tty= -p $PPID)" ]; then
  # Not running directly under a terminal emulator

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
    Oct 17, 2010 at 17:35
  • @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? Oct 17, 2010 at 17:41
  • I am running Mac OS X. And, if it matters, I am using the provided Terminal.app.
    – Wuffers
    Oct 17, 2010 at 18:02
  • @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. Oct 17, 2010 at 18:27
  • Thanks, that fixed the errors, but now when I am in the vim shell, it only shows {} instead of {vim}.
    – Wuffers
    Oct 17, 2010 at 18:32

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .