Skip to main content
32 votes
Accepted

What's the difference between setq and defvar

You should ask Emacs this question: C-h i, choose the Elisp manual (m Elisp), and search the index for "defvar" (i defvar). That takes you to node Defining Variables. Emacs tells you that defvar ...
Drew's user avatar
  • 79.1k
17 votes
Accepted

Why do setq and set quote act differently on let-bound variables with lexical scope?

This is documented behaviour. The (much improved) explanation in the Emacs 25.1 elisp manual is as follows: Note that unlike dynamic variables which are tied to the symbol object itself, the ...
phils's user avatar
  • 53k
8 votes
Accepted

When use setq or set '

In the emacs init file in one case I use: (show-paren-mode t) show-paren-mode is a function. It accepts t as an argument. Using C-h f show-paren-mode, the description says, With a prefix ...
Lorem Ipsum's user avatar
  • 4,577
7 votes

When use setq or set '

As I understand it, Daniel's question was not only about the difference between set and setq, but also about why to use set/setq in some (and not all) cases. The fundamental difference between the two ...
Philopolis's user avatar
  • 1,152
6 votes
Accepted

Should "custom" variables defined without defcustom be set via custom-set-variables or setq?

Use customize-set-variable or custom-set-variables, depending on just what you want/need. Both of those make use of any initialization or setter functions (and so on) with which the user option might ...
Drew's user avatar
  • 79.1k
6 votes

Why do setq and set quote act differently on let-bound variables with lexical scope?

BTW, one way to think about why it can't work is to remember that lexical scoping enjoys the so-called "α-renaming" property: variable names do not matter, and you (or the compiler) can ...
Stefan's user avatar
  • 26.7k
6 votes
Accepted

What type of variable binding is setq creating?

Quoth the docstring of special-variable-p: special-variable-p is a built-in function in `src/eval.c'. (special-variable-p SYMBOL) Probably introduced at or before Emacs version 24.1. Return non-...
Basil's user avatar
  • 12.6k
6 votes

When use setq or set '

Emacs comes with the Elisp manual. Try C-h i m Elisp RET. Type i setq RET to go to the place where the index topic setq is explained. There you find the following info: Special Form: (setq [symbol ...
Tobias's user avatar
  • 33.7k
5 votes
Accepted

What's really behind an assignment in Emacs lisp?

setq is doing like expected, the thing here is, that (1 2 3 4) is not a value, so it is not what you think it is. a Place is a location in memory. x and y are Symbols. a Symbol merely points to a ...
jue's user avatar
  • 4,686
5 votes
Accepted

Setq doesn't work

When writing list or cons constants in source code there is actually a subtle difference between e. g. (cons 'first 'second) and '(first . second). The latter denotes a constant cons cell in your code,...
Oliver Scholz's user avatar
5 votes
Accepted

How to define a bundle of variable-and-function pairs?

make-symbol returns an uninterned symbol. That means you get a symbol with name, function cell, value cell, and property list but the symbol is not registered in the global obarray. Therefore, you ...
Tobias's user avatar
  • 33.7k
4 votes
Accepted

Inconsistency in emacs' rules of scope?

Lexical scope (recently added to emacs) changes the rules, and some documentation is still out of date. The current documentation for setq is this manual page which says When dynamic variable binding ...
amitp's user avatar
  • 2,591
3 votes
Accepted

I'm unsure which option to use for setting a variable: setq, customize-set-variable, or setopt

Think of the new (Emacs 29+) setopt macro as being a version of setq-default1 for use with user options2. Specifically, it accepts arguments in the exact same manner as setq-default (and setq) -- ...
phils's user avatar
  • 53k
3 votes

Backup a variable and store it back

Just use let. Write something like this: (let ((foo 323)) …) Where the ellipsis is, place whatever code you wanted to run with foo set to 323. Once that code finishes, foo will go back to having ...
db48x's user avatar
  • 19.1k
3 votes
Accepted

vc-mode doesn't evaluate when mode-line-format is set with setq-default

When the code in your init file runs, there are no open files at all. vc-mode is only set when a file is finally opened and determined to be in version–controlled. This means that when it runs your ...
db48x's user avatar
  • 19.1k
2 votes
Accepted

Bind function to variable and evaluate each time variable is referenced

You seem to want the variable counter to hold two things at once: instructions to increment the counter, and the current value of the counter. You can't have both at the same time. There are a few ...
Gilles 'SO- stop being evil''s user avatar
2 votes

Bind function to variable and evaluate each time variable is referenced

The effect that (eval counter) increments counter in some sense can be reached by setting counter to a quoted closure call. The eval evaluates the closure, the closure captures the actual counter and ...
Tobias's user avatar
  • 33.7k
2 votes

Top-level variables, local variables, variable scoping and the difference between set and setq

Looking at Using Lexical Binding it appears that a needs to be defined special which in using setq it is not. Modifying your example to use defvar gives the answers expected. ;;; -*- lexical-binding:...
alls0rts's user avatar
  • 376
2 votes
Accepted

Activate a minor mode switching off the other ones in a set of minor modes

Compare this. The essential difference is using set instead of setq. (defun activate-mode-in-list (minor-mode minor-mode-list) (dolist (mm minor-mode-list) (set mm (eq mm minor-mode)))) Your ...
Drew's user avatar
  • 79.1k
2 votes

Cannot get a symbol's value for the first time when using `setq-default`

Put the make-local-variable after the setq-default: (with-temp-buffer (setq-default bar "xyz") (make-local-variable 'bar) (symbol-value 'bar)) setq-default sets the global value. ...
Drew's user avatar
  • 79.1k
2 votes
Accepted

How to set a variable name with a variable?

intern turns a string into a symbol, then you can use set on that symbol: (set (intern "var") "some value") var ⇒ "some value" or (setq varname "var") (set (...
Arch Stanton's user avatar
  • 1,777
1 vote

How to set a variable name with a variable?

The variable needs to be a symbol, not a string: my/variable, not "my/variable". (let ((myvar 'my/variable) (myval "Some Value")) (set myvar myval) (message "MYVAR: %...
Drew's user avatar
  • 79.1k
1 vote

Modify value of a variable by applying a function to it?

Yes. setq sets a variable to whatever value is returned by the second argument. (setq VAR VALUE) sets variable VAR to whatever VALUE is, and the second argument, VALUE is always evaluated. (The first ...
Drew's user avatar
  • 79.1k
1 vote
Accepted

setq in init file not overriding the default for indent-tabs-mode

You ask, "assuming the local variable takes precedence, why would changing the default value have an effect, but not changing the local value?" If you use setq in your init file, and if the variable ...
Drew's user avatar
  • 79.1k
1 vote

init.el and trampling of custom-set-variables

Is it possible to use require (or similar) to organize my config into a modular structure of directories, without running into this issue? Yes. Don't add custom-set-variables to multiple places in ...
Tyler's user avatar
  • 22.8k
1 vote

init.el and trampling of custom-set-variables

It sounds like you have done both of the following for some options, i.e., you've done them both for at least one option: Used Customize to set and save the option. Tried to call customize functions ...
Drew's user avatar
  • 79.1k
1 vote
Accepted

How to set value vc-git-grep-template within function

vc-git-grep is an interactive function which prompts, so when you want to call it from your own code you need to use call-interactively, otherwise it expects you to have supplied its required ...
rpluim's user avatar
  • 5,585
1 vote

How to define a bundle of variable-and-function pairs?

@Tobias nice. Here still a command making new entries: (defun vl/new-pathsfunc () (interactive) (let ((directory (read-from-minibuffer "directory: " (substring default-directory 2))) ...
Andreas Röhler's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible