• linum-line it not enabled when emacs is started.

I just want to enable linum-mode (see the line numbers) during I-search and disable it when I-search: is ended. Is it possible?

  • 1
    Is there a reason not to use the built-in line-numbering feature that comes with Emacs 26? The reason I ask is because the answer will be different if you have specifically chosen to use a deprecated Lisp method to generate line numbers; e.g., linum-mode or nlinum. – lawlist Jul 27 '20 at 1:43
  • Oh there is no reason at all. I was just using goto-line-with-feedback if I want to see line-numbers but I don't know on the background which line-feature it is using – alper Jul 27 '20 at 1:47

Ideally, one would not have to worry about line numbers at all: the various tools would cooperate to put you in the right place. However, there are holes in the coverage, so you occasionally need to know on what line you are and how to get to a given line.

As @lawlist points out, it would be preferable to use display-line-number-mode - see the EmacsWiki page for details. That takes care of knowing where you are and, in my case, binding goto-line to a key takes cares of the second. AFAIK, this is much more efficient than linum-mode (which I am not using, but which, when I tried it out many years ago, was very unresponsive, although undoubtedly it is much better than it was).

If you still want to turn it on and off in isearch, you can try using isearch-mode-hook and isearch-mode-end-hook. Something like this (minimally tested):

(add-hook 'isearch-mode-hook (lambda () (linum-mode 1)))
(add-hook 'isearch-mode-end-hook (lambda () (linum-mode 0)))

This is bog-standard emacs customization: the only thing that you have to research is whether the appropriate hooks are available, which is generally easy to do by using C-h v isearch--hook TAB in this case, taking advantage on one hand of the universal convention of hook naming (<prefix>-<type>-hook) and on the other hand the standard completion mechanism (which will expand the -- in the middle, giving you all the variables that start with isearch- and end with -hook). IMO, this is a very useful trick and is worth remembering.

  • I tired the code related ot add-hook but it did not show the line numbers :-( – alper Jul 27 '20 at 17:09
  • 1
    Try it starting with emacs -q - you apparently have a lot of stuff in your init file which causes problems. In any case, I "minimally tested" to make sure that the numbers show up when you start an isearch and go away when you exit. – NickD Jul 27 '20 at 17:34
  • You are right my init file is little bit mess :-) It works with emacs -q. I will try to figure out what is prevents it by removing some lines. And thanks for the trick advice – alper Jul 27 '20 at 18:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.