9

I use (global-hl-line-mode 1) in order to highlight the current line. This changes the text colours of the highlighted line. Is it possible to highlight while keeping the current syntax highlighting colours (so changing only the background)?

2
  • 1
    Possible duplicate of hl-line-mode hide background, how to avoid this?
    – Drew
    Commented Oct 14, 2016 at 23:02
  • No. Overlay highlighting (which is used by hl-line-mode) always overrides text-property highlighting (e.g. font-lock). See the question this is a duplicate of.
    – Drew
    Commented Oct 14, 2016 at 23:03

3 Answers 3

8

Just for reference, try

(set-face-attribute 'hl-line nil :inherit nil :background "gray6")
2
  • 1
    Please consider elaborating, explaining how this answers the question.
    – Drew
    Commented Jun 8, 2018 at 14:05
  • It does not work for me
    – Nisba
    Commented Jun 8, 2018 at 17:08
5

This is the case by default for me: hl-line-mode by default uses a face which only specifies a background color. That face is hl-line which by default just inherits from highlight. So maybe the problem is simply your that highlight face specifies both a background and a foreground color. I recommend you M-x customize-face and either change your highlight face or your hl-line face so as to keep its foreground color unspecified.

5

This works for me, with this in my .emacs. See how the syntax colours are nicely preserved? (thx Yadoo86)

;; highlight line with the cursor, preserving the colours.
(set-face-attribute 'hl-line nil :inherit nil :background "gray80")
(global-hl-line-mode 1)

enter image description here

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.