I am editing a text file foo.txt
using emacs.
I press C-q TAB to insert a TAB character at the beginning of a line and then follow with a few characters.
Once I press ENTER, emacs inserts eight spaces on the following line.
How do I specify in my .emacs that I would like TABs to be repeated on subsequent lines with TABs?
Importantly, I dislike TAB characters in program code, and so I have (setq-default indent-tabs-mode nil)
to make sure that TABs are inserted only when I explicitly ask for them.
The real answer is: No, there is no way to do this by some simple configuration setting in emacs.
indent-tabs-mode
is either on or off, and indentation will behave according to that.But, just because this feature is not there, doesn't mean you can't add it!
This is actually not a simple problem from what I found. Whether or not to use tabs or spaces is determined by
indent-tabs-mode
in C mostly. Assuming that you are running a recent version of emacs, the auto indentation is coming fromelectric-indent-mode
which usesindent-according-to-mode
in apost-self-insert-hook
to do the indentation.What I did for this was define a buffer local minor mode, when this mode is active
indent-tabs-mode
will be temporarily set depending on the first character in the last line while runningindent-according-to-mode
.So when
smart-electric-indent-tabs-mode
is active, and your last line started with the tab, the next line will indent with a tab too, else it will just use whateverindent-tabs-mode
would normally be set to.You could add the following to your config to activate it. The
add-hook
clause is put in there for your convenience, you can activate it on the fly like a normal minor mode if you'd like.This has only been tested to work during electric indentation
Emacs inserts
SPC
chars because you told it to, by settingindent-tabs-mode
tonil
(my preference too, BTW).If you want Emacs to indent using
TAB
chars in a particular mode (buffer), but you want it to useSPC
chars in general (i.e., in other modes), then setindent-tabs-mode
tot
in those modes where you wantTAB
s. Just usesetq
when you are in the mode, since it is a buffer-local variable. For example: