16

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!

Robin Green
  • 949
  • 7
  • 16
  • 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 04 '17 at 17:25

3 Answers3

11

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.

Robin Green
  • 949
  • 7
  • 16
  • 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 '17 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. – Robin Green Apr 04 '17 at 10:36
  • Hmm...do you know why this would be @robin-green? I'm curious. – Alex L Apr 04 '17 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. – kotakotakota Apr 01 '20 at 15:35
  • Worth noting that nowadays the effect is immediate, i.e. no need to reopen the file. – Hi-Angel Jan 18 '22 at 14:36
4

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.

Stefan
  • 26,154
  • 3
  • 46
  • 84
  • Please clarify what you mean by "set-basic-offset" and also clarify exactly *how* you "set that variable to 2". – Stefan Nov 04 '17 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. – Albaro Pereyra Nov 07 '17 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 07 '17 at 18:49
  • This variable is obsolete since 26.1, use `sh-basic-offset` instead. – goetz Apr 05 '19 at 19:39
1

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.

Wes
  • 111
  • 2