How can I use the TAB key for inserting literal tab characters (including multiple ones). With (setq-local indent-tabs-mode nil) I cannot introduce them because Emacs doesn't let me do so.

With the default (setq-local indent-tabs-mode t) I am also limited in what I can do with the TAB key.

2 Answers 2


Bind TAB to self-insert-command.

Do that globally, if you want that behavior by default:

(global-set-key (kbd "TAB") 'self-insert-command)

Do it (also) in any major mode that overrides that global behavior. E.g., for emacs-lisp-mode:

(define-key emacs-lisp-mode-map (kbd "TAB") 'self-insert-command)

That answers your question, which was to get Emacs to let you insert TAB chars.

As for your problem of RET doing its own default inserting (which you mentioned in a comment), just turn off electric-indent-mode.

  • 1
    Yes, electric-indent-mode must also be termed off. Disabling electric-indent-mode in vital for working with these codes. Emacs assumes that a file always has a style and there exists only one style. These assumptions are both wrong.
    – Dilna
    Jun 12, 2022 at 3:36
  • A file does not have to have a style, it just needs to be syntactically correct. For legacy codes written by a large number of people within different institutions can be highly uncoordinated, resulting in codes for the same language varying immensely. Imposing one style fails disastrously. The only solution is to allow the possibility of no style. This can be done by enabling a special mode for the language to cater for these cases.
    – Dilna
    Jun 12, 2022 at 3:37

Type C-q first. This calls the function quoted-insert, which reads one keypress from the keyboard, turns it into a character, and then inserts the character into the document, regardless of what keybindings have been established for that character.

  • Have tried that. But when I get to the end of a line and press return, the indentation rules are activated and the additional tabs get removed.
    – Dilna
    Jun 11, 2022 at 10:42
  • Then you should include that information in the question. Don’t forget to mention what mode your buffer is in, as that affects the answer quite a lot.
    – db48x
    Jun 11, 2022 at 20:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.