Question

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?

Was it helpful?

Solution

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)

OTHER TIPS

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)))

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top