views:

1319

answers:

6
+1  Q: 

sane tab in emacs

I want to override the insane default tabbing scheme in emacs so that it will work like most other editors (eclipse, notepad++). For some reason I can't get it to go.

I found a tutorial online ( first google result ), but none of the things there seemed to work.

I want to set it so that regardless of mode, tab will insert a tab, and pressing enter will keep me at my current tab depth.

I have:

(global-set-key (kbd "TAB") 'tab-to-tab-stop)
(setq default-tab-width 4) ;; 8 is way too many

but it does nothing.

+1  A: 

C-j does the newline + indent functionality that you want out of pressing Enter.

J Cooper
+8  A: 

To make the Enter key take you to the next line and indent it automatically, you can put

(global-set-key (kbd "RET") 'newline-and-indent)

in your .emacs. [Or you can hit C-j instead of Enter.] Once you have that, you'll never need to insert tabs manually, because Emacs automatically indents a new line to extra depth after an opening brace, etc. If you do want to change the indentation, you can hit TAB until it takes you to the right indentation, then start typing from there. [And when you type a closing brace, Emacs is smart enough to take that brace one indentation level backwards.]

You should remove the (global-set-key (kbd "TAB") 'tab-to-tab-stop) for this to work.

ShreevatsaR
+2  A: 

Many major modes override the TAB binding, for example cc-mode binds TAB to 'c-indent-to-column.

The 'global-set-key that is suggested does nothing as almost every major mode has overridden the TAB.

One trick that might work for you is to copy the approach that 'pabbrev uses, and define a global minor mode that has the TAB bound. You could do that like so:

(defvar just-tab-keymap (make-sparse-keymap) "Keymap for just-tab-mode")
(define-minor-mode just-tab-mode
  "Just want the TAB key to be a TAB"
  :global t :lighter " TAB" :init-value 0 :keymap just-tab-keymap
  (define-key just-tab-keymap (kbd "TAB") 'indent-for-tab-command))

However, this disables all TAB completion. You'll probably get best results by overriding each of the major-modes one by one (so as to avoid mussing up TAB completion).

Trey Jackson
A: 

This bugged me, too, when I first started using Emacs. I've come to love it, though. If I want to indent appropriately, I hit ; if I want to insert a literal tab, I hit M-i (Meta and 'i' or - in some parlances) which is bound to `tab-to-tab-stop'.

Joe Casadonte
+1  A: 

I think trey jackson's answer is probably what you want, except possibly use 'self-insert-command instead of 'indent-for-tab-command. I personally prefer emacs' default behavior, but self-insert-command does what it says instead of trying to do anything fancy like make sure your code is well-formatted.

The few times I actually want to insert a tab (not indent) I press M-i.

quodlibetor
A: 

You may be interested in this minor mode I created at http://github.com/vohrta/regtab.

It makes it so that when you press the tab key either a tab character (if indent-tabs-mod is not nil) or tab-width spaces will be placed at point. The mode is also capable of handling what you may consider regular behavior on a region of selected text and shift-tabbing to remove tabs at the beginning of the line (or set of lines).

You can enable or disable it at any time by pressing M-x regtab-mode.

Jake