2008-12-10

highlighting the current line

When editing text, I find it quite nice to highlight the current line, that is, the line where the cursor is, gets a different color. This can be easily done with emacs, by putting global-hl-line-mode (more about modes) and the following in your .emacs:
;; highlight the current line; set a custom face, so we can
;; recognize from the normal marking (selection)
(defface hl-line '((t (:background "Gray")))
  "Face to use for `hl-line-face'." :group 'hl-line)
(setq hl-line-face 'hl-line)
(global-hl-line-mode t) ; turn it on for all modes by default
What this does:
  1. first, we define a special font ('face' in emacs-jargon) and we call it hl-line; there is a special format for that, which is discussed in detail in the ELisp Manual;
  2. second, we assign this 'face' to the hl-line-face-variable; this determines the face that will be used for highlighting;
  3. third, we turn the highlighting on for all modes.
That is all. Now, some notes to this:
  • You can of course experiment with the font/face: you can change the foreground and background colors, make things bold or italic, bigger or smaller, etc. -- personally I like to keep things a bit subtle;
  • In some emacs-versions, hl-line-mode may not be available; if you need to use your .emacs which such an emacs, the entry on using functions only if they are available might come handy.

3 comments:

chris said...

Thanks, great tip!

FWIW, for color themes with black backgrounds (I'm using arjen), I like "Gray10" for my line highlighting.

Qarlo Clobreginny said...

This helps greatly the readability of wrapped lines. Fiddling with line highlights got to be a bit of a time sink when I discovered

M-x list-colors-display

Thanks for tip!

ahck said...

I like the way the command line in *shell* highlights. Thanks for the tip.