1

In the documentation for shell-mode:

Emacs sends the new shell the contents of the file ~/.emacs_shellname as input, if it exists, where shellname is the name of the file that the shell was loaded from. For example, if you use bash, the file sent to it is ~/.emacs_bash. If this file is not found, Emacs tries with ~/.emacs.d/init_shellname.sh.

Everything works as expected when I enter shell-mode with M-x shell. However, this doesn't work when I call term or ansi-term in Emacs. I have both ~/.emacs_bash and ~/.emacs.d/init_shellname.sh containing export PS1='\h:\W$ ', but the prompt is still bash-3.2$.

How to set a startup shell script for term-mode?

3

If you run bash in a term buffer, bash will do all of its normal bash initialisation things, just as it would when you run it inside any other terminal emulator.

So you can set your prompt in your ~/.bashrc file, no differently to the way you do it without Emacs.

If you particularly wish to detect that the shell is running inside Emacs (in order to set some non-standard prompt, for instance), then there's an INSIDE_EMACS environment variable you can test for. e.g.:

if [ -n "$INSIDE_EMACS" ]; then
    # [...]
fi

These days the value of $INSIDE_EMACS varies in useful ways. e.g.:

M-x shell RET
$ echo $INSIDE_EMACS
25.2.1,comint

M-x ansi-term RET /bin/bash RET
$ echo $INSIDE_EMACS
25.2.1,term:0.96

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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