Is there a hook which runs when the cursor position changes its current line numbers?

For a concrete toy problem, can i highlight the current line as yellow when the line number of the current cursor position is always a multiple of three. e,g. when the cursor is on line 3 or 6 or 9, that line is highlighted as yellow. When the cursor moves to another line, the highlighted line gets unhighlighted.

  • Not as far as I can tell, here's a similar question: emacs.stackexchange.com/q/20596/563
    – wvxvw
    Nov 19, 2018 at 6:03
  • Your answer will depend upon whether you are using the built-in native line numbers that became available in Emacs 26.1+, or whether you are generating line numbers with overlays such as linum.el or Stefan's nlinum library. If the latter, you can modify and do anything you want in Lisp (though you may slow it down considerably). If the former, then you are stuck with what is written in C unless you want to hack the code and rebuild from source.
    – lawlist
    Nov 19, 2018 at 6:11

1 Answer 1


I don't think there is, but perhaps you can add one yourself?

(defvar current-line-number (line-number-at-pos))

(defvar changed-line-hook nil)

(defun update-line-number ()
  (let ((new-line-number (line-number-at-pos)))
    (when (not (equal new-line-number current-line-number))
      (setq current-line-number new-line-number)
      (run-hooks 'changed-line-hook))))

(add-hook 'post-command-hook #'update-line-number)

;; Example of hook usage

(defun my-line-func ()
  (message "This is the current line: %s" current-line-number))

(add-hook 'changed-line-hook #'my-line-func)

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.