Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.bashrc : how to check in what terminal the shell is running

Tags:

bash

shell

emacs

I have the following in my .bashrc:

bind '"\e[A"':history-search-backward
bind '"\e[B"':history-search-forward

However, when I call shell from within Emacs, I get the following message:

bash: bind: warning: line editing not enabled

bash: bind: warning: line editing not enabled

And as a consequence my prompt gets messed up.

How can I detect (from within my .bashrc), that my shell is being called from emacs or, alternatively, not from a standard terminal?

My goal is to wrap the calls to bind so that they are only executed in a proper terminal.

like image 480
dangom Avatar asked Dec 06 '25 19:12

dangom


2 Answers

Probing for a variable called EMACS didn't work for me under Emacs 25 and bash 4.2.

However, looking for differences in the environment of the shell within and outside of Emacs I found a variable called INSIDE_EMACS only set when running from Emacs.

The solution that worked for me is therefore:

if [[ ! -v INSIDE_EMACS ]]; then
    bind '"\e[A"':history-search-backward
    bind '"\e[B"':history-search-forward
fi

Echoing INSIDE_EMACS returns the Emacs release number.

like image 120
dangom Avatar answered Dec 09 '25 14:12

dangom


bash disables line editing because when it sees a variable named EMACS in its environment. You can use the same variable to conditionally create those bindings:

if [[ ! -v EMACS ]]; then
    bind '"\e[A"':history-search-backward
    bind '"\e[B"':history-search-forward
fi
like image 23
chepner Avatar answered Dec 09 '25 14:12

chepner