Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs now defaults to 2-space soft tabs?

Tags:

c

emacs

I haven't used Emacs in years, and trying a fairly new one (23.4.1 on Linux) I notice that the mixed tab style I remember seems to no longer be the default indentation. It used to have 4 spaces as the first tab level, then a tab as the next tab level, then a tab plus four spaces, etc etc. I am editing a .c file.

Does Emacs still do this by default at all or are such defaults a thing of the past and is it now much easier to use Emacs for those of us who use hard tabs or soft tabs but never mixed tabs and who use 2-space tabs in some cases and 4-space in others?

UPDATE 2: Actually it's not doing 2-space soft tabs exactly, but rather once I get to the magic number of 8 spaces, then it sticks in a tab just as it always did before. So no big change and this mixed tab business is apparently still in full effect just as @legoscia says!

UPDATE: I've tried this now on three systems - Emacs v25.x on Windows 7, v24.x on Ubuntu 12.x, and v23.x on Xubuntu 15.x and all of them are doing 2-space soft tabbing when I'm in "c-mode." My question is about the defaults and how they've changed over the last let's say 5 years.

like image 407
user62177541 Avatar asked Oct 25 '25 00:10

user62177541


2 Answers

That's still what Emacs does by default: each level of indentation is 4 spaces, tabs are considered to be 8 spaces wide, and Emacs uses the minimum amount of tabs and spaces to make up the indentation for each line.

This is affected by the following variables:

  • indent-tabs-mode: this defaults to t. If it is nil, Emacs uses only spaces to indent to the desired depth.
  • tab-width: this defaults to 8. This affects how wide Emacs thinks a tab is.
  • c-basic-offset et al. This determines the indentation step for each level in C files (also C++, Java and other languages using modes based on cc-mode). Often you'll want to type C-c . to use a set of settings for a particular indentation style instead.

Usually you want to set these variables individually for a certain file or for a certain project. To set them for a single file, you can include a magic comment to set "file variables". To set variables for every file in a directory and its sub-directories, place a .dir-locals.el file in the root directory of the project, and specify per-directory local variables.

One thing that Emacs doesn't directly support is the notion of using tabs exclusively for indentation. You could emulate this by setting c-basic-offset (or the corresponding variable for other languages) and tab-width to the same value, but you might be better served by Smart Tabs, as Carsten suggested in the comments.

like image 108
legoscia Avatar answered Oct 26 '25 13:10

legoscia


Adding this to my .emacs file did the trick:

(setq-default tab-width 4)
(setq-default indent-tabs-mode nil)
(setq-default c-basic-offset 4)
like image 21
Matt Avatar answered Oct 26 '25 15:10

Matt