2

I want to change indent-tabs-mode and electric-indent-mode to nil. There exist three possibilities

  1. setq-default
  2. setq-local
  3. setq

I think setq-default only changes the behaviour for major modes where the variable indent-tabs-mode has not been set.

I just want to change them for a particular buffer when I wish.

Drew
  • 75,699
  • 9
  • 109
  • 225
Dilna
  • 1,173
  • 3
  • 10

1 Answers1

2

Again, you can use C-h f to look up the documentation for each of these functions. When you do, you will find that setq-local makes a variable buffer–local and then sets the value of that variable in the current buffer. It would work just fine for you, but since the variables you care about are already buffer–local, it isn’t necessary.

setq-default sets the default value for a buffer–local variable, which is the global value that is used when a buffer has no specific value for a variable. Since this would affect many buffers rather than a specific one, this isn’t what you want.

99% of the time you just want setq. This is one of those times. It sets the buffer–local value of the variable, if the variable is buffer–local, and otherwise sets the global value if the variable isn’t buffer–local.

Note that there are many other ways to set variables that you didn’t mention. The most primitive version is actually set. setq adds the ability to set multiple variables at a time: (setq VAR1 VAL1 VAR2 VAL2 VAR3 VAL3…). Another favorite is setf, which builds upon setq to allow you to set the value of any PLACE, rather than just any variable. A PLACE expression is any expression that would retrieve a value. The canonical example is that (car x) retrieves the first element of x, so (setf (car x) 42) sets that first element to 42.

Finally, there is the customization system. You can type M-x customize to get a nice UI that lists all of Emacs’s variables that you might want to change, grouped into categories and sub–categories and presented alongside their documentation. Customize lets you see the current value, edit the value, and save the value for future Emacs sessions without actually opening up your init file and writing any Lisp. It also validates the values that you input, and for most variables can provide a much nicer editing experience than raw lisp.

db48x
  • 15,741
  • 1
  • 19
  • 23
  • Customization System does not work very well for me because I like to organise my emacs tools in self managed `el` files. – Dilna Jun 13 '22 at 02:04
  • But it is important to mention, because most people will want to use it. Answers need to help everyone who wanders by looking for information. – db48x Jun 13 '22 at 02:18