In my init.el file I have the following line: (global-linum-mode 1). I want linum-mode enabled for every file, but not for other buffers. I.e. Is there a way to enable linum mode globally for all buffers with an extension?

Frequently my buffer list looks like

file.js  js2-mode
init.el   Emacs-Lisp
ansi-term Term:

In this case I would like to have it enabled for everything but ansi-term. Is this possible?

Furthermode, just for ansi term I tried the following:

(add-hook 'term-mode-hook (lambda () (linum-mode -1)))  

But even that did not work. If someone could tell me why that would be great but enabling it just for files would be ideal.


  • I think a little differently than everyone else, perhaps because I'm not a programmer. Personally, I would set up a regexp function in the initialization of the minor-mode that checks the file extension -- e.g., buffer-file-name -- against a list of either desired or excluded file extensions. Depending upon the match or exclusion, I would let the minor-mode continue initializing or deactivate at that point. Here is the regexp function: stackoverflow.com/a/20343715/2112489 Perhaps people just don't like modifying the source-code -- a sacred/holy thing -- not sure though.
    – lawlist
    Apr 9, 2016 at 5:40

3 Answers 3


Enable Linum Mode for all files [...] but not other buffers?

How about:

(add-hook 'find-file-hook 'linum-mode)

A solution covering nearly every file would be making use of the fact that nearly all buffers for these are derived from text-mode and prog-mode:

(add-hook 'text-mode-hook 'linum-mode)
(add-hook 'prog-mode-hook 'linum-mode)

The linum-off.el file by Matthew Fidler should be what you are looking for.

Here is a copy of his code:

(require 'linum)

(defcustom linum-disabled-modes-list '(eshell-mode wl-summary-mode compilation-mode org-mode text-mode dired-mode doc-view-mode image-mode)
  "* List of modes disabled when global linum mode is on"
  :type '(repeat (sexp :tag "Major mode"))
  :tag " Major modes where linum is disabled: "
  :group 'linum
(defcustom linum-disable-starred-buffers 't
  "* Disable buffers that have stars in them like *Gnu Emacs*"
  :type 'boolean
  :group 'linum)

(defun linum-on ()
  "* When linum is running globally, disable line number in modes defined in `linum-disabled-modes-list'. Changed by linum-off. Also turns off numbering in starred modes like *scratch*"

  (unless (or (minibufferp)
              (member major-mode linum-disabled-modes-list)
              (string-match "*" (buffer-name))
              (> (buffer-size) 3000000)) ;; disable linum on buffer greater than 3MB, otherwise it's unbearably slow
    (linum-mode 1)))

(provide 'linum-off)

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.