Sams Teach Yourself Emacs in 24 Hours

ContentsIndex

Hour 23: Binding Keys and Creating Menus

Previous HourNext Hour

Sections in this Hour:

 

Making Personal Keybindings

The cursor keys have only the default command bindings next-line (down), previous-line (up), forward-char (right), and backward-char (left) and they are waiting for you to make them more productive. You have many modifiers that can be attached to these keys.

First, Emacs doesn't have a slide bar that would scroll a window horizontally and the default bindings C-x < and C-x > are pretty useless for buffers that have many long lines. You can't bear to type C-x < and C-x > after a few minutes when you are trying to see to the end of the line that is out of the visible screen area. Try these:


(global-set-key [(control meta left)] (sams-definteractive (scroll-right 10))) 
(global-set-key [(alt control left)](sams-definteractive (scroll-right 10))) 

Adjusting the scrolling direction with cursor keys is easy while you hold down the Alt and Ctrl keys. If you have looked closely at the C-h b keybinding listing, you'll have found out that the scrolling keys have already been bound to C-prior (Ctrl Page up) and C-next (Ctrl Page down). Somehow I find it more intuitive if these keys are bound to commands that take me to the beginning and to the end of buffer:


(global-set-key [(control select)]	'end-of-buffer) 
(global-set-key [(control prior)]	'beginning-of-buffer) 

Now when you page up and down, keys scroll one window at a time. Adding a Ctrl modifier makes them leap to the buffer's bounds to the beginning and to the end.

I also find that I am unintentionally typing the Home and End keys, so let's map them to something else. The following remappings are very handy in programming modes where the Home key positions the cursor to the beginning of a function and the End key to the end of a function.


(global-set-key [(home)]    'beginning-of-defun) 
(global-set-key [(end)]    'end-of-defun) 

Defining the keys into global-map works because the programming modes seldom, if ever, define the keys Home and End, so the keybinding is looked at from the upper level, from the global-map. (For example, C++ mode only partially overrides keys in the global-map. Recall the C-c prefix key in C++ mode from the previous sections.)

Sams Teach Yourself Emacs in 24 Hours

ContentsIndex

Hour 23: Binding Keys and Creating Menus

Previous HourNext Hour

Sections in this Hour: