Removing tab inconsistencies in Emacs

emacs

To set tabs in emacs I have this line in my .emacs:

(global-set-key (kbd "TAB") 'tab-to-tab-stop)

I'm looking for some way to make all modes show tabs in emacs as 4 spaces and have emacs save the tabs as tab characters (instead of saving them as spaces).

If I'm using c-mode that .emacs line will make tabs look like 8 spaces and save them as a tab character. But in ada-mode enter will auto-indent (which I'm ok with) and it will appear as 4 spaces in emacs and save as four spaces.

Does anyone know how to universally set tabs to insert one tab (and no spaces) when the tab key is pressed and have it appear on emacs as four spaces?

I've also tried:

(setq tab-width 4)

but I still had the same problem with ada-mode.

Best Answer

You can't really do it for all modes as there are mode-specific indentation variables, but you can set it it for all of the languages you care about. For C, something akin to the following in your .emacs should work for what you describe:

(add-hook 'c-mode-common-hook`
    (lambda ()
        (setq c-basic-offset 4)
        (setq tab-width 4)
        (setq standard-indent 4)
        (setq c-tab-always-indent t)
    )
)

That will set up tab stops at 4 characters and make 4 the default indentation level for all C-style modes. For other languages and their respective modes, you have to look up their indentation variables and set them accordingly in that mode's common hook. Some examples include 'sh-indentation, 'tcl-indent-level, and 'perl-indent-level. The easiest way to figure out what needs to be set is to run:

M-x describe-key [TAB]

That should send you down the rabbit hole.

Cheers!
Sean

Related Topic