Init File Examples
Here are some examples of doing certain commonly desired things with Lisp expressions:
- Add a directory to the variable
load-path. You can then put Lisp libraries that are not included with Emacs in this directory, and load them withM-x load-library. Lisp Libraries. @example (add-to-list 'load-path "/path/to/lisp/libraries") @end example - Make
TABin C mode just insert a tab if point is in the middle of a line. @example (setq c-tab-always-indent nil) @end example Here we have a variable whose value is normallytfor ``true'' and the alternative isnilfor ``false''. - Make searches case sensitive by default (in all buffers that do not override this). @example (setq-default case-fold-search nil) @end example This sets the default value, which is effective in all buffers that do not have local values for the variable (Locals). Setting
case-fold-searchwithsetqaffects only the current buffer's local value, which is probably not what you want to do in an init file. - Specify your own email address, if Emacs can't figure it out correctly. @example (setq user-mail-address "cheney@@torture.gov") @end example Various Emacs packages, such as Message mode, consult
user-mail-addresswhen they need to know your email address. Mail Headers. - Make Text mode the default mode for new buffers. @example (setq-default major-mode 'text-mode) @end example Note that
text-modeis used because it is the command for entering Text mode. The single-quote before it makes the symbol a constant; otherwise,text-modewould be treated as a variable name. @need 1500 - Set up defaults for the Latin-1 character set, which supports most of the languages of Western Europe. @example (set-language-environment "Latin-1") @end example @need 1500
- Turn off Line Number mode, a global minor mode. @example (line-number-mode 0) @end example @need 1500
- Turn on Auto Fill mode automatically in Text mode and related modes (Hooks). @example (add-hook 'text-mode-hook 'auto-fill-mode) @end example
- Change the coding system used when using the clipboard (Communication Coding). @example (setopt selection-coding-system 'utf-8) @end example
- Load the installed Lisp library named
foo(actually a filefoo.elcorfoo.elin a standard Emacs directory). @example (load "foo") @end example When the argument toloadis a relative file name, not starting with/or~,loadsearches the directories inload-path(Lisp Libraries). - Load the compiled Lisp file
foo.elcfrom your home directory. @example (load "~/foo.elc") @end example Here a full file name is used, so no searching is done. - Tell Emacs to find the definition for the function
myfunctionby loading a Lisp library namedmypackage(i.e., a filemypackage.elcormypackage.el): @example (autoload 'myfunction "mypackage" "Do what I say." t) @end example Here the string"Do what I say."is the function's documentation string. You specify it in theautoloaddefinition 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 typingM-x myfunction =RET=or by binding it to a key. If the function is not interactive, omit thetor usenil. - Rebind the key
C-x lto run the functionmake-symbolic-link(Init Rebinding). @example (keymap-global-set "C-x l" 'make-symbolic-link) @end example or @example (keymap-set global-map "C-x l" 'make-symbolic-link) @end example Note once again the single-quote used to refer to the symbolmake-symbolic-linkinstead of its value as a variable. - Do the same thing for Lisp mode only. @example (keymap-set lisp-mode-map "C-x l" 'make-symbolic-link) @end example
- Redefine all keys which now run
next-linein Fundamental mode so that they runforward-lineinstead. @example (keymap-substitute global-map 'next-line 'forward-line) @end example - Make
C-x C-vundefined. @example (keymap-global-unset "C-x C-v") @end example One reason to undefine a key is so that you can make it a prefix. Simply definingC-x C-v /anything/will makeC-x C-va prefix, butC-x C-vmust first be freed of its usual non-prefix definition. - Make
$have the syntax of punctuation in Text mode. Note the use of a character constant for$. @example (modify-syntax-entry ?\$ "." text-mode-syntax-table) @end example - Enable the use of the command
narrow-to-regionwithout confirmation. @example (put 'narrow-to-region 'disabled nil) @end example - Adjusting the configuration to various platforms and Emacs versions. 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: @example (if (fboundp 'blink-cursor-mode) (blink-cursor-mode 0)) @c FIXME: Find better example since `set-coding-priority' is removed. (if (boundp 'coding-category-utf-8) (set-coding-priority '(coding-category-utf-8))) @end example You can also simply disregard the errors that occur if the function is not defined. @example (ignore-errors (set-face-background 'region "grey75")) @end example A
setqon a variable which does not exist is generally harmless, so those do not need a conditional. - Using
use-packageto automatically load and configure a package. @example (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))) @end example This will loadhi-lockwhen some of its commands or variables are first used, bind 3 keys to its commands, and additionally load themy-hi-lockpackage (presumably further customizinghi-lock) after loadinghi-lock. Theuse-packagefacility is fully documented in its own manual, Top.