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
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).
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
(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 () (interactive) (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.