0

I have a problem with tab size. It is always 2 chars but I want 4.

My code:

(defun my-c++-mode-hook ()
    (set (make-local-variable 'compilation-parse-errors-filename-function)
  'process-error-filename)
    (local-set-key (kbd "C-c b") 'compile)       ; KBD
    (setq compile-command "scons")
    (setq indent-tabs-mode nil)
    (setq tab-width 4)
    (setq c-basic-indent 4)
    )
(add-hook 'c++-mode-hook 'my-c++-mode-hook)
(add-hook 'c-mode-common-hook 'my-c++-mode-hook)

So. When I'm typing:

void f() {
  // Here I need 4 chars but I'm getting only 2 when I'm pressing TAB
}
denys
  • 2,437
  • 6
  • 31
  • 55

2 Answers2

1

Correct answer I've found at Post:

(setq c-basic-offset 4)

But I still do not understand what is (setq c-basic-indent 4) for and why so much suggestions to use it in the internet?

Community
  • 1
  • 1
denys
  • 2,437
  • 6
  • 31
  • 55
  • I suspect one prominent source for `c-basic-indent` is Jamie Zawinski's "Tabs versus Spaces: An Eternal Holy War". From at least [May 2000](http://web.archive.org/web/20000512004347/http://www.jwz.org/doc/tabs-vs-spaces.html) to [August 2004](http://web.archive.org/web/20040825090214/http://www.jwz.org/doc/tabs-vs-spaces.html) it was suggesting `c-basic-indent`. At some point before [October 2004](http://web.archive.org/web/20041010002433/http://www.jwz.org/doc/tabs-vs-spaces.html) it was changed to `c-basic-offset`. (The current version is here: https://www.jwz.org/doc/tabs-vs-spaces.html) – legoscia Jun 05 '19 at 14:43
1

basic offset means other indentations are based on it. So,

for () {
....if () { // 4 spaces
........ // 8 spaces
....}
}

to cite Gnue Emacs

This style variable holds the basic offset between indentation levels

So you won't get:

for () {
....if () { // 4 spaces
...... // 6 spaces
....}
}

But of course you could do that if you want.

And usually, it's recommended to use spaces instead of tabs:

(setq-default indent-tabs-mode nil)

Use M-x untabify to do that for a specific buffer.

gongzhitaao
  • 6,566
  • 3
  • 36
  • 44