Create a Cocoa keyboard map for single-line scrolling

Aug 29, '05 08:48:00AM

Contributed by: dkulp

As an emacs user, I've long had custom keybindings to scroll one line up or down. In my case, they're bound to ctrl-period and ctrl-semicolon for scrolling down and up, respectively.


(defun scroll-up-one-line ()
      (interactive)
      (scroll-up 1))
(defun scroll-down-one-line ()
      (interactive)
      (scroll-down 1))
(global-set-key [(control ?.)]  'scroll-up-one-line) ; C-.
(global-set-key [(control ?;)] 'scroll-down-one-line) ; C-;
In emacs, the cursor always stays in view, but in Mac apps, the cursor stays put so that scrolling and then typing will bounce you right back to where you were. I'd like to scroll and then type without ever taking my hands off the keyboard. To provide the same functionality as emacs, I chose to edit the Cocoa key bindings so that the cursor moves a line for each line scrolled. In that way, the cursor stays usually pretty close to the same relative window position. Moreover, the cursor stays in view, so it's easy to position the cursor using the keyboard once you've scrolled to the desired location.

Here's what to do:

Add emacs key bindings to all of your Cocoa apps by saving this file into ~/Library -> KeyBindings -> DefaultKeyBinding.dict. Adding keybindings to Cocoa is a great hint by itself, in case you missed it. Cocoa comes with some basic emacs keybindings, but the above file adds more. Then edit the file, and add the following lines within the dict:

     <key>^.</key>
        <array>
              <string>scrollLineDown:</string>
          <string>moveDown:</string>
        </array>
        <key>^;</key>
        <array>
              <string>scrollLineUp:</string>
            <string>moveUp:</string>
        </array>
[robg adds: This works as described. Note that you'll have to create the KeyBindings directory if you haven't done so previously.]

Comments (4)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20050823100726625