1

By default font-lock-variable-name-face is defined as follows:

'(font-lock-variable-name-face ((t (:foreground "black" :weight bold))))

Would it be possible to change its color for different modes such as for shell-mode:

I have tried the following, which did not have any effect:

(add-hook 'shell-mode-hook
          (lambda ()
            '(font-lock-variable-name-face ((t (:foreground "LightGoldenrod" :weight bold))))))

dracula-theme.el is located under ~/.emacs.d/themes: https://github.com/dracula/emacs/blob/master/dracula-theme.el

minimal.el:

(add-to-list 'custom-theme-load-path "~/.emacs.d/themes")
(load-theme 'dracula t)

(defun my-shell-mode-faces ()
  (face-remap-add-relative 'font-lock-variable-name-face '(:foreground "LightGoldenrod" :weight bold)))

(add-hook 'shell-mode-hook 'my-shell-mode-faces)
alper
  • 1,238
  • 11
  • 30
  • Can you give us an example of a shell command that produces output in a `shell-mode` buffer with the `font-lock-variable-name-face`? I tried a few things like `make`, `ls`, `grep`, and `vi` of a few different files -- but none of those produce output with the `font-lock-variable-name-face`. – lawlist Aug 02 '21 at 15:06
  • Ah I was just opening a file ending with `sh` like: `test.sh` . Its just a `Shell-script` file. Example file can contain something like: `#!/bin/bash RED="\033[1;31m"` where `RED`should be in `LightGoldenrod ` color – alper Aug 02 '21 at 15:42
  • 1
    **STEP #1**: Create a file called `test.sh`, with the first line being `#!/bin/bash RED="\033[1;31m"` **STEP #2**: Open the file `test.sh` and place the cursor on the word `RED` in the first line and type `C-u C-x =` and a `*Help*` buffer pops up telling us that this is `font-lock-comment-face`. **STEP #3**: From the buffer containing the `test.sh` file, type `C-h m`, which is also known as `M-x describe-mode` -- this opens a `*Help*` buffer that tells us this is shell-script mode defined in `sh-script.el` -- follow the link for `sh-script.el` in the `*Help*` buffer .... – lawlist Aug 02 '21 at 16:08
  • 1
    [... continued from previous comment] and we are transported to the definition for `sh-mode`. Based thereon, we conclude that the major-mode hook we are interested in is `sh-mode-hook`. **CONCLUSION**: From steps 1 to 3 above, we learned that the face is `font-lock-comment-face` and the major-mode hook is `sh-mode-hook`. Therefore, adjust the example in the answer below accordingly to suit your own needs using the relevant face and the relevant hook. – lawlist Aug 02 '21 at 16:10

1 Answers1

4

My previous answer in this forum to a similar thread did not include an example using a major-mode hook How to modify-face for a specific buffer? , so I chose to write up this new answer.

The function face-remap-add-relative sets the variable face-remapping-alist (responsible for buffer-local faces), which the manual recommends be set using the aforementioned function:

https://www.gnu.org/software/emacs/manual/html_node/elisp/Face-Remapping.html

(defun my-shell-mode-faces ()
  (face-remap-add-relative 'font-lock-variable-name-face '(:foreground "LightGoldenrod" :weight bold)))

(add-hook 'shell-mode-hook 'my-shell-mode-faces)
lawlist
  • 18,826
  • 5
  • 37
  • 118
  • I have tried it but it did not make any color change in the shell-mode :-( Could something overwrite into it? I was loading another theme before `(load-theme 'dracula t)`, seems like it prevents it ? – alper Aug 01 '21 at 13:13
  • Before posting the answer, I tested it (Emacs 27, in a `shell-mode` buffer) with the `default` face to verify that it worked. If you can come up with a minimal working example that produces the particular face at issue within a `shell-mode` buffer, either myself or another forum participant might be able to see what the problem might be. – lawlist Aug 01 '21 at 15:19
  • Thanks, I was not able to figure out the problem :-( I have updated my question with the minimal.el. Basically I am loading `dracula-theme.el` right before applying your answer. Please note that I am using `emacs 26.3` but not sure its version will cause it – alper Aug 02 '21 at 13:17