I have following definitions in my .emacs file for keyword coloring in shell-mode:

(add-hook 'shell-mode-hook 'ansi-color-for-comint-mode-on)

(font-lock-add-keywords 'shell-mode '(("alias " . font-lock-builtin-face)))
(font-lock-add-keywords 'shell-mode '(("mv " . font-lock-builtin-face)))
(font-lock-add-keywords 'shell-mode '(("rm " . font-lock-builtin-face)))
(font-lock-add-keywords 'shell-mode '(("cp " . font-lock-builtin-face)))

It successfully color those keywords but it also colors them if they are end of a word(string).


For git clone https://github.com/SchedMD/slurm it colors rm at the end:

enter image description here

Would it be possible to prevent this, where I just want to highlight the keyword?

Related: shell-mode alias keyword is not recognized as font-lock-builtin-face type

1 Answer 1


font-lock-add-keywords takes a list of regular expressions, not merely strings. You can use the regular expression syntax to match more complicated constructs. In particular, you can match word boundaries with \b. Try this instead:

(font-lock-add-keywords 'shell-mode '(("\\brm\\b" . font-lock-builtin-face)))

For more information on regular expressions, see chapter Backslash Constructs in Regular Expressions in the Emacs Lisp manual. Don’t forget that you can open the manuals inside of Emacs using C-h i.

Also, just a head’s up; this function takes a list of multiple regexes. Instead of calling it four times, you can call it with one list of four things:

(font-lock-add-keywords 'shell-mode '(("\\balias\\b" . font-lock-builtin-face)
                                      ("\\bmv\\b" . font-lock-builtin-face)
                                      ("\\brm\\b" . font-lock-builtin-face)
                                      ("\\bcp\\b" . font-lock-builtin-face)))

Because it takes regular expressions, you can also use alternations to express the same thing:

(font-lock-add-keywords 'shell-mode '(("\\b\\(alias\\|mv\\|rm\\|cp\\|\\)\\b" . font-lock-builtin-face)))

Finally, there is a new syntax for regular expressions which is less traditional, but is often easier to read and maintain:

(font-lock-add-keywords 'shell-mode `((,(rx word-boundary
                                            (or "alias" "mv" "rm" "cp")
                                       . font-lock-builtin-face)))

This is documented in chapter 34.3.3 The rx Structured Regexp Notation of the Emacs Lisp manual.

  • For the last solution, I am getting following error: Error during redisplay: (jit-lock-function 3013) signaled (invalid-function (rx word-boundary (or "alias" "unalias" "source" "mkdir" "mv" "rm" "cp") word-boundary)) is it normal?
    – alper
    Dec 10, 2021 at 9:22
  • Oops, you’re right; I quoted it wrong! I will fix it.
    – db48x
    Dec 10, 2021 at 12:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.