Brandon's Emacs Tips

It occurred to me that some people might be interested in some of the Emacs configuration code I have written over the years.

Clever Scrolling Commands

This is probably one of the first bits of Elisp code I wrote and it is certainly the one that gets used the most. I wrote this because I was unhappy with the scrolling behaviour of most editors, including Emacs. Most editors scroll the screen when you attempt to move the cursor beyond the top or bottom lines that are displayed. The problem with this is that you very often want to see the lines immediately above or below where you are typing. With the normal scrolling behaviour, this means you have to move your cursor up then down again (or down then up again) so that you can both see the lines you want to see and type at the place you want.

The functionality of clever-scroll-up and clever-scroll-down is simply that, instead of scrolling only when you are at the top or bottom line of the screen, scrolling occurs when you are in the top third or bottom third of the screen. More specifically, if you are in the top third of the screen and move your cursor up, the screen will scroll down; and, if you are in the bottom half of your screen and scroll down, the screen will scroll up. It's simple, but I think if you try it you will find that, compared to the usual scrolling behaviour, it is a much more convenient way to control screen scrolling while browsing and editing files. I would be interested to hear if others find this as beneficial as I do.

Here is the Emacs code. Simply add it to your .emacs file to enable stress free scrolling. I think it should work with pretty much any Emacs setup although the appropriate key binding commands may vary.

;; Clever scrolling commands 
;; by Brandon Bennett, University of Leeds
(defun clever-scroll-down ()
       (interactive)
       (previous-line 1)
       (setq winstart (window-start))
       (if (and  ( > winstart 1)
                 ( > ( / (screen-height)
                        (+ 1 (count-lines winstart (point))))
                  3 )
           )
           (scroll-down 1) ) )

(defun clever-scroll-up ()
       (interactive)
       (next-line 1)
       (if (and (> (buffer-size) (window-end))
                ( > (* 3 (+ 1 (count-lines (window-start) (point))))
                    (* 2 (screen-height))
                )    
           )
           (scroll-up 1) ) )

;; Now bind to your up and down arrow buttons
(global-set-key [up] 'clever-scroll-down)
(global-set-key [down] 'clever-scroll-up)

More Tips?

Sorry, there is just the one tip on this page so far. I will probably put some more up if I get time, especially if I get positive comments about the clever scrolling. My .emacs file is certainly very large (a boast that only a true geek could make), so probably there is some other stuff in it that people might find useful.