ARCHIVED: In Emacs, how can I change tab sizes?

This content has been archived, and is no longer maintained by Indiana University. Information here may no longer be accurate, and links may no longer be available or reliable.

To change the tab size in Emacs, add the following line to your .emacs file:

  (setq default-tab-width #)

Replace # with the number of spaces at which you would like to set your tabs. This will only affect the way your files look in Emacs; if you look at your files with another command or program (like the cat or more commands), the tabs will contain eight spaces. For instance, if you have tabs set to four spaces in Emacs, and write a C program using tabs, it would look like this in Emacs:

  main()
  {
      if (i==7)
          printf("lucky 7\n");
      else
          printf("try again\n");
      return 0;
  }

But if you use the cat command or the more command to look at it, it would look like this:

  main()
  {
          if (i==7)
                  printf("lucky 7\n");
          else
                  printf("try again\n");
          return 0;
  }

This is because standard tabs are set to eight spaces.

Tabs are special characters. If you use a tab, it does not insert as many characters as it takes to get to the 8th, 16th, or so on space. If you replace tabs with the appropriate number of spaces (with the untabify command in Emacs), the file you are working with will look the same when viewed with the cat, more, or vi commands, another Emacs configuration, etc. You can make the tabs convert to spaces before saving a document with M-x untabify, and the spacing will then look acceptable when you use the more or cat commands or when you print the document.

If you want to change the default indentation level when editing source code in Emacs, you should modify the c-basic-offset variable. For example, to cause Emacs C mode to indent four spaces, you would enter:

  M-x set-variable RET c-basic-offset RET 4 RET

Note: If you aren't sure how to enter these commands in Emacs, see How keystrokes are denoted in Emacs

At Indiana University, for personal or departmental Linux or Unix systems support, see Get help for Linux or Unix at IU.

This is document abde in the Knowledge Base.
Last modified on 2018-01-18 09:01:36.