3

I am using global-linum-mode for line numbers. It would be nice if the line number of the current line was highlighted with different color (and/or also different background). Anybody has an idea how to achieve this?

Thank you!

user673592
  • 2,090
  • 1
  • 22
  • 37

3 Answers3

8

I've derived this answer from my previous answer to Relative Line Numbers In Emacs, as it deals with the same issue of remembering the current line number during the linum format process.

I'm inheriting from the linum face, but using the background colour from hl-line. If the foreground and background don't match nicely, you can assign a foreground colour explicitly with
M-x customize-face RET my-linum-hl RET

(require 'hl-line)

(defface my-linum-hl
  `((t :inherit linum :background ,(face-background 'hl-line nil t)))
  "Face for the current line number."
  :group 'linum)

(defvar my-linum-format-string "%3d")

(add-hook 'linum-before-numbering-hook 'my-linum-get-format-string)

(defun my-linum-get-format-string ()
  (let* ((width (1+ (length (number-to-string
                             (count-lines (point-min) (point-max))))))
         (format (concat "%" (number-to-string width) "d")))
    (setq my-linum-format-string format)))

(defvar my-linum-current-line-number 0)

(setq linum-format 'my-linum-format)

(defun my-linum-format (line-number)
  (propertize (format my-linum-format-string line-number) 'face
              (if (eq line-number my-linum-current-line-number)
                  'my-linum-hl
                'linum)))

(defadvice linum-update (around my-linum-update)
  (let ((my-linum-current-line-number (line-number-at-pos)))
    ad-do-it))
(ad-activate 'linum-update)

As with that other answer, this is more efficient in generating the dynamic width than the default dynamic format, but you can use a static width for maximum speed by commenting out the line (add-hook linum-before-numbering-hook 'my-linum-get-format-string) (and optionally modify the initial value of my-linum-format-string to set your preferred width).

Community
  • 1
  • 1
phils
  • 71,335
  • 11
  • 153
  • 198
  • Thanks! Excellent work! BTW - the idea is to use it *instead* hl-line (or with underlining of the current line instead of highlighting). – user673592 May 15 '12 at 06:47
  • I thought that might be the case. As-is, this will work happily with or without `hl-line` active, but if you wanted to avoid even loading the `hl-line` library, you could delete the `(require 'hl-line)` and change `,(face-background 'hl-line nil t)` to a hard-coded value. – phils May 15 '12 at 08:24
  • Because of an implementation detail of `linum` the same buffer in multiple windows will share the current line highlight. The points in each window remain correct, so whichever window is current will have the correct highlight. Fixing this would require a change to linum and how it caches/reuses overlays in a buffer local variable. If someone decides to tackle this you might also extend linum to track the current line and pass it to `linum-format` :) –  Mar 30 '15 at 10:58
8

It can be achieved with hlinum extension.

(require 'hlinum)
(hlinum-activate)

You can change linum-highlight-face to customize background and foreground colors.

Aydin Han
  • 81
  • 1
  • 1
2

I am not sure about line numbers, but you can use global-hl-line-mode to highlight the current line.

choroba
  • 231,213
  • 25
  • 204
  • 289