14

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.

3 Answers 3

12

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.

Sign up to request clarification or add additional context in comments.

1 Comment

That will not work in Ubuntu 16.04 that uses Emacs 24.5 due to Emacs bug debbugs.gnu.org/cgi/bugreport.cgi?bug=20084 that has already been fixed on git://git.sv.gnu.org/emacs.git at commit 792d44b3c31d2a682607ab8b79ae7d26b7402f41. To make your solution work, use gnu.org/software/emacs/manual/html_node/elisp/… with "comint" to fix "comint-snapshot-last-prompt" and "comint-output-filter" and with "font-lock" to add "font-lock--remove-face-from-text-property".
8

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))) 

Comments

0

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\]" 

1 Comment

Thanks, but it still gets set to the comint-highlight-prompt face. I have tried doing (set-face-foreground 'comint-highlight-prompt nil) in my .emacs file, but the prompt still gets set to all cyan.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.