Tip 20Applying Customizations to Your Shell in a Terminal Buffer
images/neovim-only.png

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.

Open a shell in a terminal outside of Neovim and take note of how your prompt looks. Then launch Neovim with a shell running inside a terminal buffer:

=> $ nvim +terminal

Can you tell which shell is running inside of Neovim and which one is running outside? You could make it more obvious by changing how the prompt looks in each context. In the bash shell, this is done by setting the $PS1 environment variable.

Detecting That a Shell Is Running Inside 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

Try starting two new shells as you did earlier: one inside a terminal buffer, the other outside. You should now be able to tell the two apart just by looking at the prompt.

You can use this technique to set up environment variables and aliases, so that your shell behaves differently inside and outside of a terminal buffer. For another example of how this can be useful, read the next tip.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.102.225