0

I were following the Modern Vim book by Drew Neil. In tip 20 he writes:

At a glance, you may not be able to tell whether a shell is running within a terminal buffer or outside of Neovim. In this tip, you’ll find out how you can customize the prompt for your shell so that it appears differently inside of a terminal buffer.

. . .

When Neovim starts up, it sets the $NVIM_LISTEN_ADDRESS environment variable. In the start up script for your shell, you can test for the presence of this variable. If $NVIM_LISTEN_ADDRESS is set, then you can assume that the shell is running inside of a terminal buffer. Try adding this snippet to your bashrc file:

if [ -n "$NVIM_LISTEN_ADDRESS" ]; then
    export PS1="» "
else
    export PS1="\$ "
fi

I found that $NVIM_LISTEN_ADDRESS variable is deprecated. I tried to echo it in neovim session, it is empty.

So there is a question, how to detect if zsh or bash session is run inside nvim? (:term command)

4
  • 2
    You can always set something yourself, e.g., alias nvim='INSIDE_NVIM=1 nvim' and then any terminals opened inside nvim will have $INSIDE_NVIM set for them.
    – muru
    Jun 30 at 2:59
  • Could be. I hope it won't interact with $VISUAL and other stuff :) Jun 30 at 4:08
  • 1
    You can also set the environment variable in nvim's config as well: let $INSIDE_NVIM = '1'
    – muru
    Jun 30 at 4:55
  • If you are not setting the $NVIM environment variable, you can use that one distinguish it. In general, you can just compare the output of env inside nvims terminal and outside nvims terminal. Just for reference, Vim exposes the VIM_TERMINAL environment variable exactly for this purpose. Jun 30 at 7:06

1 Answer 1

1

You can check using the following variables:

  • $NVIM/$VIM
  • $VIMRUNTIME

those are usually set by the running Vim/Neovim to detect from where to load the initial runtime files. However, for some rare cases (e.g. when Vim/Neovim is not able to figure this out itself), you may set those variable to point Vim/Neovim from where to load its configuration. But usually this should not be necessary.

Therefore Vim also provides the $VIM_TERMINAL variable, exactly for this purpose, which also gives you the main Vim version under which the shell is run. See :h terminal-unix

1
  • Really, $NVIM and $VIMRUNTIMEare set only for current nvim instance and do not influence other zsh sessions. Thanks! Jul 1 at 13:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.