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

Why doesn't this [terminal or window-system setup] code work in my

 .emacs file, but it works just fine after Emacs starts up?

This is because you're trying to do something in your .emacs file that needs to be postponed until after the terminal/window-system setup code is loaded. This is a result of the order in which things are done during the startup of Emacs. For more details see section Why don't the arrow keys work?.

In order to postpone the execution of Emacs Lisp code until after the terminal/window-system setup, set the value of the variable term-setup-hook or window-setup-hook to be a function which does what you want.

See etc/OPTIONS for a complete explanation of what Emacs does every time it is started.

Here is a simple example of how to set term-setup-hook:

  (setq term-setup-hook
        (function
         (lambda ()
           (cond ((string-match "\\`vt220" (or (getenv "TERM") ""))
      	    ;; Make vt220's "Do" key behave like M-x:
      	    (define-key CSI-map "29~" 'execute-extended-command))
      	   ))))

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