4

I'm trying to get rid of tabs, following this guide:

(setq-default indent-tabs-mode nil)

So: why setq-default and not setq? I know I would need to use setq-default for a buffer-local variable, but I can't actually determine (except from inferring from the guide) whether indent-tabs-mode is buffer local.

C-h v return something more confusing to me:

Automatically becomes buffer-local when set.

So it would become buffer local if I setq it, so I must setq-default it?

djechlin
  • 923
  • 8
  • 21
  • 1
    A particular major-mode and some minor-modes may set `indent-tabs-mode`, so `setq-default` is not a 100% guarantee that your buffer will have said default setting. Only if nothing else sets that variable differently when using a particular combination of major-mode and minor-modes would that default setting be respected. Thus, it may be necessary to use a major-mode hook to set the variable to your liking. Here is link to a recent example where the `tab-width` is set by a major mode to `4` and a major-mode hook is used to reset the value to `2`. http://emacs.stackexchange.com/a/10329/2287 – lawlist Mar 31 '15 at 23:38

1 Answers1

2

When you see Automatically becomes buffer-local when set you can rely on it meaning just what it says: whenever that variable is set (e.g., using setq), it becomes buffer-local if it was not already buffer-local. So yes, setq sets the buffer-local value, and if you want to set the global, default value then you need to use setq-default.

Drew
  • 75,699
  • 9
  • 109
  • 225
  • I thought as much, but `emacswiki` says: "You should ensure these variables have the same value, e.g., (setq tab-width..." That certainly can be interpreted in different ways. One of which is: "Use `setq` for `tab-width`, but `setq-default` for `indent-tabs-mode`" Which I assume to be wrong. Do you have any clues why they use `setq` for `tab-width`? – x-yuri Apr 13 '15 at 15:16
  • @x-yuri: Sorry, I don't know what you are quoting from, or who "they" is. The Emacs Wiki page cited by the OP doesn't say any of that. Use `setq-default` to set the global value, and `setq` to set a buffer-local value (assuming that the variable is buffer-local. – Drew Apr 13 '15 at 16:38
  • Sorry, I forgot to specify [the link](http://www.emacswiki.org/emacs/IndentationBasics). There `indent-tabs-mode` is set using `setq-default`, but `tab-width` is set using just `setq`. Do you have any clues why that is so? Both of them are buffer-local, as far as I can tell. Why not set both of them with `setq-default`? – x-yuri Apr 13 '15 at 16:53
  • As that page says: the first turns off `indent-tabs-mode` everywhere, by default. The second is in the context of "*The rest of this page will assume tabs are used..."* It is telling you how to deal with particular modes that allow the use of tab chars, i.e., buffers that overrule the default of using only spaces as set by the `setq-default` of `indent-tabs-mode`. To be clear, I didn't write that page, and I'm no expert on any of this. My reading of it is that it is telling you (a) how to turn off using tabs, by default and (b) how to allow tabs in some buffer/mode. – Drew Apr 13 '15 at 18:07