I've been using hide-ifdef-mode with c-mode buffers for a while, but it is quite tedious to have to manually run hide-ifdefs (C-c @ h) every time a block of code containing preprocessors changes.

Is there any way this can be run automatically? (for example during syntax highlighting)

1 Answer 1


After a while, I think I have found a viable solution: add hide-ifdefs to after-save-hook hook when running c-mode-common-hook - like so:

  (add-hook 'c-mode-common-hook
             (lambda ()
               ;... your other hook code here
               (add-hook 'after-save-hook (lambda () (hide-ifdefs)))

The sequence is: open a C file which runs the c-mode-common-hook; that enables hide-ifdef-mode and adds a function to the buffer's after-save-hook. Eventually, when the buffer is saved, the after-save-hook is run which executes hide-ifdefs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.