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.
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.
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 theM-x set-variable
command to see the list of variables.
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.
精彩评论