Emacs/docs/latest/emacs/Init-Examples
Next: Terminal Init, Previous: Init Syntax, Up: Init File [Contents][Index]
49.4.2 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 with M-x load-library. See Lisp Libraries.(add-to-list 'load-path "/path/to/lisp/libraries")
- Make TAB in C mode just insert a tab if point is in the middle of a line.
(setq c-tab-always-indent nil)
- Make searches case sensitive by default (in all buffers that do not override this).
(setq-default case-fold-search nil)
Specify your own email address, if Emacs can’t figure it out correctly.
(setq user-mail-address "[email protected]")
Various Emacs packages, such as Message mode, consult
user-mail-address
when they need to know your email address. See Mail Headers.- Make Text mode the default mode for new buffers.
(setq-default major-mode 'text-mode)
- Set up defaults for the Latin-1 character set, which supports most of the languages of Western Europe.
(set-language-environment "Latin-1")
- Turn off Line Number mode, a global minor mode.
(line-number-mode 0)
- Turn on Auto Fill mode automatically in Text mode and related modes (see Hooks).
(add-hook 'text-mode-hook 'auto-fill-mode)
- Load the installed Lisp library named
foo
(actually a filefoo.elc
orfoo.el
in a standard Emacs directory).(load "foo")
- Load the compiled Lisp file
foo.elc
from your home directory.(load "~/foo.elc")
Tell Emacs to find the definition for the function
myfunction
by loading a Lisp library namedmypackage
(i.e., a filemypackage.elc
ormypackage.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 theautoload
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 thet
or usenil
.Rebind the key C-x l to run the function
make-symbolic-link
(see Init Rebinding).(global-set-key "\C-xl" 'make-symbolic-link)
or
(define-key global-map "\C-xl" '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.
- Do the same thing for Lisp mode only.
(define-key lisp-mode-map "\C-xl" 'make-symbolic-link)
- Redefine all keys which now run
next-line
in Fundamental mode so that they runforward-line
instead.(substitute-key-definition 'next-line 'forward-line global-map)
- Make C-x C-v undefined.
(global-unset-key "\C-x\C-v")
- Make ‘
$
’ have the syntax of punctuation in Text mode. Note the use of a character constant for ‘$
’.(modify-syntax-entry ?\$ "." text-mode-syntax-table)
- Enable the use of the command
narrow-to-region
without confirmation.(put 'narrow-to-region 'disabled nil)
- 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:
(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"))
Next: Terminal Init, Previous: Init Syntax, Up: Init File [Contents][Index]