Next: , Previous: Skeleton Language, Up: Top

5 Inserting Matching Pairs of Characters

Various characters usually appear in pairs. When, for example, you insert an open parenthesis, no matter whether you are programming or writing prose, you will surely enter a closing one later. By entering both at the same time and leaving the cursor in between, Emacs can guarantee you that such parentheses are always balanced. And if you have a non-qwerty keyboard, where typing some of the stranger programming language symbols makes you bend your fingers backwards, this can be quite relieving too.

This is done by binding the first key (see Rebinding) of the pair to skeleton-pair-insert-maybe instead of self-insert-command. The “maybe” comes from the fact that this at-first surprising behavior is initially turned off. To enable it, you must set skeleton-pair to some non-nil value. And even then, a positive argument (see Arguments) will make this key behave like a self-inserting key (see Inserting Text).

While this breaks with the stated intention of always balancing pairs, it turns out that one often doesn't want pairing to occur, when the following character is part of a word. If you want pairing to occur even then, set skeleton-pair-on-word to some non-nil value.

Pairing is possible for all visible characters. By default the parenthesis ‘(’, the square bracket ‘[’, the brace ‘{’ and the pointed bracket ‘<’ all pair with the symmetrical character, and the grave accent ‘`’ pairs with the apostrophe ‘'’. All other characters pair themselves. This behavior can be modified by the variable skeleton-pair-alist. This is in fact an alist of skeletons (see Skeleton Language), with the first part of each sublist matching the typed character. This is the position of the interactor, but since pairs don't need the str element, this is ignored.

Some modes have bound the command skeleton-pair-insert-maybe to relevant keys. These modes also configure the pairs as appropriate. For example, when typing TeX input, you'd expect the grave accent (‘`’) to pair with the apostrophe (‘'’), while in Shell script mode it must pair to itself. They can also inhibit pairing in certain contexts. For example an escaped character stands for itself.