automatic pairing of brackets and quotes





Some text-editors, notably TextMate for MacOS, have a nice feature where
inserting a opening ( will automatically insert the closing ), and put the
cursor in between them (and does same for [], {}, and various quote-marks).



Not surprisingly, there are some implementations for emacs as well; the best
one I have found so far is called autopair, which was written by João
Távora. It usually does things just right. Do things 'just right' is
essential for such a tool; even small annoyances can disturb your
flow. Autopair tries to do whatever makes the most sense for a given mode
(programming language etc.), but it can be tuned as well.



After installation, you can automatically activate it for all modes with (in
your .emacs):





(require 'autopair)
(autopair-global-mode 1)





Now, evaluate this or restart emacs, and enjoy the autopairing-magic!



Except for autopairing, autopair also takes care of autocleaning; that
is, if I press ( it turns that into () (the pairing part), and if I press
Backspace then, it removes the whole () (the cleaning part). This makes
things much less annoying if you type a pair by accident. Autopairing is the
kind of thing that can get annoying quickly if it does not things exactly
right – and autopair succeeds!



Another nice trick it offers is autowrapping – that is, I select a word,
press ", and automatically it's turned into "word". To enable that, you need
to add the following:





(setq autopair-autowrap t)





Note: you might want to see the notes below about delete-selection-mode
and cua-mode.



Anyway, autopair with autowrap makes for a really smooth editing
experience, I love it! There are two small issues for me though. First, when the cursor in front of some non-whitespace,
I'd like autopairing not to happen, and second, somehow I can't seem to
get "-autopairing to work in org-mode; of course, that could be my own
fault. These things might be tunable; I haven't tried very hard yet.






delete-selection-mode






Important to mention here is that autopair is (by default) not fully
compatible with delete-selection-mode. As you may know, that is the mode that
causes emacs to replace the current selection with a character typed, similar
to what most other programs do. I think many people have it enabled in their
.emacs with something like:





(delete-selection-mode 1)





If you want to keep on using that together with autopair, add the
following to your .emacs:





(put 'autopair-insert-opening 'delete-selection t)
(put 'autopair-skip-close-maybe 'delete-selection t)
(put 'autopair-insert-or-skip-quote 'delete-selection t)
(put 'autopair-extra-insert-opening 'delete-selection t)
(put 'autopair-extra-skip-close-maybe 'delete-selection t)
(put 'autopair-backspace 'delete-selection 'supersede)
(put 'autopair-newline 'delete-selection t)





But, not that that still won't give you the autowrap behavior mentioned
above. For that, we can use cua-mode.







cua-mode






We discussed CUA-mode before, focusing on its nice rectangle-editing
features. But CUA-mode can also be an alternative for
delete-selection-mode, and it goes together more nicely with autopair;
so, instead of delete-selection-mode and the put's, add the following
to your .emacs:





(setq cua-enable-cua-keys nil)           ;; don't add C-x,C-c,C-v
(cua-mode t) ;; for rectangles, CUA is nice





See the linked CUA-mode article for the 'why' of that first line. With this
change, autopair should be working smoothly, including autowrap.







further customization






As I have hinted at, autopair can be tuned for different modes, and can
differentiate between it's behaviour in literal strings, code, comments
etc. The default are usually sane, but if you're interested, have a look at
the documentation, in particular autopair-extra-pairs and the More tricks-section in the documentation.



No comments:

Post a Comment

Followers

Popular Posts