0

I'm new with emacs and I'd like to know how can we disable the highlight line mode (global-hl-line-mode) when we are in the VISUAL mode of Evil Mode. I find it really confusing when we start selecting a block with the hl-line activated, even if their background colors are different. Of course, I want the hl-line-mode activated again when we leave the VISUAL mode. Thanks.

EDIT: I tried this one and I was finally able to disable hl-line when in VISUAL mode.

(add-hook 'evil-visual-state-entry-hook (lambda () (setq-local global-hl-line-mode nil)))

But I could not enable it when I leave. I tried this but it didn't work:

(add-hook 'evil-visual-state-exit-hook (lambda () (global-hl-line-mode 1)))

EDIT: nevermind, this actually works: (add-hook 'evil-visual-state-entry-hook (lambda () (setq-local global-hl-line-mode nil)))

(add-hook 'evil-visual-state-exit-hook (lambda () (global-hl-line-mode nil)))

Walid Ber
  • 81
  • 1
  • 1
  • 4

1 Answers1

0

It might be better to disable hl-line-mode in the current buffer, rather than disable it globally:

(add-hook 'evil-visual-state-entry-hook (lambda() (hl-line-mode -1)))
(add-hook 'evil-visual-state-exit-hook (lambda() (hl-line-mode +1)))

Note that this may be a problem if you're not using global-hl-line-mode. hl-line-mode would be enabled each time you exit visual mode, whether or not hl-line-mode was on in the first place. This is what I hacked together to prevent that:

(defvar-local was-hl-line-mode-on nil)
(defun hl-line-on-maybe ()  (if was-hl-line-mode-on (hl-line-mode +1)))
(defun hl-line-off-maybe () (if was-hl-line-mode-on (hl-line-mode -1)))
(add-hook 'hl-line-mode-hook 
  (lambda () (if hl-line-mode (setq was-hl-line-mode-on t)))))

(add-hook 'evil-visual-state-entry-hook 'hl-line-off-maybe)
(add-hook 'evil-visual-state-exit-hook 'hl-line-on-maybe)

This way hl-line-mode won't be tampered with unless it was explicitly activated in the buffer beforehand, say, with a (add-hook 'python-mode-hook 'hl-line-mode).

EDIT: fixed the hl-line-mode hook in the second snippet.

Henrik
  • 982
  • 1
  • 8
  • 10
  • Thanks @Henrik L. You're right. Actually I'm disabling hl-line mode when I open for example ansi-term, so I don't want to enabe hl-line after I leave visual mode (I didn't notice that). But the snippet above doesn't seem to work. When I enter visual mode, the hl-line is still there... – Walid Ber Nov 13 '16 at 12:53
  • @WalidBer My bad! There was a mistake in the second snippet. The if statement in the hl-line hook incorrectly checked `was-hl-line-mode-on` instead of `hl-line-mode`. I've fixed it. It should work now. – Henrik Nov 14 '16 at 01:58