I would like to execute a script [...] every time I enter M-x shell
This is supported as standard.
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
.
-- C-hig (emacs)Interactive Shell
I agree with @stsquad that testing [ "$TERM" = "dumb" ]
makes good sense (as your use-case applies to any dumb terminal, not just in Emacs); but if you particularly wanted an Emacs-specific prompt, you can also do that within your normal shell config code:
When you are setting your normal fancy prompt you can test the environment variable INSIDE_EMACS
. For M-x shell
the value will be along the lines of 26.1,comint
and the comint
part tells you that you're in a dumb terminal and should set a simpler prompt.
# Set simpler prompt for M-x shell inside Emacs.
if [ "${INSIDE_EMACS%,comint}" != "$INSIDE_EMACS" ]; then
PS1='\u@\h:\w\$'
fi
Note that other values of INSIDE_EMACS
are possible in other circumstances. In M-x term
(which isn't dumb) I see 26.1,term:0.96
, for example.