user673592 user673592 - 1 year ago 107
Bash Question

Emacs shell script mode hook

For some reason my shell script mode hooks do not get executed. Example in my .emacs:

(add-hook 'shell-script-mode-hook (lambda ()
(rainbow-delimiters-mode 1)))

causes the variables to be set, but the mode is not loaded for the opened script files. What is the proper way to hook here?

I use the default shell script mode (modeline says e.g.
). Do I have to hook for each shell type individually (sh, bash, zsh)? If yes can you please tell me how?

Thank you very much!


It turned out to be due a conflict of textmate-mode with the skeleton-pair-insert in sh-mode (I tried to avoid the conflict by disabling textmate in sh-mode, which then left the sh-mood-hook aparatus in ruins. I've removed textmate-mode completely and use now the standard skeleton-pair approch globaly.

I'll accept phils answer - without him I'd probably not be able to debug this on my own.


Thanks to phils, I think his comment takes us closer to solution. It's not a problem with rainbow-delimiters though. I removed all sh-mode-hook except your hello message one and restart Emacs. When I open a .sh file I get this:

Setting up indent for shell type bash
setting up indent stuff
Indentation variables are now local.
Indentation setup for shell type bash
File mode specification error: (void-function nil)

Note no "hello" message. The value of sh-mode-hook is:
(lambda nil
(message "hello")))

I think the problem is this first nil value - though I don't see that it would be set anywhere.

If I eval this:

(setq sh-mode-hook t)
(add-hook 'sh-mode-hook (lambda () (message "hello")))

I see the hello message, though after restart (I've put those lines in .emacs) it is gone again (the nil is again on top of the hook).

Any idea what to do to have active hook at setup?

I've tried also:

(add-hook 'sh-mode-hook (lambda ()
(rainbow-delimiters-mode 1)))

with same negative result - not sure if this is relevant though...

Answer Source

shell-script-mode is an alias for sh-mode. I haven't checked, but I would suspect that only the hook variable for the 'real' function name is evaluated, so I think sh-mode-hook would be the one to use.

Anyhow, there's nothing broken about your syntax, so there may be something amiss with the use of (rainbow-delimiters-mode 1). For instance, you should be able to observe that the following works correctly:

(add-hook 'sh-mode-hook (lambda () (message "hello")))

FWIW, for hooks I recommend not using anonymous functions at all, simply because it's much easier to update your hook function if it is named (removing the old lambda expression from the variable before adding an updated one is just annoying in my books).