views:

470

answers:

4

The title of my question is a reference to sane tabs in emacs.

Basically what I want is to globally set tabs and indention to work in some uniform way. I feel like emacs is so much better than TextMate or BBEdit but really the way they handle indention is simple and great for my purposes. In emacs if you use some tab/space scheme that's different than the scheme enforced by a minor mode you use you're in trouble.

When I press enter I'd like to be moved to the next line indented to the right place using tabs. If I can have my cake and eat it too I'd like to be indented using spaces if the rest of the file is composed that way.

I've tried these also:
doing tabs in emacs
force emacs to use tabs

Thanks to anyone who can help me achieve this.

-Mike

+4  A: 

Perhaps (global-set-key (kbd "RET") 'newline-and-indent) is what you want? (Or reindent-then-newline-and-indent if that's available, or you could just hit C-j instead of the Enter key.)

ShreevatsaR
+2  A: 

For this part of your question:

If I can have my cake and eat it too I'd like to be indented using spaces if the rest of the file is composed that way.

does this do what you want?

(defun dtrt-indent ()
  (setq indent-tabs-mode  
        (save-excursion
          (goto-char (point-min))
          (search-forward "\t" nil t))))
(add-hook 'text-mode-hook #'dtrt-indent)
(add-hook 'c-mode-hook #'dtrt-indent)
; etc for all modes you care about

So if there's a tab anywhere in the buffer, indent using tabs; if there is no tab, indent using spaces.

Jouni K. Seppänen
FWIW, rather than guess, I declare settings like this with eproject:http://wiki.github.com/jrockway/eproject/project-attributes
jrockway
A: 

Hi,

if you do your setup as described:

(setq indent-tabs-mode t)
(setq-default indent-tabs-mode t)
(setq tab-width 4) ;; 8 is way too many
(setq default-tab-width 4) ;; 8 is way too many
(global-set-key (kbd "RET") 'newline-and-indent)

The indent-tabs-mode thing will tell emacs to create your indentation by using TABS and SPACES to make up the desired indentation (defined by the individual mode). This means, if you want to have a TAB inserted instead of TABS/SPACES you need to configure your mode to use tab-width as indentation.

For example if you use c-mode and select cc-mode as indentation style (select with C-c .) which uses 4 as indentation value, newline-and-indent will insert spaces.

To conclude:

  1. Check that your mode uses tab-width as indentation
  2. Check that your mode doesn't overwride indent-tabs-mode (python-mode seems to do this)

Although I personally don't like TABS good luck on your journey :)

danielpoe
A: 

The best strategy is to convince your programming mode of choice to indent things the way you like. This is generally very easy; I am picky about indentation and my emacs always does the right thing automatically. (That means that "indent-region" also always does what I want, which is very convenient.)

jrockway