3

In shell-script-mode, when a line ends in &&, Emacs will indent the following line. Is there a way to switch that off? (I am concatenating most of my statements with &&. This gives finer control over error handling than set -e.)

1

The indentation code for shell scripts has been completely rewritten around the time of Emacs-24 to use SMIE, and SMIE itself has evolved during the life of Emacs-24, so the best solution may depend on your exact Emacs version, but with Emacs-24.5 you should be able to do:

  • Go to the line with incorrect indentation.
  • type M-x smie-config-show-indent which should show you the rules that were used to indent this line. It might say something like:

    Rules used: :before "&&" -> (column . 19), :before "[" -> nil, :elem basic -> 4, :after "&&" -> nil

Sadly the (column . 19) in there mostly implies that you won't be able to change this behavior in a "simple" way (i.e. you can't change it with smie-config-set-indent).

But I think the following should do it:

(defun my-sh-smie-sh-rules (origfun kind token)
  (pcase (cons kind token)
    (`(:before . "&&") nil)
    (_ (funcall origfun kind token))))
(advice-add 'sh-smie-sh-rules :around #'my-sh-smie-sh-rules)

You might like to M-x report-emacs-bug and request that such a config be made easier, since it's probably a fairly common indentation style.

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.