In Latex mode, the tab key runs the command indent-for-tab-command

From the output of C-h k tab when in Latex mode

enter image description here

I rarely need to indent lines in my Latex files, and so I want to disable this behavior. Also I often find it necessary to insert 5 spaces at a time, when I am writing little snippets of Python or Yaml code in my .tex files.

Is it possible to bind the tab key to be able to insert 5 spaces (but only when in Latex mode)?

1 Answer 1


Well, the answer to "is it possible to make Emacs do " is almost always "yes". If you really just want to insert five spaces with a TAB, you could do it by:

(defun my-tab-with-spaces ()
  "Insert five spaces for a tab the way I like it."
  (insert "     "))

(add-hook 'latex-mode-hook
          '(lambda ()
                   (local-set-key (kbd "TAB")'my-tab-with-spaces)))

There are lots of other ways to fight with Emacs indentation functions, or to make this code more flexible with different situations. Many major modes try to make TAB be smart for what to do.

Also, if you're using AUCTeX instead of the built-in latex-mode, you'll need to change the above to use LaTeX-mode-hook.

  • Perfect! That worked nicely! Thank you so much! Small point. You need to add two closing parentheses at the end, because they are unbalanced as written. Commented Dec 17, 2018 at 4:37
  • Glad it worked, and sorry about the missing parens (I edited them in). That's what I get for sloppy cut & paste.
    – Win
    Commented Dec 18, 2018 at 15:48

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.