0
votes

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)))

1

1 Answers

0
votes

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.