M-x all-things-emacs

Highlight the Current Line

April 9th, 2007 by Ryan McGeary · 12 Comments

There are several ways to customize the display of the cursor. I happen to prefer highlighting the current line as a visual cue to keep my eye focused on where I’m working.

Highlight Current Line, 1

Before a few days ago, I had not realized there were so many packages to accomplish this, but let’s review a few.

highlight-current-line

I’ve used highlight-current-line for years. It’s a very adequate package. To enable, put highlight-current-line.el in your load-path and add the following to your emacs initialization:

(require 'highlight-current-line)
(highlight-current-line-on t)
 
;; To customize the background color
(set-face-background 'highlight-current-line-face "light yellow")

hl-line

To my surprise, I learned that GNU Emacs 21 introduced a built-in hl-line-mode. To enable it globally, add this to your emacs initialization:

(global-hl-line-mode 1)
 
;; To customize the background color
(set-face-background 'hl-line "#330")  ;; Emacs 22 Only
;(set-face-background 'highlight "#330")  ;; Emacs 21 Only

(Note the difference between customizing the color in Emacs 21 and 22.)

highline

Highline was inspired by hl-line, highlight-current-line, and linemenu. It is supposedly the best option for the XEmacs contingent. To enable, put highline.el in your load-path and add the following to your emacs initialization:

(require 'highline)
(highline-mode 1)
 
;; To customize the background color
(set-face-background 'highline-face "#222")

Even More

Other packages such as hl-line+, col-highlight, and crosshairs have extra features such as idle-timers, line flashing, and column highlighting. I found most of these a little over-the-top, but if your personal preferences differ, give them a try.

Background Colors

I use a black emacs background and prefer a deep-dark gray like #111 for highlighting to keep things subtle.

Highlight Current Line, 2

When on a white background, I prefer a light yellow such as lemon chiffon or light goldenrod yellow. What colors do you like?

Highlight Current Line, 3

Conclusion

Up until this article, I used highlight-current-line, but I just switched to using the built-in hl-line. It’s now one less dependency I need to manage, and hl-line happens to work better in conjuction with visible bookmarks.

Bibliography

1 EmacsWiki, HighlightCurrentLine — libraries that highlight the current line of characters.

2 GNU Emacs Manual, Displaying the Cursor

3 EmacsWiki, HighlightCurrentColumn — libraries that highlight the current column of characters.

Tags: misc · reviews

12 responses so far ↓

  • 1 colours // Apr 9, 2007 at 1:01 pm

    I’ve enabled the hl-line mode. Thanks!

    I notice that when the current line is highlighted, all the other faces are dissabled. So, in a C file, the keywords are no longer bold etc.

    Is there a way to keep all the other highlighting intact, and just shade the background slightly?

  • 2 Ryan McGeary // Apr 9, 2007 at 1:45 pm

    colours, It sounds like the foreground color for the hl-line (or highlight) face is set to something other than nil. To fix, try this:

    (set-face-foreground 'hl-line nil)  ;; Emacs 22
    ;(set-face-foreground 'highlight nil)  ;; Emacs 21
  • 3 colours // Apr 9, 2007 at 9:51 pm

    Thanks!

    FYI, on my emacs 23, (set-face-background ‘hl-line “#222”) works, but only (set-face-foreground ‘highlight nil) works to preserve the color-theme colors. Weird.

    (and you must do the above after loading the color theme :-)

  • 4 sec // Apr 9, 2007 at 11:40 pm

    One question: if i use global-hl-line-mode, is it possible to turn it off for certain modes? M-x hl-line-mode doesn’t seem to turn it off.

  • 5 Ryan McGeary // Apr 10, 2007 at 12:08 am

    sec, Using hl-line, you can set a buffer-local variable to turn it off for certain modes or buffers. For example:

    1
    2
    3
    4
    5
    6
    
    (defun local-hl-line-mode-off ()
      (interactive)
      (make-local-variable 'global-hl-line-mode)
      (setq global-hl-line-mode nil))
     
    (add-hook 'ruby-mode-hook 'local-hl-line-mode-off)

    This would turn off hl-line for all ruby buffers. You could also run M-x local-hl-line-mode-off to turn it off for a specific buffer.

    Alternatively, for highlight-current-line, it supports a regexp to disable highlighting for certain buffers. Buffers whose buffer-name match the regular expression in the customizable variable highlight-current-line-ignore-regexp do not highlight.

    The highline package also has support for turning off highlighting in local buffers (M-x highline-off).

  • 6 sec // Apr 10, 2007 at 12:32 am

    Thanks, works perfectly. I don’t want hl-mode in Erc-Buffers, so i hooked it up for that. You don’t need the lambda in add-hook I think.

  • 7 Ryan McGeary // Apr 10, 2007 at 1:12 am

    sec, Yes, you’re right. I cleaned up the hook above.

  • 8 KraYmer // Apr 10, 2007 at 12:31 pm

    Hi & thanx for the tip, but I don’t manage to preserve my color-theme colors.

    The 4 lines i’ve put in my .emacs :

    (global-hl-line-mode 1)
    (set-face-background 'hl-line "#222")
    (set-face-foreground 'highlight nil) 
    (set-face-foreground 'hl-line nil)

    The result : http://kraymer.free.fr/public/highlightline.tiff

    I’m using Emacs 22.0.92.1 (Carbon Version 1.6.0)

  • 9 Ryan McGeary // Apr 10, 2007 at 1:46 pm

    KraYmer, Be sure to load your color-theme before setting the face colors for hl-line or highlight. Some color-themes will reset the face colors for highlight and hl-line can be affected by that.

  • 10 Ryan McGeary // Apr 10, 2007 at 1:57 pm

    If anyone else is having trouble, you can double check your face values by running M-x describe-face. It will prompt you for a face name. Run it once for hl-line and again for highlight. Make sure that the foreground colors for both are unspecified.

    The reason some people have seen issues with highlight affecting things in Emacs 22 or 23 is because the hl-line face inherits from highlight.

  • 11 dude… » Blog Archive » zsh + screen + emacs ? 256 ?????????????? // May 29, 2007 at 2:25 pm

    [...] ??????????????????Highlight the Current Line?????highlight-current-line.el??????????????? [...]

  • 12 coldpeer // Jan 10, 2008 at 12:55 pm

    Thanks for this tip

Leave a Comment