sh-mode won't care if indent-tabs-mode is on, it will still insert spaces instead of tabs! Can this be fixed?

  • What you describe doesn't match my experience, so please give more details, otherwise people will assume it's a misunderstanding of yours, or some local misconfiguration. – Stefan Oct 3 '18 at 1:36
  • Can you reproduce the problem starting from emacs -Q ? – phils Oct 3 '18 at 1:54
  • Just press tab, then delete and you'll delete spaces, not tabs. As for me a tab is 4 spaces wide, meaning it is as big as for spaces but only a single press of backspace will delete all these 4 spaces (1 tab) – Suppboi Oct 3 '18 at 2:45
  • 1
    That's a different question then. You'll find that a tab was inserted into the buffer, regardless of what the backspace key does. – phils Oct 3 '18 at 4:32

I'm going to guess that tab-width and sh-basic-offset are not aligned.

indent-tabs-mode will only use tabs to indent up to multiples of tab-width, as otherwise it would indent too much.

That said, I find tabs bad for shell script indentation, as copying and pasting code from a script into a shell running in a terminal can be problematic. I would recommend using spaces for shell scripts for this reason.

Edit: In this instance it was the backspace behaviour which was causing confusion.

C-hkDEL tells me:

DEL (translated from <backspace>) runs the command
backward-delete-char-untabify (found in sh-mode-map), which is an
interactive compiled Lisp function in ‘simple.el’.

It is bound to DEL.

(backward-delete-char-untabify ARG &optional KILLP)

Delete characters backward, changing tabs into spaces.
The exact behavior depends on ‘backward-delete-char-untabify-method’.
Delete ARG chars, and kill (save in kill ring) if KILLP is non-nil.
Interactively, ARG is the prefix arg (default 1)
and KILLP is t if a prefix arg was specified.

Follow the link provided there and set backward-delete-char-untabify-method to nil so that it deletes a single character (which should be a tab) each time.

  • both them hos is 4 – Suppboi Oct 3 '18 at 1:30
  • In that case, I can't reproduce your problem. indent-tabs-mode works fine for me in sh-mode (and I used to use tabs in shell scripts a long time ago, so it was working in versions of Emacs as far back as I recall). – phils Oct 3 '18 at 1:52
  • Answer updated to explain what backspace is doing. – phils Oct 3 '18 at 4:39
  • good looking out. Should I change the title? If so any suggestions? – Suppboi Oct 3 '18 at 13:29
  • Your warning against tabs seems ok in general, but if the script uses a heredoc input with the <<- operator tabs MUST be used for it to function correctly. – nolandda May 16 at 3:59

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.