I found a way to auto indent when I type one of }, ; or ) using this code:

(setq mode-autoindent-list '(javascript-mode js-mode php-mode web-mode))
(defun mode-autoindent ()
  (when (and (member major-mode mode-autoindent-list) (looking-back "[});]"))

(add-hook 'post-self-insert-hook 'mode-autoindent)

inspired by the code in this answer on stack overflow Auto indent after inserting a semicolon or curly brace

Is there a better way, maybe build in mode?

  • I'm not sure about what specifically happens in these modes, but you could try modifying the value of electric-indent-chars while these modes are active and electric-indent-post-self-insert-function should take care of it for you. – wvxvw Jan 29 '17 at 15:14
  • @wvxvw Forget about electric-indent-mode, that's exactly what I've needed. – jcubic Jan 29 '17 at 16:02

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.