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

Here is a simple extension to Emacs that moves the line that 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 that point is on to top of window."
  (interactive)
  (recenter 0))

Now for the key binding.

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)

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

If you run two versions of GNU Emacs, such as versions 27 and 28, and use one .emacs file, you can select which code to evaluate with the following conditional:

(cond
 ((= 27 emacs-major-version)
  ;; evaluate version 27 code
  ( … ))
 ((= 28 emacs-major-version)
  ;; evaluate version 28 code
  ( … )))

For example, recent versions blink their cursors by default. I hate such blinking, as well as other features, so I placed the following in my .emacs file21:

(when (>= emacs-major-version 21)
  (blink-cursor-mode 0)
  ;; Insert newline when you press 'C-n' (next-line)
  ;; at the end of the buffer
  (setq next-line-add-newlines t)
  ;; Turn on image viewing
  (auto-image-file-mode t)
  ;; Turn on menu bar (this bar has text)
  ;; (Use numeric argument to turn on)
  (menu-bar-mode 1)
  ;; Turn off tool bar (this bar has icons)
  ;; (Use numeric argument to turn on)
  (tool-bar-mode nil)
  ;; Turn off tooltip mode for tool bar
  ;; (This mode causes icon explanations to pop up)
  ;; (Use numeric argument to turn on)
  (tooltip-mode nil)
  ;; If tooltips turned on, make tips appear promptly
  (setq tooltip-delay 0.1)  ; default is 0.7 second
   )

Footnotes

(21)

When I start instances of Emacs that do not load my .emacs file or any site file, I also turn off blinking:

emacs -q --no-site-file -eval '(blink-cursor-mode nil)'

Or nowadays, using an even more sophisticated set of options,

emacs -Q -D