Most of Emacs commands use key sequences. See Keystrokes, for more information about Keys and Commands. In Emacs, the keys themselves carry no meaning unless they are bound to a function. For example, C-n moves the cursor to the next line because its bound to the function next-line. Similarly, C-p moves to the previous line because its bound to the function previous-line. The functions themselves define a particular behavior. You can customize the key C-n to move to the previous line by binding it to previous-line and C-p to move to the next line by binding it to next-line. To bind keys to globally run commands you need to use the following syntax in your init.el file:
(global-set-keykeys cmd)
Here, global-set-key is a function which will bind the
keys to the specified cmd. For example, if you type the
following in your init.el file:
(global-set-key "\C-p" 'next-line)
(global-set-key "\C-n" 'previous-line)
then C-p will move to the next line and C-n to the previous line.
You can also disable a key binding, by using ‘nil’ as the cmd in the syntax stated above. Here, ‘nil’ stands for ‘false’ which means disable a command or turn off a feature. If you want to enable a command or turn on a particular feature use ‘t’ which stands for ‘true’. For example, if you do not wish C-x C-c to ‘Exit Emacs’ you can type the following expression in your init.el file:
(global-set-key "\C-x\C-c" nil)
You might want to have this statement in your init.el file because its easy to hit this command by mistake and it could be annoying to exit Emacs unintentionally. There is an Exit Emacs option in the File menu which you might want to use instead. To make a particular key undefined you can also use:
(global-unset-key "\C-x\C-c")
Now if you use the command C-x C-c, you will get an error saying that the command is undefined.
Some other customizations you could try are:
(global-set-key 'button3 'beginning-of-buffer)
Now when you press the third button of your mouse, the cursor will be
placed at the beginning-of-buffer.
(global-set-key 'f1 'goto-line)
If you press the <F1> key, you will be prompted for a line number. After you type the line number and hit <RET>, the cursor will be placed on that line number.
(global-set-key 'f2 'undo)
Pressing <F2> will undo the last command. If you have a <undo> key on your keyboard, try binding that key to the undo command.
Another syntax for customizing key bindings is:
(define-key keymap keys def)
It defines keys to run def in the keymap keymap.
keymap is a keymap object which records the bindings of keys to the commands that they run.
keys is the sequence of keystrokes to bind.
def is anything that can be a key's definition:
Look at the following two examples:
(define-key global-map "\C-xl" 'make-symbolic-link)
(define-key c-mode-map "\C-xl" 'make-symbolic-link)
Both the examples bind the key C-xl to run the function
make-symbolic-link (see Misc File Ops). However, the second example will bind the key only for C
mode. See Major Modes, for more
information on Major Modes in SXEmacs.