3

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.

3 Answers 3

3

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.

1
  • Note that the value of INSIDE_EMACS varies depending on the Emacs command which was used. e.g. M-x shell and M-x term produce different values. You can use this to further differentiate between those types (which are, of course, very different to one another).
    – phils
    Sep 25, 2018 at 23:00
2

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
2
  • thanks for your prompt answer. Somehow I still get the same warnings when calling the shell from emacs. If I then echo $EMACS, I noticed the variable is not assigned. (bash 4.2)
    – Daniel
    Dec 9, 2016 at 9:48
  • @dangom I rejected your edit only because I don't know emacs very well and just saw the reference to EMACS in the bash man page. Feel free to post your edit as a new answer if that worked for you.
    – chepner
    Dec 9, 2016 at 14:06
1

This snippet specifically tests if line editing is enabled in bash or not. It works everywhere, not just in the emacs shell:

if [[ "$(set -o | grep 'emacs\|\bvi\b' | cut -f2 | tr '\n' ':')" != 'off:off:' ]]; then
  echo "line editing is on"
fi

It could probably be simplified...

Your Answer

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

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