M-x all-things-emacs

Highlight the Current Line

April 9th, 2007 by Ryan McGeary · 22 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

22 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

  • 13 EMSTONE Crew » Blog Archive » emacs ??? ?? ? // Dec 28, 2008 at 4:31 am

    [...] ?? ???: http://www.emacsblog.org/2007/04/09/highlight-the-current-line [...]

  • 14 Joniale // May 6, 2009 at 12:11 pm

    Hi, a very good post, really thanks.

    I have questions. First,
    it doesnt work the GNU Emacs 21 built-in hl-line-mode in my xemacs i have seen that i have XEmacs 21.4 i have tried all that you said. I have also downloaded hl-line.el to try but it doesn´t work.
    I have tried also the visible bookmark and they don´t work i have an error define-fringe-bitmap is void.

  • 15 Highlight the Current Line | Best Eye Cream // Jun 8, 2009 at 2:40 am

    [...] Highlight the Current Line Posted by root 2 hours 22 minutes ago (http://www.emacsblog.org) Some color themes will reset the face colors for highlight and hl line can be affected by that leave a comment powered by wordpress styled by cutline Discuss  |  Bury |  News | Highlight the Current Line [...]

  • 16 trikapalanet // Sep 30, 2009 at 1:53 pm

    Thanks for the tip, works great!

  • 17 Dodge Boy // Oct 13, 2009 at 9:07 pm

    Great post! How did I ever live without this?

  • 18 Karthik // Feb 5, 2010 at 4:56 am

    Awesome, enabled it.

  • 19 Michael Norrish // Aug 24, 2010 at 4:03 am

    It seems that it has to be enabled with a positive arg in 23.2 (at least on MacOS).

    I.e.,

    (global-hl-line-mode 1)

    rather than

    (global-hl-line-mode)

  • 20 Philip Ganchev // Nov 13, 2010 at 7:17 am

    Thanks for the posting. I found it while searching the web on how to customize my Emacs 21 line highlight. I still can’t! It’s still plain gray, which is too dark on a white background. I tried:

    (set-face-background ‘highlight “goldenrod yellow”)

    (set-face-background ‘highlight “#F0F0F0”)

    (defface hl-line ‘((t (:background “gray90”)))
    “Face to use for `hl-line-face’.” :group ‘hl-line)

    And all those are shown fine with M-x list-colors-display .

  • 21 Daniel // Aug 15, 2013 at 10:40 pm

    Is it possible to have highlighted lines in other windows (besides the active one) using hl-line mode?

  • 22 Juneki Hong: Make a website they said. Real CS majors have websites they said. » Underline the current line in emacs // Jul 22, 2014 at 6:22 am

    […] Taken from: here […]