Why might my Emacs use spaces instead of tabs?

一笑奈何 提交于 2019-11-28 12:58:15

Tabs and indentation in Emacs is a considerably more complex subject than most people anticipate. I highly recommend spending some time reading about it -- it will almost certainly save you some confusion in the long run.

The following page at the Emacs Wiki groups together most of the relevant discussion: http://www.emacswiki.org/emacs/CategoryIndentation

There's quite a lot there, but it's worth looking through.

One or other of the TabsAreEvil and SmartTabs configurations is quite likely to be appealing to you, btw, depending on your personal opinions on the subject!

Make sure you read the page on the tab-stop-list variable. It's tucked away near the bottom of that list of links, but it's critical to understanding the behaviour of tabs in the absence of automated-indentation rules, along with things like 'tabify'.

ruler-mode is useful here as well. I enable it automatically with text-mode:

;; Use ruler in text-mode
(add-hook 'text-mode-hook
          (function (lambda ()
                      (setq ruler-mode-show-tab-stops t)
                      (ruler-mode 1))))

I figured out the problem. It was inserting a tab character after all. It turns out I thought it wasn't because when I hit backspace that key is bound to c-electric-backspace, which looks at the variable c-backspace-function which was set to backward-delete-char-untabify, which IMO defeats the purpose of having tabs.

Check tab-width variable. If it is 8 (the default), then Emacs of course has to insert four spaces since a tab would be "too much".

Check of the file for Emacs "File Local Variables". These specially formatted lines can override your settings when that file is loaded.

Here is an example from the bottom of a bit of Ruby code, forcing indent to 2 spaces, and tabs converted to spaces:

# Local Variables:
# tab-width: 2
# ruby-indent-level: 2
# indent-tabs-mode: nil
# End:

Be sure to take a look at the first line of the file as well. If you see something like // -- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -- This line will override any global or mode settings.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!