Nate
Nate

Reputation: 7079

What is the difference between setq and set-variable in emacs?

What is the difference between setq and set-variable in emacs lisp. When should I use setq and when should I set-variable.

Upvotes: 41

Views: 14318

Answers (3)

unutbu
unutbu

Reputation: 879143

setq is a special form, while set-variable is an interactive function.

From the docs:

For me, the major use of the set-variable command is to suggest variables that I might want to set in my .emacs file. There are now more than 700 such variables -- far too many to remember readily. Fortunately, you can press <TAB> after calling the M-x set-variable command to see the list of variables.

Upvotes: 13

Drew
Drew

Reputation: 30701

Besides what was said above, for set-variable, as the doc string says:

    VARIABLE should be a user option variable name, a Lisp variable
    meant to be customized by users.

setq applies to any variable, whether or not it is a user option.

Upvotes: 4

Sean
Sean

Reputation: 29772

set-variable is an interactive command, meaning that you can type M-x set-variable RET to be interactively prompted for a variable name and value. setq is not an interactive command, meaning it's only suitable for writing in Emacs Lisp code. Personally, I never use set-variable in my Lisp code, only interactively, when I want to give a value to a variable that has an immediate effect on my text editing, such as (for example) setting indent-tabs-mode to t or nil.

Another difference is that setq can set multiple variables at once. For example, in my .emacs file on OS X I have:

(setq mac-command-modifier 'meta
      mac-option-modifier 'super)

set-variable can't do that.

Upvotes: 36

Related Questions