Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

emacs:highlight the current line by underline it

Tags:

emacs

In vim, we can use "set cursorline" in dotvim file to turn it on. Is there a way to do this in emacs?

like image 639
Joel Avatar asked Apr 27 '10 01:04

Joel


3 Answers

In your .emacs, customize the face for hl-line-mode, with something like:

(global-hl-line-mode 1)
(set-face-attribute hl-line-face nil :underline t)

hl-line-face is a variable that stores the name of the face hl-line-mode uses to show the current line. You can customize the :foreground :background and a bunch of other attributes to your liking. Check the docs here.

The global-hl-line-mode turns on highlighting the current line in all buffers. If you just want it in some buffers, turn it on with M-x hl-line-mode.

like image 183
Trey Jackson Avatar answered Sep 26 '22 17:09

Trey Jackson


There is a good blog post about this topic: M-x all-things-emacs

The following code (enter wit M-: or in ~/.emacs) uses the RGB code #222 as background color (if you are in 256-color mode!) and underlines the font on the current line. Unsetting the foreground color uses the default color, this e.g. preserves the C++ colours on the highlighted line.

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

You can check whether you need to change highlight or (afaik older) hl-line with M-x and:

describe-face <RET> highlight
describe-face <RET> hl-line

This command shows you all settings of the font face used for highlighting the current line. You should get an output like this:

Face: highlight (sample) (customize this face)
Documentation:
Basic face for highlighting.

        Family: unspecified
       Foundry: unspecified
         Width: unspecified
        Height: unspecified
        Weight: unspecified
         Slant: unspecified
    Foreground: unspecified
    Background: #222
     Underline: t
      Overline: unspecified
Strike-through: unspecified
           Box: unspecified
       Inverse: unspecified
       Stipple: unspecified
          Font: unspecified
       Fontset: unspecified
       Inherit: unspecified
like image 14
Simon A. Eugster Avatar answered Sep 24 '22 17:09

Simon A. Eugster


I don't think there's an exact equivalent built in. You can use hl-line-mode to highlight the current line, and customising that mode lets you set the highlighting to be underlining rather than the default different background colour -- but the underline you get stops at the end of the text in the line, rather than continuing to the edge of the window.

like image 2
Porculus Avatar answered Sep 23 '22 17:09

Porculus