Here are some examples of doing certain commonly desired things with Lisp expressions:
load-path
. You can then put
Lisp libraries that are not included with Emacs in this directory, and
load them with M-x load-library. See Libraries of Lisp Code for Emacs.
(add-to-list 'load-path "/path/to/lisp/libraries")
(setq c-tab-always-indent nil)
Here we have a variable whose value is normally t
for “true”
and the alternative is nil
for “false”.
(setq-default case-fold-search nil)
This sets the default value, which is effective in all buffers that do
not have local values for the variable (see Local Variables). Setting
case-fold-search
with setq
affects only the current
buffer’s local value, which is probably not what you want to do in an
init file.
(setq user-mail-address "cheney@torture.gov")
Various Emacs packages, such as Message mode, consult
user-mail-address
when they need to know your email address.
See Mail Header Fields.
(setq-default major-mode 'text-mode)
Note that text-mode
is used because it is the command for
entering Text mode. The single-quote before it makes the symbol a
constant; otherwise, text-mode
would be treated as a variable
name.
(set-language-environment "Latin-1")
(line-number-mode 0)
(add-hook 'text-mode-hook 'auto-fill-mode)
(setopt selection-coding-system 'utf-8)
(load "foo")
When the argument to load
is a relative file name, not starting
with ‘/’ or ‘~’, load
searches the directories in
load-path
(see Libraries of Lisp Code for Emacs).
(load "~/foo.elc")
Here a full file name is used, so no searching is done.
myfunction
by loading a Lisp library named mypackage (i.e., a file
mypackage.elc or mypackage.el):
(autoload 'myfunction "mypackage" "Do what I say." t)
Here the string "Do what I say."
is the function’s
documentation string. You specify it in the autoload
definition so it will be available for help commands even when the
package is not loaded. The last argument, t
, indicates that
this function is interactive; that is, it can be invoked interactively
by typing M-x myfunction RET or by binding it to a key.
If the function is not interactive, omit the t
or use
nil
.
make-symbolic-link
(see Rebinding Keys in Your Init File).
(keymap-global-set "C-x l" 'make-symbolic-link)
or
(keymap-set global-map "C-x l" 'make-symbolic-link)
Note once again the single-quote used to refer to the symbol
make-symbolic-link
instead of its value as a variable.
(keymap-set lisp-mode-map "C-x l" 'make-symbolic-link)
next-line
in Fundamental mode
so that they run forward-line
instead.
(keymap-substitute global-map 'next-line 'forward-line)
(keymap-global-unset "C-x C-v")
One reason to undefine a key is so that you can make it a prefix. Simply defining C-x C-v anything will make C-x C-v a prefix, but C-x C-v must first be freed of its usual non-prefix definition.
(modify-syntax-entry ?\$ "." text-mode-syntax-table)
narrow-to-region
without confirmation.
(put 'narrow-to-region 'disabled nil)
Users typically want Emacs to behave the same on all systems, so the same init file is right for all platforms. However, sometimes it happens that a function you use for customizing Emacs is not available on some platforms or in older Emacs versions. To deal with that situation, put the customization inside a conditional that tests whether the function or facility is available, like this:
(if (fboundp 'blink-cursor-mode) (blink-cursor-mode 0)) (if (boundp 'coding-category-utf-8) (set-coding-priority '(coding-category-utf-8)))
You can also simply disregard the errors that occur if the function is not defined.
(ignore-errors (set-face-background 'region "grey75"))
A setq
on a variable which does not exist is generally
harmless, so those do not need a conditional.
use-package
to automatically load and configure a
package.
(use-package hi-lock :defer t :init (add-hook 'some-hook 'hi-lock-mode) :config (use-package my-hi-lock) :bind (("M-o l" . highlight-lines-matching-regexp) ("M-o r" . highlight-regexp) ("M-o w" . highlight-phrase)))
This will load hi-lock
when some of its commands or variables
are first used, bind 3 keys to its commands, and additionally load the
my-hi-lock
package (presumably further customizing
hi-lock
) after loading hi-lock
. The use-package
facility is fully documented in its own manual, see use-package User manual.