Is there a way to make Emacs indent code using only tabs globally and not spaces. I know of tabify command. I would prefer the automated indentation to use tabs rather than a mixture of tabs and spaces as the default configuration does.


Well, to indent with tabs, you'll want to ensure that indent-tabs-mode is t (which is the default value). But to get only tabs, you'd need to adjust the mode that's doing the indentation such that every line is indented by a multiple of your tab-width (default 8). For example, in C mode, you could try setting c-basic-offset to 8.

Or, you could reduce your tab-width to a smaller value, so that Emacs won't need to use spaces to reach each level of indentation. But if you do that, everybody else who has to work on your code (and uses the standard 8-character tab stops) will get annoyed.

I urge you to reconsider. Personally, I find working on code indented with tabs to be annoying, especially if they don't appear to be 8 space tabs. (The very worst is code where some lines use spaces, some lines use tabs, and it's clearly not 8 space tabs.) Do us all a favor and just (setq indent-tabs-mode nil).

  • I woud mark this question up for the second paragragh setting tab width and the comment don't mix tabs and spaces. But if your tab width is set the indentation editing is much easier with a tab for each level rather than several spaces – Mark Oct 6 '11 at 11:26
  • I think your third paragraph is generally helpful, but often we don't get to control the indentation style of the original author, e.g., to whom we are submitting pull/merge requests. I have my preferences, they have theirs. I think it's respectful to use their indentation style to a reasonable level, so occasionally the choice is not ours to make. – r2evans May 6 '17 at 21:49

Emacs comes with several predefined styles for C indentation, or you can write your own if none suits your needs. Amongst the standard styles, it seems that you're looking for linux (the style used in the Linux kernel source), which has indents of 8 columns and uses tab characters of width 8.

You can make these settings your default style for C by putting this in your .emacs:

(setq c-default-style "linux")
(defun my-c-mode-hook ()
  (setq indent-tabs-mode t)
  (setq tab-width 8))
(add-hook 'c-mode-hook 'my-c-mode-hook)

Make sure that you don't use these settings when collaborating with other people with different preferences, though. In particular, using tabs will make you enemines. If a particular file is to be kept in a particular style, you can use file variables to ensure that everyone who edits the file in Emacs uses the same settings:

 * Local Variables:
 * c-file-style: "linux"
 * indent-tabs-mode: t
 * tab-width: 8
 * End:

If you have a whole project with similarly-indented C files, use directory variables rather than pollute every file. Create a file called .dir-locals.el with contents like the following:

((c-mode . ((c-file-style . "linux")
            (indent-tabs-mode . t)
            (tab-width . 8))))

Tabs annoy me, but I had to figure out how to make emacs use only tabs to indent so I could work on someone else's code. Here's what worked for me:

(defun tmp/change-indent ()
  (setq indent-tabs-mode t)
  (setq tab-width 2))

I noticed that before messing with anything, when I pressed the tab key, it auto indented by 2 columns. Setting "tab-width" to match the 2 column offset allowed emacs to use tabs when doing auto-format. I was working in nxml mode, but I think this should work for any mode.

  • Nice. This has bothered me too. It's not intuitive to have to set the tab width to match the column offset. Good work. I used this idea in c++-mode – Sean Lynch Feb 5 '14 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.