How to set Emacs tabs to spaces in every new file?

12,456

Solution 1

add this in your .emacs:

(setq-default indent-tabs-mode nil)

or you can define a before-save-hook that eliminate hard tabs

Solution 2

Also of use, M-x untabify, which will convert all the tabs into spaces in the current region. You can use this to get rid of the existing tabs in files you've edited before you had the indent-tabs-mode set properly.

C-x h                   (M-x mark-whole-buffer)
M-x untabify
Share:
12,456
Peter
Author by

Peter

SOreadytohelp Make everything as simple as possible.. (but not simpler!) Albert Einstein The irrationality of a thing is no argument against its existence, rather a condition of it. Friedrich Nietzsche

Updated on June 03, 2022

Comments

  • Peter
    Peter about 2 years

    I would like to have an .emacs setting so that tabs are always formed by consecutive spaces. Preferably in each possible mode. In other editors it never seemed a problem, but in .emacs I'm a bit stuck with the tabs I'm afraid.

  • Peter
    Peter about 15 years
    I don't know who voted you down, it works fine for me, at least in textmode, other modes I haven't tried. Tx.
  • Peter
    Peter about 15 years
    +1 for usefull indeed,(although I used that exact combination of commands already :-))
  • Jesper.Reenberg
    Jesper.Reenberg over 12 years
    @dfa, that might not be a good idea. For example if you are editing make file :)
  • nnyby
    nnyby over 10 years
    I'm doing this in ruby-mode/rspec-mode and emacs is still converting some of my spaces to tabs..
  • nnyby
    nnyby over 10 years
    Nevermind -- I got around this with an "untabify on save" hook -- emacswiki.org/emacs/UntabifyUponSave
  • Reb.Cabin
    Reb.Cabin over 6 years
    It would be great to have the exact syntax for the before-save-hook, please & thanks