I am trying to set the number of spaces per indent for starting a new function in sh-mode (bash sub-mode). I have tried setting:

'(sh-basic-offset 2)
'(sh-indentation 2)
'(smie-indent-basic 2)

(as shown in custom-set-variables in .emacs) and some others as well, but none of them have had any effect!

  • The way to set variables is not via custom-set-variables (which is the thing used internally by Customize but is not meant to be used by hand). Better use things like (setq sh-basic-offset 2).
    – Stefan
    Nov 4, 2017 at 17:25

3 Answers 3


Setting sh-basic-offset to the required indentation is what is needed - but what I didn't realise is, it's also necessary to close and reopen all shell script files before the customisation takes effect. I can't remember ever having to do this before in emacs.

  • 1
    After changing .emacs, you must either quit emacs and restart it, or use M-x load-file to reload your .emacs If you want to reload a file that you have open in your editor, you can say C-x C-v RETURN to "revisit" it.
    – Alex L
    Jan 13, 2017 at 19:09
  • @AlexL load-file does not work in this particular case. Like I said, this is very unusual. Usually simply customising a variable is sufficient. Apr 4, 2017 at 10:36
  • Hmm...do you know why this would be @robin-green? I'm curious.
    – Alex L
    Apr 4, 2017 at 19:22
  • You can also use M-x set-variable if you want to just change this for the buffer you are working in. Apr 1, 2020 at 15:35
  • Worth noting that nowadays the effect is immediate, i.e. no need to reopen the file.
    – Hi-Angel
    Jan 18, 2022 at 14:36

I set-basic-offset to 2 but that didn't seem to work, I went back pressed C-h v sh-<TAB> and realized that there was a sh-indentation I set that variable to 2 and everything seems to work like a charm now.

  • Please clarify what you mean by "set-basic-offset" and also clarify exactly how you "set that variable to 2".
    – Stefan
    Nov 4, 2017 at 16:12
  • The forum omitted tab because I placed in between the greater than and less than character. C-h v sh- "press tab" autocomplete will show you a list of variables. Nov 7, 2017 at 11:23
  • I fixed your markup, but your comment still doesn't explain what you meant by "set-basic-offset" nor how you "set that variable to 2".
    – Stefan
    Nov 7, 2017 at 18:49
  • This variable is obsolete since 26.1, use sh-basic-offset instead.
    – goetz
    Apr 5, 2019 at 19:39

I had the same experience as Albaro: setting sh-basic-offset having no effect, but setting sh-indentation working as expected.

In my case, I evaluated (setq sh-basic-offset 2) from with emacs, eg. via M-x eval-expression. I am running GNU Emacs 25.2.2 (x86_64-pc-linux-gnu, GTK+ Version 3.22.21) of 2017-09-22, modified by Debian. My major mode is Shell-script.

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.