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?
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.
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-\ .
This is because standard tabs are set to eight spaces. Tabs are special characters.
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 ).
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With