Colors in Emacs shell prompt

2020-04-02 08:04发布

问题:

Is it possible to use colors in the Emacs shell prompt (the prompt itself, not the rest of the shell screen) with the bash PS1 variable? I believe I have the syntax correct (e.g., PS1='[\u@\h \[\e[0;31m\]\W\[\e[m\]]\$ ' - it works in term or ansi-term), but it seems like Emacs is applying the comint-highlight-prompt face. I can set that to some color, and it works, but I want to be able to set individual parts of the prompt to different colors. I prefer using shell over term or ansi-term, so I'd rather not switch.

Thanks.

回答1:

Figured it out: The comint-highlight-prompt face was set to inherit from minibuffer-prompt, which was setting the :weight, :foreground and :background. Removing the inheritance prevented the colors set in PS1 from being overridden by the comint-highlight-prompt face. Added this to my .emacs file.

(set-face-attribute 'comint-highlight-prompt nil
                    :inherit nil)

Also, M-x customize-group <ret> font-lock-faces was helpful in figuring this out.



回答2:

I would recommend not to change the face globally (since there are many comint users besides shell mode) but in a buffer specific manner by setting a mode hook:

(add-hook 'shell-mode-hook
      (lambda ()
        (face-remap-set-base 'comint-highlight-prompt :inherit nil)))


回答3:

I use that to change the terminal color and the information in mac, maybe can help:

Cyan="$(tput setaf 6)"
NC="$(tput sgr0)" # No Color
export PS1="$Cyan.Where-> \w\n\\$\[$NC\]"


回答4:

You'll need to use ansi-term rather than shell for terminal colours.



标签: emacs