Friday, June 11, 2010

First emacs customization

Bob Glickstein's book Writing GNU Emacs Extensions appears to be written to get you going right away on customizing emacs. I was able to successfully implement my first customization within a half-hour of starting the book. One of the annoyances of emacs is the number of CTRL-key combos you have to press to get stuff done. For example, to save the current buffer (roughly speaking every window in emacs is an emacs buffer) one has to press CTRL-x CTRL-s. I was able to assign an additional sequence of keys to save a buffer:

1. Check if the sequence of keys you want to use is assigned (or "bound" in emacs terminology) to a function. To do that: M-x describe-bindings [return] will bring up a buffer that contains all the current bindings. Search through this to find the key sequence you are interested in. I want to bind M-s to the function that saves the buffer. Currently, I'm not using that key for anything.

2. Edit your personal .emacs file which will be in your home directory and insert the following code in it -- for clarity on a line by itself:
(global-set-key "\M-s" 'save-buffer)

The above line of code has three tokens in it. The first token, global-set-key, is an emacs function that sets key bindings globally, i.e., in all modes. The second token, "\M-s", is an argument to the global-set-key function that says "I want to bind this key sequence" -- in this case it's the Meta-s key. Note how the argument is enclosed in quotation marks and the M is preceded by a backslash. The third token is the second argument to the global-set-key function that tells the function what it is that should be invoked with that key sequence. In this case it's the emacs function save-buffer that saves the current buffer (I was able to identify this function by searching for C-x C-s in the buffer that contained the current binding descriptions). Note how this function is preceded by a single quote....this is required because we don't want to evaluate this function now, instead we want to pass it as an argument to the global-set-key function.

3. Save the .emacs file, quit and reinvoke emacs to have the key sequence implemented.

No comments:

Post a Comment