Emacs tab not working

Richard Knop picture Richard Knop · Jan 23, 2012 · Viewed 19.1k times · Source

I have installed Emacs on my FreeBSD 8.2 box. Everything works fine but I cannot use tabs. When I am editing a file with emacs and hit tab, nothing happens.

What could be causing this?

Answer

Inaimathi picture Inaimathi · Jan 23, 2012

If you're new to Emacs, you might expect pressing TAB to insert a literal \T. For various reasons, that's not the way most Emacs modes work. Most editing modes auto-indent your code as needed (<tab> is bound toindent-for-tab-command rather than self-insert). If the line you're TABbing on is already at the correct indentation level, it might seem that nothing happened.

Auto-indenting like this is easier and more consistent than manually indenting, but doesn't give you as much flexibility when it comes to deciding exactly how much whitespace is going to be present at the beginning of each line (and it also causes some problems when you want to, for example, tab-separate some fields). You can auto-indent a region using C-M-\ (that's Ctrl + Alt + \).

If you absolutely, positively must insert a literal \T into your code somewhere, you can do so using C-q TAB (press and release Ctrl + q and then press TAB). Typically, this is done to align columns in other editors; if that's what you're doing, it's probably a better idea to use align-regexp rather than tab literals.