Emacs, How to change some colors in M-x shell?
Asked Answered
C

3

8

I use Emacs 24 and want to change a color of dirs and files while I'm in shell-mode (ls command). Ideally - depending on rights of the file.

How can i do this?

I tried playing with

(setq ansi-color-names-vector
      ["black" "red" "green" "yellow" "PaleBlue" "magenta" "cyan" "white"])
(add-hook 'shell-mode-hook 'ansi-color-for-comint-mode-on)

But I think it doesn't work for me.
I have solarized color scheme now. But I do not want to change it's colors, only for shell-mode.

Edit:
When I change colors for my system terminal (using .dircolrs file in my home directory) - emacs reads it, BUT it(emacs) slightly changes colors - it makes a color a bit darker or lighter in shell-mode.

And I don't know why Emacs does it.

Different dirs colors (games, Public...:
This is system terminal: enter image description here
And this is Emacs: enter image description here

These are changed colors, with default colors and solarized-theme Emacs made blue dirs on a blue background.

I understand that it's not a big problem to find a hack, just want to know why Emacs changes colors a bit.

Cott answered 7/12, 2012 at 13:19 Comment(4)
My emacs shell uses the font colors set by zsh with the emacs buffer background color. Is this not what your seeing?Karns
@AndrewMyers, I think yes, then I need to change colors in bash config. Actually I wanted to know if it can be done in emacs.Cott
I see, if you do M-x customize-group ansi-colors do you get a customizable group? I see the ansi-color-names-vector there.Karns
@AndrewMyers, yes just checked them, but emacs ignores them, still in shell-mode there are different colors, see edit..Cott
H
6

Here is what I use to set Solarized colors for emacs. I went ahead and posted my whole ansi-term config in case any other settings might be useful.

(use-package ansi-term
  :defer t
  :init
  (progn

    ;; ;; Use variable width font faces in current buffer
    (defun my-buffer-face-mode-variable ()
    ;;   "Set font to a variable width (proportional) fonts in current buffer"
      (interactive)
      (setq buffer-face-mode-face '(:family "Menlo For Powerline" :height 100))
      (text-scale-adjust 1)
       (buffer-face-mode))

    (setq system-uses-terminfo nil)
    (add-hook 'term-mode-hook
              '(lambda ()
                 (linum-mode 0)
                 (term-set-escape-char ?\C-z)
                 (term-set-escape-char ?\C-x)
                 (define-key term-raw-map "\C-c" 'term-interrupt-subjob)
                 (define-key term-raw-map (kbd "M-x") 'execute-extended-command)
                 (setq autopair-dont-activate t)
                 (setq ac-auto-start nil)
                 (visual-line-mode -1)
                 ;; (my-buffer-face-mode-variable)
                 ))

    (defun my-term-paste (&optional string)
      (interactive)
      (process-send-string
       (get-buffer-process (current-buffer))
       (if string string (current-kill 0))))

    (defun my-term-pasteboard-paste ()
      (interactive)
      (process-send-string
       (get-buffer-process (current-buffer))
       (ns-get-pasteboard)))

    (add-hook 'term-exec-hook '(lambda ()
                                 (set-buffer-process-coding-system 'utf-8-unix 'utf-8-unix)
                                 (goto-address-mode)
                                 (define-key term-raw-map (kbd "C-y") 'my-term-paste)
                                 (define-key term-raw-map (kbd "s-v") 'my-term-pasteboard-paste)
                                 (let ((base03 "#002b36")
                                       (base02 "#073642")
                                       (base01 "#586e75")
                                       (base00 "#657b83")
                                       (base0 "#839496")
                                       (base1 "#93a1a1")
                                       (base2 "#eee8d5")
                                       (base3 "#fdf6e3")
                                       (yellow "#b58900")
                                       (orange "#cb4b16")
                                       (red "#dc322f")
                                       (magenta "#d33682")
                                       (violet "#6c71c4")
                                       (blue "#268bd2")
                                       (cyan "#2aa198")
                                       (green "#859900"))
                                   (setq ansi-term-color-vector
                                         (vconcat `(unspecified ,base02 ,red ,green ,yellow ,blue
                                                                ,magenta ,cyan ,base2))))))
Hatshepsut answered 7/12, 2012 at 18:3 Comment(1)
thanks for the answer, it would be very helpful for me, but I just found what solved the problem (at least I can manually set colors now)Cott
T
8

M-x customize-variable RET ansi-color-names-vector RET

Existing text will not have been affected, but newly printed text (even in the same shell buffer) will show the new colors.

Tierell answered 4/12, 2014 at 13:6 Comment(1)
I think this is the best answer. After customizing the variable and saving it you can inspect your .emacs file to see how the customization was performed and saved.Renny
C
7

Somewhere I found this, and it helped me (I'm a complete newbie, but I thought thatsetq ansi-color-names-vector would be enough):

(setq ansi-color-names-vector
      ["black" "tomato" "PaleGreen2" "gold1"
       "DeepSkyBlue1" "MediumOrchid1" "cyan" "white"])
(setq ansi-color-map (ansi-color-make-color-map))  ;; helped line

Now ansi-color-names-vector works in shell.

Cott answered 7/12, 2012 at 18:41 Comment(2)
do you know by any chance if there is a way to interactively test those color mappings? I.e. instead of having to restart shell after every changeGuay
@dolzenko, Don't know how to do thatCott
H
6

Here is what I use to set Solarized colors for emacs. I went ahead and posted my whole ansi-term config in case any other settings might be useful.

(use-package ansi-term
  :defer t
  :init
  (progn

    ;; ;; Use variable width font faces in current buffer
    (defun my-buffer-face-mode-variable ()
    ;;   "Set font to a variable width (proportional) fonts in current buffer"
      (interactive)
      (setq buffer-face-mode-face '(:family "Menlo For Powerline" :height 100))
      (text-scale-adjust 1)
       (buffer-face-mode))

    (setq system-uses-terminfo nil)
    (add-hook 'term-mode-hook
              '(lambda ()
                 (linum-mode 0)
                 (term-set-escape-char ?\C-z)
                 (term-set-escape-char ?\C-x)
                 (define-key term-raw-map "\C-c" 'term-interrupt-subjob)
                 (define-key term-raw-map (kbd "M-x") 'execute-extended-command)
                 (setq autopair-dont-activate t)
                 (setq ac-auto-start nil)
                 (visual-line-mode -1)
                 ;; (my-buffer-face-mode-variable)
                 ))

    (defun my-term-paste (&optional string)
      (interactive)
      (process-send-string
       (get-buffer-process (current-buffer))
       (if string string (current-kill 0))))

    (defun my-term-pasteboard-paste ()
      (interactive)
      (process-send-string
       (get-buffer-process (current-buffer))
       (ns-get-pasteboard)))

    (add-hook 'term-exec-hook '(lambda ()
                                 (set-buffer-process-coding-system 'utf-8-unix 'utf-8-unix)
                                 (goto-address-mode)
                                 (define-key term-raw-map (kbd "C-y") 'my-term-paste)
                                 (define-key term-raw-map (kbd "s-v") 'my-term-pasteboard-paste)
                                 (let ((base03 "#002b36")
                                       (base02 "#073642")
                                       (base01 "#586e75")
                                       (base00 "#657b83")
                                       (base0 "#839496")
                                       (base1 "#93a1a1")
                                       (base2 "#eee8d5")
                                       (base3 "#fdf6e3")
                                       (yellow "#b58900")
                                       (orange "#cb4b16")
                                       (red "#dc322f")
                                       (magenta "#d33682")
                                       (violet "#6c71c4")
                                       (blue "#268bd2")
                                       (cyan "#2aa198")
                                       (green "#859900"))
                                   (setq ansi-term-color-vector
                                         (vconcat `(unspecified ,base02 ,red ,green ,yellow ,blue
                                                                ,magenta ,cyan ,base2))))))
Hatshepsut answered 7/12, 2012 at 18:3 Comment(1)
thanks for the answer, it would be very helpful for me, but I just found what solved the problem (at least I can manually set colors now)Cott

© 2022 - 2024 — McMap. All rights reserved.