Turning on linum-mode when in python/c mode

2019-04-06 18:08发布

问题:

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?

回答1:

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) 


回答2:

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)


回答3:

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))))


回答4:

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.