Saturday, June 18, 2011

Scroll up and down line by line in Emacs

It is sometimes useful to align a particular line in your buffer in your Emacs window. Rather than moving the cursor and recentering the buffer around your cursor, this is more easily done if you can simply scroll the text. Add the following code to your .emacs file and use Ctrl-, and Ctrl-. to scroll up or down one line at a time.

(defun scroll-up-one-line()
  (interactive)
  (scroll-up 1))

(defun scroll-down-one-line()
  (interactive)
  (scroll-down 1))

(global-set-key [?\C-.] 'scroll-down-one-line)
(global-set-key [?\C-,] 'scroll-up-one-line)

2 comments:

Jürgen said...

BTW, scroll-down/scroll-up is allready bound to C-v/M-v

To scroll down/up by n-lines just invoke it with a prefix-argument n like

C-u 1 C-v

Steve Ford said...

Thanks!

The problem with the C-u 1 C-v solution is that I don't know how to bind it directly to a single keystroke. Your two little functions work nicely.

I got used to ^y / ^e on vi, and have been missing it since returning to emacs.