Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change tab width in emacs

Tags:

emacs

I added lines from this website https://www.emacswiki.org/emacs/IndentationBasics to my ~/.emacs file:

  (setq-default indent-tabs-mode nil)
  (setq tab-width 4)
  (defvaralias 'c-basic-offset 'tab-width)

And it made my tabs in .h files and .cpp files 8 spaces long (before they were 2 spaces long). How to make them 4 spaces long?

like image 843
spiderface Avatar asked May 08 '16 22:05

spiderface


People also ask

How do I change the width of a tab in Emacs?

The tab character. Normally, the width of tab is eight characters, so a file created by a `vi' user with a tab width of 4 will look different when viewed in a program which uses the default tab width setting. If you only want this in a particular mode, add (setq tab-width 4) to that mode's hook.

How do I change the indent in Emacs?

Note: This step is unnecessary in version 20 and all subsequent versions of Emacs. Move to the end of the block you want to indent and set the mark by pressing C-SPC . Move the cursor to the first line of the block, and indent this line however far you want all the lines in the block to be indented. Press M-C-\ .

How many spaces is a tab in Emacs?

This is because standard tabs are set to eight spaces. Tabs are special characters.

How do I replace tabs with spaces in Emacs?

To replace tabs with the appropriate number of spaces, use M-x untabify . To do the reverse and convert multiple spaces to tabs, you can use M-x tabify . Both commands work on a region. To run on the whole buffer use a prefix argument (i.e. C-u M-x untabify ).


1 Answers

Both tab-width and c-basic-offset variables are buffer-local, which means it is effective only in the buffer you set it. Emacs only evaluates ~/.emacs at start up, and it is only effective in that file.

To set a default value for all buffers, you need

(setq-default tab-width 4)
like image 102
Colin Yang Avatar answered Nov 09 '22 13:11

Colin Yang