Cedric Martin
Cedric Martin

Reputation: 6014

How to use font-lock-add-keywords inside an ansi-term?

I can easily add keywords that I want to highlight to, say, lisp-interaction-mode, by doing the following:

(font-lock-add-keywords 'lisp-interaction-mode '(("foo" (0 '(font-lock-warning-face)))))

This kicks in as soon as I (re)open any buffer in lisp-interaction-mode.

However I can't figure out how to use font-lock-add-keywords inside an ansi-term buffer.

I don't know if I get my mode wrong or if there's something "special" about term / ansi-term buffers that would prevent the above from working.

I tried with "term-mode", which is apparently the major mode ansi-term is using, but it doesn't seem to work.

How can I use font-lock-add-keywords inside an ansi-term?

Upvotes: 0

Views: 456

Answers (1)

This should work:

(defun highlight-foo ()
  (font-lock-add-keywords
   nil ;; putting nil here adds the keywords only to the current buffer
   '(("foo" 0 font-lock-warning-face))))

(add-hook 'term-mode-hook 'highlight-foo)

However, please note that while font-lock-mode is active with such settings, regular ANSI escape sequences won't have any effect on the colors in the terminal (e.g. the output from ls will not be colorized).

Upvotes: 0

Related Questions