Creating An Emacs Command

1 - I can do better 2 - Jury's out 3 - Pretty darn good 4 - Splendiferous 5 - Awesometastic by 0 people | Log in to rate

Ranked #5,042 in Tech & Geek, #118,754 overall

Emacs Command Creation - A Tutorial

Whenever you press a key in emacs, a command is run. E.g. if you press 'a' it calls a command called self-insert-command. Much of the power of emacs derives from the fact that anyone can define their own command in emacs lisp. This command will look the same as any built-in command and can be bound to any key or key combination.

For example, if you don't like the letter 'e' you could bind it to a command that would display a message "Please don't use the letter 'e'". How can you do this?

If you want a more useful example of extending emacs, have a look at my emacs hooks lens.

The basics of emacs commands 

The (interactive) function

(defun no-e-please ()
  (message "Please don't use the letter e"))

(global-set-key (kbd "e") 'no-e-please)



However, when you try this, typing the letter 'e' results in the following error.

Wrong type argument: commandp, no-e-please



What happened here? It turns out that emacs differentiates between commands, that can be bound to a key, and normal functions which cannot. A command is simply a function that is marked interactive.

(defun no-e-please ()
  (interactive)
  (message "Please don't use the letter e"))

Self Insert Command 

Inserting the character you pressed

However, we probably still want to insert the letter 'e', even though we've given a warning (otherwise, you will find it tricky, although not impossible, to enter a letter 'e' when you really want to).

(defun no-e-please ()
  (interactive)
  (self-insert-command 1)
  (message "Please don't use the letter e"))



That is better, but it still has a couple of problems. The first thing is if we want to assign this to a key other than 'e' then it will insert the wrong character. Not wanting to ruin my configuration, I tested it with C-c C-q and it emitted ^Q.

As the parameter in self-insert-command specifies how many times the character should be inserted how do we choose the character?

Prefix commands (C-u) 

The other problem is that if you enter C-u e then it only enters a single e whereas most emacs users would expect eeee. If you use (interactive "p") then the prefix argument is passed in to the command.

(defun no-e-please (&optional n)
  (interactive "p")
  (let ((last-command-char ?e))
    (self-insert-command (or n 1)))
  (message "Please don't use the letter e"))

In Emacs you have complete control over everything

All bound keys execute emacs commands. You can define commands that behave in the same way as built-in commands, many of which are implemented in emacs-lisp.

My Other Emacs Lenses 

...take a look at my other emacs lenses

Curious Programmer Emacs Blog 

Tips and Tutorials

Loading Fetching RSS feed... please stand by

Leave Me Feedback 

If there is anything else you want to know about defining your own emacs command let me know here.

submit

Latest Emacs News In The Blogosphere 

Aquamacs: Emacs for Mac OS X

Explore related pages

Create a Lens!