Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Colors in Emacs shell prompt

Tags:

emacs

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.

like image 885
Justin Schell Avatar asked Sep 13 '14 01:09

Justin Schell


People also ask

What Is syntax of the colorized bash?

Here's what you need to know: You must include the entire color code information between the \[ and \] character s. Inside the tag, you must begin with either \033[ or \e[ to indicate to Bash that this is color information. Both \033[ and \e[ do the same thing.

How do I use Emacs shells?

7.7 How do I use a shell in Emacs? You can start an interactive shell in Emacs by typing M-x shell . By default, this will start the standard Windows shell cmd.exe . Emacs uses the SHELL environment variable to determine which program to use as the shell.


2 Answers

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.

like image 138
Justin Schell Avatar answered Sep 18 '22 01:09

Justin Schell


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)))
like image 26
memeplex Avatar answered Sep 18 '22 01:09

memeplex