0

Is it possible to have the line number where the cursor is shown in the mode-line (or line and column numbers appearing in the buffer) only for certain buffers, while it is disabled for other buffers.

I know that I can toggle it globally with M-x line-number-mode, but it interactive buffers (shells) or in buffers where I write text, I don't like it.

Please note that I am using the smart-mode-line package.

1 Answer 1

3

Line number mode is implemented as a global-minor-mode. I don't know an easy way to make it a 'normal' minor-mode (although there probably is). However, you can easily hide the line-number indicator locally by configuring the mode-line-position-line-format. For shells, you can achieve it e.g. as follows

(defun hide-line-number-hook-fn ()
  (setq-local mode-line-position-line-format ""))

(add-hook 'comint-mode-hook #'hide-line-number-hook-fn)

For other buffers, you can use similar methods (e.g. using the org-mode and/or text-mode hooks).

5
  • That does not work for me, because I am using smart-mode-line package. I edited the question accordingly. Jan 11 at 13:43
  • 1
    You'd need to check how/where smart-mode-line gets the text %l into mode-line-format then. Or try the same answer but with (setq-local line-number-mode nil) instead of (setq-local mode-line-position-line-format "").
    – phils
    Jan 11 at 20:00
  • 2
    Note: That last suggestion is not a generic way to "make a global minor mode buffer-local" (you would always have to read and understand specifically how each global mode works to figure out whether and how you could achieve that); but for this particular mode the variable line-number-mode simply controls whether or not the %l in the mode line format will be processed, so we can just set the buffer-local value of that. (And likewise for column-number-mode.)
    – phils
    Jan 11 at 20:16
  • 2
    dalanicolai: I wouldn't even mention anonymous functions (here or in any other such answer). It's too easy for people to unwittingly add multiple conflicting lambdas on a hook, not understand why things aren't working, and create more confusion for themselves.
    – phils
    Jan 11 at 20:17
  • @phils Good point. I have updated the answer. Although I slightly prefer your solution of setting line-number-mode to nil, that solution also makes the variable nil while the mode is still 'active' (quite irrelevant, of course). Anyway, I kept the solution as is because of that, and because your suggestion is easy to find in the comments anyway. Thanks for the advice for improving the answer. Jan 11 at 23:04

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.