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


A Simple Extension: line-to-top-of-window

Here is a simple extension to Emacs that moves the line point is on to the top of the window. I use this all the time, to make text easier to read.

You can put the following code into a separate file and then load it from your `.emacs' file, or you can include it within your `.emacs' file.

Here is the definition:

;;; Line to top of window;  
;;; replace three keystroke sequence  C-u 0 C-l
(defun line-to-top-of-window ()
  "Move the line point is on to top of window."
  (interactive) 
  (recenter 0))

Now for the keybinding.

Although most of an Emacs version 18 `.emacs' file works with version 19, there are some differences (also, of course, there are new features in Emacs 19).

In version 19 Emacs, you can write a function key like this: `[f6]'. In version 18, you must specify the key strokes sent by the keyboard when you press that function key. For example, a Zenith 29 keyboard sends ESC P when I press its sixth function key; an Ann Arbor Ambassador keyboard sends ESC O F. Write these keystrokes as `\eP' and `\eOF', respectively.

In my version 18 `.emacs' file, I bind line-to-top-of-window to a key that depends on the type of terminal:

(defun z29-key-bindings () 
  "Function keybindings for Z29 terminal."
  ;; ...
  (global-set-key "\eP" 'line-to-top-of-window))

(defun aaa-key-bindings () 
  "Function keybindings for Ann Arbor Ambassador"
  ;; ...
  (global-set-key "\eOF" 'line-to-top-of-window))

(You can find out what a function key sends by typing the function key, and then typing C-h l (view-lossage) which displays the last 100 input keystrokes.)

After specifying the key bindings, I evaluate an expression that chooses among keybindings, depending on the type of terminal I am using. However, before doing that, I turn off the predefined, default terminal-specific keybindings, which overwrite bindings in the `.emacs' if they clash.

;;; Turn Off Predefined Terminal Keybindings

; The following turns off the predefined 
; terminal-specific keybindings such as the
; vt100 keybindings in lisp/term/vt100.el.  
; If there are no predefined terminal
; keybindings, or if you like them,
; comment this out.

(setq term-file-prefix nil)

Here is the selection expression itself:

(let ((term (getenv "TERM")))
  (cond 
   ((equal term "z29") (z29-key-bindings))
   ((equal term "aaa") (aaa-key-bindings))
   (t (message
       "No binding for terminal type %s."
       term))))

In Emacs version 19, function keys (as well as mouse button events and non-ASCII characters) are written within square brackets, without quotation marks. I bind line-to-top-of-window to my F6 function key like this:

(global-set-key [f6] 'line-to-top-of-window)

Much simpler!

For more information, see section `Rebinding Keys in Your Init File' in The GNU Emacs Manual.

If you run both Emacs 18 and Emacs 19, you can select which code to evaluate with the following conditional:

(if (string=
     (int-to-string 18)
     (substring (emacs-version) 10 12))
    ;; evaluate version 18 code
    (progn
       ... )
  ;; else evaluate version 19 code
  ...


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