11

I want to delete trailing whitespace on save for every mode except org-mode.

In my .emacs, I have the following line:

(add-hook 'before-save-hook 'delete-trailing-whitespace)

I use the use-package macro, and I tried adding (remove-hook 'before-save-hook 'delete-trailing-whitespace) to my :config block, but that removes the hook globally. How can I remove it specifically for org-mode?

16

I don't have any experience with the use-package macro, but in principle, you could use the third argument to remove-hook which tells it to remove the function only from the local hook:

(remove-hook 'before-save-hook 'delete-trailing-whitespace t)

Here's the docstring for reference:

(remove-hook HOOK FUNCTION &optional LOCAL)

Remove from the value of HOOK the function FUNCTION. HOOK should be a symbol, and FUNCTION may be any valid function. If FUNCTION isn't the value of HOOK, or, if FUNCTION doesn't appear in the list of hooks to run in HOOK, then nothing is done. See add-hook.

The optional third argument, LOCAL, if non-nil, says to modify the hook's buffer-local value rather than its default value.

  • Works perfectly, thank you. Another great reminder to C-h f. I'll accept as soon as the timer runs down. – Matthew Piziak Aug 3 '15 at 21:39
  • 2
    @MatthewPiziak: if it makes you feel any better, it took a very long time before I realized that it took that third argument, and it was only after someone on this site pointed it out to me. – Dan Aug 3 '15 at 21:41
  • 2
    Won't this only work if the function was explicitly added as LOCAL in add-hook to begin with? (A quick test in 24.5 seems to confirm my expectation... is there new functionality in trunk to facilitate this kind of override?) – phils Aug 3 '15 at 22:49

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.