10

I would like to use global-hl-line-mode, but some of my themes lack a good highlight face. Is there a general way to define one (that's not inverse-color)? I would like to maintain syntax highlighting, which has broken in my attempts.

1
  • What exactly do you mean by "way to define"? Are you asking about suggestions for the appearance? About how to define a face that gives a given appearance? About how to make a face definition supersede the effect of a theme's face definitions? Have you tried customizing the face used by global-hl-line-mode? In sum, please specify the question more clearly - it will help people help you.
    – Drew
    Mar 2, 2015 at 16:07

1 Answer 1

18

This is really a matter of opinion, but I have always found that that making the hl-line have a slightly darker background than the default is best.

I would like to maintain syntax highlighting, which has broken in my attempts.

If you want to maintain syntax highlighting, make sure that hl-line face's :foreground attribute is nil, then set the :background attribute to what you want.

Here is what it looks like for me with these settings:

(set-face-attribute 'default nil :background "gray15")
(set-face-attribute 'hl-line nil :foreground nil :background "gray5")

enter image description here

By the sound of it, you don't want hard coded colors because you will be changing themes. You can have emacs automatically set the :background of the hl-line face to a color slightly darker than the current default :background like this:

(require 'color)

(defun set-hl-line-color-based-on-theme ()
  "Sets the hl-line face to have no foregorund and a background
    that is 10% darker than the default face's background."
  (set-face-attribute 'hl-line nil
                      :foreground nil
                      :background (color-darken-name (face-background 'default) 10)))

(add-hook 'global-hl-line-mode-hook 'set-hl-line-color-based-on-theme)

This will set the face's background to a color 10% darker than the default background anytime you enable global-hl-line-mode

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.