Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to highlight a particular line in emacs?

I need to highlight facility for emacs in order to mark some lines in file while working with it. It should be smth like M-s h l but should work based on line number, not on a regexp. I want to highlight a current line, but the hl-line-mode is not suitable, as I need to highlight many lines, every time I press a specific key on each of them.

like image 312
Necto Avatar asked Jan 22 '13 08:01

Necto


People also ask

How do I highlight multiple lines in Emacs?

This is shift-select-mode , and it is enabled by default in Emacs 24+. On some (non-chiclet) keyboards, you should be able to hold down C-S- with a single pinky.

How do I change the highlight color in emacs?

To customize colors for color syntax highlighting, see the section on font-lock . To change the foreground or background color in Emacs through the windowing interface, you can use the menu commands Foreground Color->Other and Background Color->Other in the Edit->Text Properties menu.


2 Answers

I just quickly wrote the following:

(defun find-overlays-specifying (prop pos)
  (let ((overlays (overlays-at pos))
        found)
    (while overlays
      (let ((overlay (car overlays)))
        (if (overlay-get overlay prop)
            (setq found (cons overlay found))))
      (setq overlays (cdr overlays)))
    found))

(defun highlight-or-dehighlight-line ()
  (interactive)
  (if (find-overlays-specifying
       'line-highlight-overlay-marker
       (line-beginning-position))
      (remove-overlays (line-beginning-position) (+ 1 (line-end-position)))
    (let ((overlay-highlight (make-overlay
                              (line-beginning-position)
                              (+ 1 (line-end-position)))))
        (overlay-put overlay-highlight 'face '(:background "lightgreen"))
        (overlay-put overlay-highlight 'line-highlight-overlay-marker t))))


(global-set-key [f8] 'highlight-or-dehighlight-line)

(Here find-overlays-specifying came from the manual page)

It will highlight current line, and when used again it will remove it.

Maybe the following could be useful as well: removing all your highlight from the buffer (could be dangerous, you might not want it if you highlight important things)

(defun remove-all-highlight ()
  (interactive)
  (remove-overlays (point-min) (point-max))
  )

(global-set-key [f9] 'remove-all-highlight)
like image 102
PascalVKooten Avatar answered Sep 22 '22 12:09

PascalVKooten


You can use bm.el. You can install bm.el from MELPA.

bm.el provides bm-toggle to highlight and unhighlight current line. bm.el also provides bm-bookmark-regexp which highlights only matched lines. And you can jump between highlighted lines by bm-previous and bm-next

Following is sample configuration of bm.el

(require 'bm)
(global-set-key (kbd "<f5>") 'bm-toggle)
(global-set-key (kbd "<f6>") 'bm-previous)
(global-set-key (kbd "<f7>") 'bm-next)
(global-set-key (kbd "<f8>") 'bm-bookmark-regexp)
like image 25
syohex Avatar answered Sep 26 '22 12:09

syohex