0

I have changed the emacs-shell mode shell to WSL ubuntu but however, the prompt is not very user-readable. For this reason I would like to execute a script that changes it so that its more readable using:

PS1='\u@\h:\w\$'

every time I enter M-x shell At the same time, I would like to keep my prompt as is outside of emacs as it has color coding etc enabled so I want the change to only affect emacs hence my question. Is it possible?

  • This isn't quite a duplicate of either https://emacs.stackexchange.com/q/38775/454 or https://emacs.stackexchange.com/q/30669/454 but it's worth cross-referencing them both. – phils Mar 27 '19 at 21:18

2 Answers2

3

You can test the TERM environment variable in your bashrc file. For M-x shell it will be "dumb" as opposed to anything else. For ansi-term the TERM environment variable is set to "eterm-color".

stsquad
  • 4,626
  • 28
  • 45
  • Thanks for the answer. My problem is that I am unable to have Term run the wsl shell even though strangely I can do it for the shell command by changing explicit-shell variable. If you have managed to do it please advise. – Teererai Marange Feb 26 '19 at 20:12
  • 1
    @TeereraiMarange, this answer is not about any particular terminal emulator (I assume "Term" is a terminal emulator). The `TERM` environment variable will be available in your shell no matter which terminal you are using. e.g. `echo $TERM` from your shell prompt. You would test it in your shell init code, at the point where you are currently setting your fancy prompt. – phils Mar 27 '19 at 21:22
1

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.

phils
  • 48,657
  • 3
  • 76
  • 115