Turning on linum-mode when in python/c mode
Asked Answered
B

4

8

I want to turn on linum mode (M-x linum-mode) automatically with python and c mode. I add the following code in .emacs, but it doesn't seem to work.

(defun my-c-mode-common-hook ()
  (line-number-mode 1))
(add-hook 'c-mode-common-hook 'my-c-mode-common-hook)

(defun my-python-mode-common-hook ()
  (line-number-mode 1))
(add-hook 'python-mode-common-hook 'my-python-mode-common-hook)

What might be wrong?

Bailiwick answered 6/10, 2010 at 17:21 Comment(0)
H
9

line-number-mode and linum-mode are not the same.

Try this:

(defun my-c-mode-hook () 
  (linum-mode 1)) 
(add-hook 'c-mode-hook 'my-c-mode-hook) 

(defun my-python-mode-hook () 
  (linum-mode 1)) 
(add-hook 'python-mode-hook 'my-python-mode-hook) 
Habanera answered 6/10, 2010 at 17:26 Comment(2)
Unfortunately, it doesn't work after the change. Is there any way to debug what might be wrong?Bailiwick
Remove the common from the hook name. I fixed the code above.Habanera
R
15

You also have the option of setting linum-mode globally.

;; In your .emacs
(global-linum-mode 1)

Edit: In my configuration I have global-linum-mode active and inhibit it for certain major modes:

(setq linum-mode-inhibit-modes-list '(eshell-mode
                                      shell-mode
                                      erc-mode
                                      jabber-roster-mode
                                      jabber-chat-mode
                                      gnus-group-mode
                                      gnus-summary-mode
                                      gnus-article-mode))

(defadvice linum-on (around linum-on-inhibit-for-modes)
  "Stop the load of linum-mode for some major modes."
    (unless (member major-mode linum-mode-inhibit-modes-list)
      ad-do-it))

(ad-activate 'linum-on)
Robertroberta answered 7/10, 2010 at 7:50 Comment(0)
H
9

line-number-mode and linum-mode are not the same.

Try this:

(defun my-c-mode-hook () 
  (linum-mode 1)) 
(add-hook 'c-mode-hook 'my-c-mode-hook) 

(defun my-python-mode-hook () 
  (linum-mode 1)) 
(add-hook 'python-mode-hook 'my-python-mode-hook) 
Habanera answered 6/10, 2010 at 17:26 Comment(2)
Unfortunately, it doesn't work after the change. Is there any way to debug what might be wrong?Bailiwick
Remove the common from the hook name. I fixed the code above.Habanera
O
3

All major mode for programming languages derive from prog-mode, so (add-hook 'prog-mode-hook 'linum-mode) will enable linum-mode for all programming modes.

Overflow answered 5/12, 2018 at 3:6 Comment(0)
V
0

Not sure what hooks C-mode is supposed to use (never used C-mode), but this should do what you want:

(dolist (hook '(python-mode-hook
            c-mode-common-hook))
  (add-hook hook (lambda () (linum-mode t))))
Virginia answered 10/10, 2010 at 22:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.