137: Why don't the arrow keys work? When Emacs starts up, it doesn't know anything about arrow keys at all (except when running under X, see question 130). During the process of starting up, Emacs will load a terminal-specific initialization file for your terminal type (as determined by the environment variable TERM), if one exists. This file has the responsibility for enabling the arrow keys. There are several things that can go wrong: 1. There is no initialization file for your terminal. You can determine this by looking in the lisp/term directory. If your terminal type (as determined by the TERM environment variable) is xxx-yy-z, then the first of these files in the lisp/term directory will be loaded as the terminal-specific initialization file: xxx-yy-z.el, xxx-yy.el, or xxx.el. There are two major cases of this problem: ! ! * Your terminal type is very similar to one that has an init file. ! ! In this case, there are several techniques suggested by Colin Jensen ! , Ben Liblit , and Marc ! Auslander : ! ! A. Add a symbolic link in lisp/term for your terminal type that ! points to the similar type. For example, you could make VT102 ! terminals work with this command: ! ! ln -s vt100.el vt102.el ! ! This fixes things for everyone on the system who uses the terminal ! type. ! ! B. If you can't do the solution in part A, you can add code to your ! term-setup-hook that loads the correct file like this: ! ! (setq term-setup-hook ! (function ! (lambda () ! (cond ((equal "vt102" (or (getenv "TERM") "")) ! (load (concat term-file-prefix "vt100"))) ! (;; Code for other terminal types goes here ... ! ))))) ! ! C. If you use `tset' to set your TERM environment variable when you ! login, you can use the `-m' switch to tell tset to use a terminal ! type known by Emacs instead of another similar one. For example, ! specifying this: ! ! tset ... -m 'dec-vt220:vt220' ... ! ! will make tset say you are on a `vt220' instead of a `dec-vt220'. ! ! D. Interactively, you can type "M-x load-library RET term/vt100" to ! load the terminal-specific initialization files for VT100 ! terminals. ! ! * Your terminal type is not similar to one that has an init file. ! One can be made for your terminal, or you can just add code to your own .emacs to handle this problem for yourself. For example, if your terminal's arrow keys send these character sequences: Up: ESC [ A Down: ESC [ B Right: ESC [ C Left: ESC [ D then you can bind these keys to the appropriate commands with code in your .emacs like this: (setq term-setup-hook (function (lambda () (cond ((string-match "\\`xyzzy" (or (getenv "TERM") "")) ;; First, must unmap the binding for left bracket ! (or (keymapp (lookup-key global-map "\e\[")) + (define-key global-map "\e\[" nil)) + ;; Enable terminal type xyzzy's arrow keys: (define-key global-map "\e\[A" 'previous-line) + (define-key global-map "\e\[B" 'next-line) + (define-key global-map "\e\[C" 'forward-char) + (define-key global-map "\e\[D" 'backward-char)) + ((string-match "\\`abcde" (or (getenv "TERM") "")) ;; Do something different for terminal type abcde ;; ..... ))))) - NOTE: You may have to restart Emacs to get changes to take effect. ! NOTE: Your arrow keys may send sequences beginning with "ESC O" when Emacs is running, even if they send sequences beginning with "ESC [" at all other times. This is because Emacs uses any command there may be in your terminal's termcap entry for putting the terminal into "Application Keypad Mode". Just map these sequences the same way as above. The next two cases are problems even if there is a initialization file for your terminal type. 2. The initialization file for your terminal doesn't bind arrow keys. + + If your terminal type is `xterm', you will have to bind the arrow keys + as in part 1 above, since the xterm.el file doesn't do anything useful. + There may be other terminal types with the same problem. + 3. Your terminal's arrow keys send individual control characters. For example, the arrow keys on an ADM-3 send C-h, C-j, C-k, and C-l. There is not much Emacs can do in this situation, since all the control characters except for C-^ and C-\ are already used as Emacs commands. It may be possible to convince the terminal to send something else when you press the arrow keys; it is worth investigating. You have to make the hard choices of how to rebind keys to commands to make things work the way you want. Another alternative is to start learning the standard Emacs keybindings for moving point around: C-b, C-f, C-p, and C-n. Personally, I no longer use the arrow keys when editing because I have switched keyboards so many times. 4. Your terminal's arrow keys send sequences beginning with "ESC [". Due to an extremely poor design decision (ie., these sequences are ANSI standard), none of the the terminal-specific initialization files that are distributed with Emacs will bind these character sequences to the appropriate commands by default. (This also applies to any other function keys which generate character sequences starting with "ESC [".) This is because it was deemed far more important to preserve the binding of M-[ to the backward-paragraph command. It appears that this will change in Emacs 19. Some of the terminal-specific initialization files that come with Emacs provide a command enable-arrow-keys that will fix this problem. To get this automatically invoked, put this in your .emacs: (setq term-setup-hook (function (lambda () (if (fboundp 'enable-arrow-keys) (enable-arrow-keys))))) We put this in our lisp/default.el file, so users don't have to worry about it: ;; don't override a user's term-setup-hook (or term-setup-hook (setq term-setup-hook (function (lambda () (and (fboundp 'enable-arrow-keys) ;; don't override a user key mapping (eq 'backward-paragraph (lookup-key esc-map "[")) (enable-arrow-keys)))))) If your terminal type is `sun', you should put this in your .emacs instead (or in addition to the above): (setq sun-esc-bracket t) - It is possible that the terminal-specific initialization file for your terminal type was written locally and does not follow the rule mentioned above. In this case you may need to inspect it to find out how to enable the arrow keys. (Actually, if it was written locally, it probably enables the arrow keys by default.)