10

I've binded a indent-for-tab-command command to one the keys and I want it to make smart mode-specific indentation just like it already does but with tabs. In all the modes. It always inserts spaces instead of tabs. How to reconfigure/reprogram it?

I want to use Emacs as fully customizable editor as it's announced to be. So that it would behave exactly as I want. I do not care about developers' opinions at all and want to customize everything. Is this wrong?

Gherman
  • 6,768
  • 10
  • 48
  • 75
  • It depends a bit on exactly what you mean by "tabs instead of spaces". As you suggested, Emacs is extremely configurable, and it doesn't just have a tabs / spaces toggle setting. EmacsWiki has [several pages on different approaches](https://duckduckgo.com/?q=tabs+site%3Aemacswiki.org) that may work for you. – ChrisGPT was on strike Feb 14 '14 at 19:50
  • When I invoke `indent-for-tab-command` it always adds some spaces and tabs to the begininngs of lines. I want these spaces and tabs to be always tabs. That's what I mean. – Gherman Feb 14 '14 at 19:55
  • 1
    If you use tabs instead of spaces, you're on the save side, since everyone can configure their editor to display tabs as wide as they want and thus everyone gets what they want. (The only issue: People don't get that.) – Zelphir Kaltstahl Nov 22 '15 at 17:59

3 Answers3

13

Not all major modes handle indentation the same way, and so you may have to make some adjustments to certain modes to get the behaviour that you want. Often they will have their own indentation settings, e.g. cperl-indent-level.

In cc-mode based modes for C-like languages, something like this should do what you want:

(setq-default indent-tabs-mode t)
(setq-default tab-width 4) ; Assuming you want your tabs to be four spaces wide
(defvaralias 'c-basic-offset 'tab-width)

Note that there are some interesting situations that can come up when using tabs for indentation. The EmacsWiki indentation basics page is worth reading, if only to understand how Emacs treats indentation differently from other editors.

Edit:

For ruby-mode, this should work (assuming you've already set tab-width as above):

(setq ruby-indent-tabs-mode t)
(defvaralias 'ruby-indent-level 'tab-width)

For sgml-mode-derived modes, including html-mode:

(defvaralias 'sgml-basic-offset 'tab-width)
ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
  • I've updated my answer, which should get you going for C. The line setting `tab-width` should have used `setq-default`, not `setq`. Which HTML mode are you using? – ChrisGPT was on strike Feb 14 '14 at 20:41
  • the defailt one. It's called HTML mode as far as I know. – Gherman Feb 14 '14 at 20:49
  • With that it works fine on C and Ruby. It seems I need to investigate each language separately. What documentation can I use to do that? – Gherman Feb 14 '14 at 20:53
  • @German, I've added some settings for both `ruby-mode` and `html-mode` (which is based on `sgml-mode`, and uses its offset). – ChrisGPT was on strike Feb 14 '14 at 20:53
  • @German, precisely because Emacs *does* allow so much configuration, not all modes are consistent. Often you can look for variables like `mode-name-basic-offset` or `mode-name-indent`. If you need a specific one, look on EmacsWiki or ask here. – ChrisGPT was on strike Feb 14 '14 at 20:54
  • 1
    Nice work! Finally I can use that "indent everything correctly" functionality with real tabs for all major modes. – Zelphir Kaltstahl Nov 22 '15 at 17:58
  • Why use `defvaralias` instead of just `setq` ? – deb0ch Jun 16 '16 at 09:42
  • @deb0ch, with [`defvaralias`](https://www.gnu.org/software/emacs/manual/html_node/elisp/Variable-Aliases.html) the two symbols reference the same variable. If you later change either (e.g. in a mode where you want to use different indentation rules) the other will change in that scope as well. – ChrisGPT was on strike Jun 16 '16 at 12:38
  • "People new to Emacs often feel like they just want to insert a tab." – Robert Calhoun Nov 17 '21 at 17:17
2

Setting indent-tabs-mode to a non-nil value is the way to tell Emacs "I want indentation to use TABs wherever possible". But the thing is, if your tab-width is 8 (i.e. TAB chars span 8 columns) and the indentation code found that indentation should be to column 13, there's no way to get there only with TABs, so Emacs will then insert a mix of TABs and SPCs.

But if you really only want TABs, you could override the indentation's choice of column. E.g.:

(setq-default indent-tabs-mode 'only)

together with something like:

(advice-add 'indent-to :around
  (lambda (orig-fun column &rest args)
    (when (eq indent-tabs-mode 'only)
      (setq column (* tab-width (round column tab-width))))
    (apply orig-fun column args)))
Stefan
  • 27,908
  • 4
  • 53
  • 82
-5

Put (setq-default indent-tabs-mode nil) in your .emacs file.

https://www.emacswiki.org/emacs/NoTabs

Worik
  • 152
  • 1
  • 8