Go to the first, previous, next, last section, table of contents.

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 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:

  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:

The next two cases are problems even if there is a initialization file for your terminal type.

  1. 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.
  2. 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.
  3. 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.)

Go to the first, previous, next, last section, table of contents.