1

I have a shell script that uses 2-space indentation, like this:

#!/bin/bash

function foo() {
  echo "Hello world"
  echo "Nice to see you $1"
}

foo Tony

Emacs default indentation for shell is 4-space. I have been trying to specify local variables to change the indentation to 2-space, but failed. This is the extra stub I added at the end for the script above (that did not succeed):

# Local-variables:
# sh-indentation: 2
# sh-basic-offset: 2
# End:

Before doing this local variable thing, I tried to customize emacs using Programming -> Languages -> Sh -> Sh Script -> Sh Indentation -> Sh Basic Offset. If I set this variable to 2, and open the shell script, it did indent that script to 2-space.

On the other hand, the exact same Emacs could do it for a Python script, simply with this kind of stub at the end of that Python script:

# Local-variables:
# python-indent: 2
# End:

Can someone help me through this?

Wirawan

  • Have you tried replicating the problem without any init file (emacs -q). This issue could be caused by some hook running when the file is opened. – user12563 Jul 4 '19 at 15:37
2

There should be a space instead of a - between Local and Variables:

  • Thank you for spotting my typo! Couldn't believe I did not catch that small error! – Wirawan Purwanto Jul 4 '19 at 18:33
  • 1
    @WirawanPurwanto Note that you can use M-x add-file-local-variable and it will generate those comments for you (if they don't already exist), as well as giving you tab-completion for the variable name. – phils Jul 4 '19 at 22:24

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.