5

Whenever I start emacs, there are a few things I do right away:

M-x slime
M-x ido-mode

I also open a few files that I always use so they are available as buffers:

C-x C-f ....

When I get into a buffer, I then do this for that buffer (nearly all buffers):

M-x visual-line-mode

If it is a Lisp buffer, I also always do this as well:

M-x paredit-mode
M-x rainbow-delimiters-mode
M-x show-paren-mode

Is there a way for emacs to automatically do all of these things when I start emacs and when I load buffers?

Baggers
  • 3,183
  • 20
  • 31
johnbakers
  • 24,158
  • 24
  • 130
  • 258
  • Emacs has several hooks built-in:  http://www.gnu.org/software/emacs/manual/html_node/elisp/Standard-Hooks.html#Standard-Hooks   And, most major modes, and most minor modes, also have hooks. – lawlist Oct 30 '13 at 01:13
  • Some modes can be made global -- e.g., `(global-visual-line-mode 1)`, but I recommend using mode hooks for more control and stay away from most "global" settings unless you are absolutely sure you want it affecting everything. – lawlist Oct 30 '13 at 01:43

1 Answers1

6
(add-hook 'emacs-startup-hook
  (lambda ()
    (kill-buffer "*scratch*")
    (find-file "~/todo.org")
    (ido-mode t)
  ))

;; Emacs Lisp
(add-hook 'emacs-lisp-mode-hook
  (lambda ()
    (slime-mode t)
    (visual-line-mode 1)
    (paredit-mode 1)
    (rainbow-delimiters-mode 1)
    (show-paren-mode 1)
  ))

;; Common Lisp
(add-hook 'lisp-mode-hook
  (lambda ()
    (slime-mode t)
    (visual-line-mode 1)
    (paredit-mode 1)
    (rainbow-delimiters-mode 1)
    (show-paren-mode 1)
  ))
lawlist
  • 13,099
  • 3
  • 49
  • 158
  • thanks, shouldn't `ido-mode` be moved to the `startup` hook, since it affects all buffer navigation in emacs? – johnbakers Oct 30 '13 at 01:48
  • If `ido-mode` is indeed global, then it could be set all by itself without a hook or within the `emacs-startup-hook` -- I'll move it over to the startup hook -- thank you. – lawlist Oct 30 '13 at 02:01
  • 1
    When I type `M-x slime` in emacs, it opens lisp REPL, which in turns opens a couple of new buffers. Is there a way to get this to happen on startup? – johnbakers Oct 30 '13 at 02:07
  • I think there must be a better hook than `emacs-lisp-mode` because that doesn't seem to affect a common lisp file; the rainbow parens are there when I edit `.emacs` but are not there in a `.lisp` file unless I turn them on manually; same with paredit, only applies to .emacs file not .lisp files – johnbakers Oct 30 '13 at 02:09
  • ah, I changed it to `slime-mode-hook` and that did the trick. not sure `slime-mode t` is necessary there though if we are already in slime mode? – johnbakers Oct 30 '13 at 02:15
  • I don't presently have all of the dependencies for slime set up on my OSX 10.6.8 installation. `emacs-lisp-mode` is a major mode. It looks like `lisp-mode` is a slightly different animal. Perhaps consider using this in your `.emacs` file: `(add-to-list 'auto-mode-alist '(".lisp\\'" . emacs-lisp-mode))` Alternatively, there is a `lisp-mode-hook` you could try instead. – lawlist Oct 30 '13 at 02:23
  • Here is an interesting thread I just found with links / citations to the differences between Common Lisp and Emacs Lisp: http://stackoverflow.com/questions/8433474/what-are-the-major-differences-between-emacs-lisp-and-common-lisp   I will add a `lisp-mode-hook` to the answer. Thank you for helping me to understand that there were differences between the two. – lawlist Oct 30 '13 at 02:40
  • Slime mode hook also works in this example. I presume that is because I already have slime required from earlier in my config file – johnbakers Oct 30 '13 at 02:58
  • I usually enable minor modes with a major mode hook, rather than a minor-mode hook. Since it would appear that slime-mode is a minor mode, I would probably use major mode hooks instead to activate the other minor modes e.g., one of the two mentioned above (i.e., `lisp-mode-hook` for Common Lisp, or `emacs-lisp-mode-hook` for Emacs Lisp). I would leave `(slime-mode t)` under one of the major mode hooks, so there would be no need to call it again with the `slime-mode-hook`. I would probably use the `slime-mode-hook` sparingly for something special that is only used in that minor mode. – lawlist Oct 30 '13 at 02:59
  • OpenLearner: FYI, you might prefer to use the pattern `(add-hook 'hook-var 'my-func-name) (defun my-func-name () (do) (things))` which has the advantage of letting you modify and re-evaluate each `my-func-name` function to immediately see the effects on the hook. To do that with lambda expressions, you need to *remove* the old unmodified lambda from the hook variable, and then add the new one, which makes experimentation more difficult/cumbersome. – phils Oct 30 '13 at 21:09