I\'ve been unsuccessful in getting Emacs to switch from 8 space tabs to 4 space tabs when pressing the TAB in buffers with the major mode text-mode
.
The key point is to tell emacs to insert whatever you want when indenting, this is done by changing the indent-line-function. It is easier to change it to insert a tab and then change tabs into 4 spaces than change it to insert 4 spaces. The following configuration will solve your problem:
(setq-default indent-tabs-mode nil)
(setq-default tab-width 4)
(setq indent-line-function 'insert-tab)
From Indentation Controlled by Major Mode @ emacs manual:
An important function of each major mode is to customize the key to indent properly for the language being edited.
[...]
The indent-line-function variable is the function to be used by (and various commands, like when calling indent-region) to indent the current line. The command indent-according-to-mode does no more than call this function.
[...]
The default value is indent-relative for many modes.
From indent-relative @ emacs manual:
Indent-relative Space out to under next indent point in previous nonblank line.
[...]
If the previous nonblank line has no indent points beyond the column point starts at, `tab-to-tab-stop' is done instead.
Just change the value of indent-line-function to the insert-tab function and configure tab insertion as 4 spaces.
Add this to your .emacs file:
This will set the width that a tab is displayed to 2 characters (change the number 2 to whatever you want)
(setq default-tab-width 2)
To make sure that emacs is actually using tabs instead of spaces:
(global-set-key (kbd "TAB") 'self-insert-command)
As an aside, the default for emacs when backspacing over a tab is to convert it to spaces and then delete a space. This can be annoying. If you want it to just delete the tab, you can do this:
(setq c-backspace-function 'backward-delete-char)
Enjoy!
Try this:
(add-hook 'text-mode-hook
(function
(lambda ()
(setq tab-width 4)
(define-key text-mode-map "\C-i" 'self-insert-command)
)))
That will make TAB always insert a literal TAB character with tab stops every 4 characters (but only in Text mode). If that's not what you're asking for, please describe the behavior you'd like to see.
This is the only solution that keeps a tab from ever getting inserted for me, without a sequence or conversion of tabs to spaces. Both of those seemed adequate, but wasteful:
(setq-default
indent-tabs-mode nil
tab-width 4
tab-stop-list (quote (4 8))
)
Note that quote
needs two numbers to work (but not more!).
Also, in most major modes (Python
for instance), indentation is automatic in Emacs. If you need to indent outside of the auto indent, use:
M-i
The best answers did not work for until I wrote this in the .emacs file:
(global-set-key (kbd "TAB") 'self-insert-command)
This problem isn't caused by missing tab stops; it's that emacs has a (new?) tab method called indent-relative that seems designed to line up tabular data. The TAB key is mapped to the method indent-for-tab-command, which calls whatever method the variable indent-line-function is set to, which is indent-relative method for text mode. I havn't figured out a good way to override the indent-line-function variable (text mode hook isn't working, so maybe it is getting reset after the mode-hooks run?) but one simple way to get rid of this behavior is to just chuck the intent-for-tab-command method by setting TAB to the simpler tab-to-tab-stop method:
(define-key text-mode-map (kbd "TAB") 'tab-to-tab-stop)