1

When running incremental search by pressing C-s repeatedly (isearch-repeat-forward) is there a way to highlight, in addition to the matching string, the line where that string is found?

I would like to get an effect equivalent to the one shown below (obtained using swiper and searching the string "dict"), where both the matching string and the entire line are clearly visible.

enter image description here

1 Answer 1

2

Three ways I can think of:

  1. Turn on hl-line-mode. This just always highlights the current line.

  2. Put the function you use to highlight the current line on isearch-update-post-hook. You can use this if you don't want to use hl-line-mode.

(add-hook 'isearch-update-post-hook 'my-highlight-current-line)
  1. Have hl-line-mode be turned on only for isearch, by using hooks isearch-mode-hook and isearch-mode-end-hook:
(defun turn-off-hl-line-mode () (hl-line-mode -1))

(add-hook 'isearch-mode-hook 'hl-line-mode)
(add-hook 'isearch-mode-end-hook 'turn-off-hl-line-mode)
2
  • Thank you; very straightforward answer. I opted for number 3. To make it work I had to modify it slightly, (add-hook 'isearch-mode-hook (apply-partially #'hl-line-mode +1)) (add-hook 'isearch-mode-end-hook (apply-partially #'hl-line-mode -1)), as the isearch-mode-end-hook was being ignored, possibly because it was activating hl-line-mode again instead of setting it off. Commented Aug 10, 2022 at 17:01
  • 1
    Yeah, you're right. I've updated the answer wrt isearch-mode-end-hook. Thx.
    – Drew
    Commented Aug 10, 2022 at 22:29

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.