Is there an easy way to highlight all the words in a line with just one command? I'm trying to write an elisp function to achieve this but my knowledge is pretty rudimentary. So far, I can only move the point to the beginning of the line and highlight the word there:

(defun hx ()
  "Highlight all words in a line."
  ;; Split string based on comma-separator
  (let ((mylist (split-string (thing-at-point 'line t) ","))))


I was hoping to iterate over mylist and use re-search-forward to find the first occurrence of each word in the list, move the point to that word and then highlight it. However, mylist contains an empty list. Even if mylist contained a list of words, I don't know if the above approach would actually work.

1 Answer 1


mylist is only usable inside the body of let. your (highlight-symbol-at-point) is not inside of this let

Btw. highlight-symbol-at-point is just a alias for hi-lock-face-symbol-at-point (use M-. to get to the source code).

So I took the source code of latter and your code and combined it into a working version:

(defun hx ()
  "Highlight all words in a line."
  ;; Split string based on comma-separator
  (let ((mylist (split-string (thing-at-point 'line t) ",")))
    (dolist (item mylist)
      (let ((hi-lock-auto-select-face t)
            (face (hi-lock-read-face-name)))
        (or (facep face) (setq face 'hi-yellow))
        (unless hi-lock-mode (hi-lock-mode 1))
        (hi-lock-set-pattern item face)))))

Note, that you are splitting that line at ,, not space. (beginning-of-line) is not needed.

Note, you might need to (require 'hi-lock) before using this function.

Note, this function uses (with default Emacs settings) different colors for every (different) string. It is asking which color to use at the minibuffer.

  • Thanks. Is it possible to set a different color for each of the underlying strings in the line? I should've made this explicit in my question.
    – linuxfan
    Mar 14, 2021 at 4:23
  • It is, using different colors, at least with standard emacs config. It prompts at the minibuffer, which color it should use for which string.
    – jue
    Mar 14, 2021 at 10:03

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.