When Emacs starts up, it doesn't know anything about arrow keys at all (except when running under X, see section How do I use function keys under X Windows?). 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:
In this case, there are several techniques suggested by Colin Jensen <email@example.com>, Ben Liblit <Liblit@cs.psu.edu>, and Marc Auslander <firstname.lastname@example.org>:
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.
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.
(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.)