Navigation: Table of Contents, Index, next: Elisp Installation HOWTO, prev: Emacs
Overview:
This page is the ideal read if you worked your way through the Emacs tutorial, used Emacs for a few days, and now you think the time has come to do some customizing.
Beware, however: Some of the things presented on this page can also be achieved by using Customize. Check it out by choosing the menu Help / Customize / Top-level Customization Group. Customize is for the users that don't want to learn elisp. There are some exceptions to this rule, but if you would like to learn elisp, there is no better way to start than by editing your own .emacs file without the aid of Customize.
EMacro
This is a package which strives to provide the only .emacs file you will ever need. If you don't feel like learning lisp but want to use Emacs none the less, give it a try.
The convention used by the Emacs community for key presses:
Emacs has also been called The Self Documenting Editor. I think you start to see why. Many of the comments on this page will refer you to the documentation of a function or variable. The two commands in the list above will help you read them.
The third command in the list above will help you evaluate a lisp function after typing it in. Another word for `evaluating' might be `executing' or `calling'. In our cases, the lisp expressions you will want to evaluate are functions, and all lisp functions are written in parentheses. Therefore you will have to put point after the closing parenthesis and hit C-x C-e to evaluate the function.
When you start Emacs, .emacs is loaded and all the code in it evaluated. Therefore C-x C-e is only needed if you are typing the elisp code as you read this page and want to evaluate it immediately (instead of putting it into your .emacs, saving the file and restarting Emacs).
As you read the elisp code on this page, note that a semikolon starts a comment.
If you get an elisp file from the Internet or from a friend, you will need to put it in a place Emacs can find. Emacs needs to know this in case you want to load such a file from your .emacs. This is therefore the first thing I have in my .emacs file:
;;; Paths and directories (add-to-list 'load-path "~/elisp")
This will add the ~/elisp directory to my load-path. The variable load-path is a list of directories which Emacs will search when it tries to load a library. Any elisp file you get from the Internet or from friends can be copied into the ~/elisp directory and loaded from there.
Note that elisp files are often refered to as packages or libraries. Note also, that with XEmacs, a package may be a collection of elisp files that you can install using a package manager.
The command add-to-list does just what is says: It takes the first argument and checks wether the second argument is already part of the list, and if it is not, the second argument is prepended to the list.
You can examine the effect of this function by evaluating it first, and then getting help on the load-path variable.
For more on installing elisp files, see the Elisp Installation HOWTO.
The following sections list some files I like to load or files that I would like to automatically load as soon as I need some function in the file.
A very usefull elisp file is already part of the Emacs distribution. It allows me to read and write compressed files. Emacs will compress and uncompress the files on the fly. In order to activate this feature, all I have to do is load one elisp file:
;; read and write compressed files (require 'jka-compr)
The funtion require will load the library given as the argument only if the symbol with the same name is undefined. If the symbol is undefined, Emacs goes through each directory in the load-path variable, tries first to load a compiled version of the library by appending ".elc" to the library name, and then Emacs tries to load the source version of the library by appending ".el". If no file is found, the next directory in load-path is examined.
Once the file is loaded a symbol with the same name as the library is defined so subsequent calls to require will not load anything.
Note that the jka-compr library will be useless on Windows and Macintosh Systems unless you have the commandline compression and decompression utilities installed: gzip and gunzip, bzip2 and bunzip2, zip and unzip, etc.
I got this calculator elisp file from the Internet and saved it in my ~/elisp directory. The autoload function tells Emacs to load the calculator source file when I call the calculator function for the first time:
;; simple math using calculator (autoload 'calculator "calculator" "Simple calculator." t)
The following function allows filename completion in the buffer (file name completion in the minibuffer is usually available if you have to enter a file name in the minibuffer).
;; from comint.el: filename completion (autoload 'comint-dynamic-complete-filename "comint" "Complete filenames." t)
This is the most important one if you are using Emacs on a color display: it enables syntax hilighting.
;; Syntax hilighting!!
(global-font-lock-mode t)
The following line will disable beeping on errors. Instead, Emacs will flash the screen.
;; No beeping
(setq visible-bell t)
The following line will do away with the scrollbar!
Scrollbars are for sissies, anyway. Seriously, though, this can be
usefull if you want to choose on which side of the the frame
scrollbars ought to be. Try (set-scroll-bar-mode 'left)
and (set-scroll-bar-mode 'right)
instead.
;; No scrollbar
(set-scroll-bar-mode nil)
This little command will display the column number in the modeline, right after the line number.
;; Put column number into modeline
(column-number-mode 1)
This mode will highlight matching parenthesises or highlight mismatched parenthesises, depending on your parenthesizing skills. A must for lisp programmers.
;; Hilight matching parenthesis
(show-paren-mode 1)
The space below the modeline is either the echo area (if messages are displayed) or the minibuffer (if you ought to type something after a prompt). The minibuffer can grow if you need more than one line for prompt and answer. Very usefull. This little function allows the minibuffer to grow when required.
;; Make minibuffer larger if there is more to see
(resize-minibuffer-mode 1)
This makes Emacs keybindings a little bit more Windows like. You still can't use C-c, C-v and C-x to copy, paste and delete the selection, but you can at least use C-INS, S-INS, and S-DEL to do it.
;; Allow marking of the region using cursor keys and the shift key and other stuff
(pc-selection-mode)
The following function changes the way you switch buffers. You are presented with a list of buffers that shrinks as you type the name, only the matching buffers are shown, making buffer switching much easier.
;; iswitchb
(iswitchb-default-keybindings)
Some of the possible key bindings are reserved for users. C-c and a lower case plain letter are such reserved keybindings.
Here are some interesting functions, bound to keys reserved for users.
The following code fragment collects all the lines matching a regular expression in another buffer and lets you jump to the respective locations of all the matches.
;; C-c o finds all matches for a regular expression (global-set-key (kbd "C-c o") 'occur)
Switch to auto-fill mode and back, quickly. Usefull in HTML mode. Auto-fill is another word for automatic word-wrapping.
;; C-c q toggles auto-fill, also known as word wrap (global-set-key (kbd "C-c q") 'auto-fill-mode)
A quick key combination for the calculator I installed in my ~/elisp directory.
;; C-c c calls the calculator (global-set-key (kbd "C-c c") 'calculator)
Start a shell!
;; C-c d calls the shell (global-set-key (kbd "C-c d") 'shell)
This is one of the functions I autoloaded from the comint library (see above). It does filename completion in the normal buffer.
;; C-c f does filename completion (global-set-key (kbd "C-c f") 'comint-dynamic-complete-filename)
This redefines C-x C-b to a buffer menu I like better than the default one. There are even more variations of buffer menues around, but this one is good enough for me.
;; redefine C-x C-b for a better buffer menu (global-set-key (kbd "C-x C-b") 'buffer-menu)
A lot of commands having to do with frames start with C-x 5, but there is no key to make new frames. Therefore I chose to put that on C-x 5 1.
;; C-x C-5 1 creates a new frame (global-set-key (kbd "C-x 5 1") 'make-frame)
A lot of MULE related commands start with C-x RET, but there is no key to set the input method. Therefore I chose to put that on C-x RET i.
;; C-x RET i sets the input method (global-set-key (kbd "C-x RET i") 'set-input-method)
Another cool feature I like is dynamic expansion. This takes the word before point and checks all the buffer wether any other word started with the same fragment. Consecutive hitting of C-TAB will cycle through the various expansions. Note how I use the vector notation to get C-TAB to work. This will not work on a Linux console. See further down for a workaround. I suspect it will also not work for XEmacs, though I know of no workaround.
;; C-TAB cycles through the input methods
(global-set-key [C-tab] 'dabbrev-expand)
Every once in a while, you will want to rebind keys which you don't know how to access. Here are some of the things that helped me solve the problems.
If you are using the Linux console, chances are that the insert key does not work. Here's how to go about helping yourself. Try C-h k INS and see what Emacs thinks you typed. On my console, Emacs says insertchar is undefined.
;; fix missing INSERT key to toggle overwrite-mode on the console
(global-set-key [insertchar] 'overwrite-mode)
Another tough one. I wanted to use C-TAB on the console, but that doesn't even send a character to the console. Use the showkey(1) command to find out what the console is actually receiving. On my system it shows that one of the control keys I'm pressing has keycode 97 and that the tab key has the keycode 15.
Use the dumpkeys(1) command to find out what keycode 15 actually sends. You'll probably see two entries, both saying Tab. That means that TAB means Tab and S-TAB means Tab as well. What about C-TAB, however? You have to use a symbol which C-TAB can send. I looked through the output of dumpkeys for unknown symbols and found keycode 113, "Macro". What does Macro do? Further down I found that Macro was equivalent to the string "\033[M" -- well my keyboard doesn't have a Macro key so this is what I did: I defined C-TAB to send the Macro symbol to the console, and I defined within Emacs that "\033[M" was to do whatever I wanted C-TAB to do:
In order to make C-TAB equivalent to Macro, I had to edit my .profile file. The following code checks wether loadkeys(1) is there (my copy resides in /usr/bin) and if the .keymap file exists. The .keymap file will contain the redefinition of C-TAB.
# If console, change some keymaps if [ $TERM = 'linux' -a -x /usr/bin/loadkeys -a -f .keymap ] ; then loadkeys .keymap 2> /dev/null fi
The .keymap file contains the following code:
# this only works for console!
# allow control tab
control keycode 15 = Macro
And my .emacs contains the following:
;; This requires special setup by calling loadkeys(1) from .profile! (global-set-key (kbd "ESC [ M") 'dabbrev-expand)
I've since started to add other stuff to my .keymap file: control addow keys, control home, control end, etc. Note that the keycode numbers might vary; I got them reading the output of dumpkeys(1).
# control up down left right home end
control keycode 103 = Meta_p
control keycode 108 = Meta_n
control keycode 105 = Meta_Control_b
control keycode 106 = Meta_Control_f
control keycode 102 = Meta_less
control keycode 107 = Meta_greater
Time and time again, I realize that things are possible withing Emacs because some elisp function will do what I need, but the elisp function needs a few lines of extra code around it so that I cannot just bind it to a key. The following section will define a few usefull little functions, not necessarily binding them to keys.
The following function is not bound to a key. Call it by typing M-x indirect-buffer RET. An indirect buffer is a second buffer for the same file. The good thing about the indirect buffer is that it can have a major mode different from the first buffer visiting the same file. This is great if you have C code intermixed with SQL statements, or HTML intermixed with Perl code. Just create an indirect buffer and put it in the major mode you need (by typing M-x sql-mode RET, for example).
;; create an indirect buffer (defun indirect-buffer () "Edit stuff in this buffer in an indirect buffer. The indirect buffer can have another major mode." (interactive) (let ((buffer-name (generate-new-buffer-name "*indirect*"))) (pop-to-buffer (make-indirect-buffer (current-buffer) buffer-name))))
Here is another function which I started using so often I bound it to a key. Now I can use C-c i to insert the current date.
;; Insertion of Dates. (defun insert-date-string () "Insert a nicely formated date string." (interactive) (insert (format-time-string "%Y-%m-%d"))) ;; C-c i calls insert-date-string (global-set-key (kbd "C-c i") 'insert-date-string)
If you are a programmer that works with the latest programming languages, there will come a time when even Emacs does not support the language out of the box. You might get a library from the Internet or from a friend, however, which provides a new major mode to deal with the language.
You know how to let Emacs know where to find the file. See the section on where to put your elisp files for more information. Here is what you need to do in order to use python-mode for all your .py files in addition to getting the file python-mode.el and copying it into a directory in your load-path.
;; Python (autoload 'python-mode "python-mode" "Python editing mode." t) (add-to-list 'auto-mode-alist '("\\.py$" . python-mode)) (add-to-list 'interpreter-mode-alist '("python" . python-mode))
This does three things:
.pyis edited.
#!. The entry we are adding lets Emacs call the function python-mode whenever a file for the python interpreter is edited.
Another interesting major mode is cperl-mode. It's an alternative to perl-mode and included in recent Emacs distributions. Here's how to switch from perl-mode to cperl-mode:
;; Perl (add-to-list 'auto-mode-alist '("\\.pl$" . cperl-mode)) (add-to-list 'auto-mode-alist '("\\.pm$" . cperl-mode)) (add-to-list 'interpreter-mode-alist '("perl" . cperl-mode))
Read Kai Großjohann's tutorials: A Collection of Tutorials on Emacs. They are specially usefull if you want to start using Gnus as your news and mail reader.
Writing Skeletons:
Skeletons are an easy way to define functions which insert entire
source code skeletons, eg. you could define a skeleton which, when
called, inserts if () {}
and places point in the first
parenthesis.
Navigation: Top, Table of Contents, Index, next: Elisp Installation HOWTO, prev: Emacs