In an earlier answer, Aristotle Pagaltzis wrote: "Vim excels in the small ... You can easily do things in Vim in the normal course of editing that would require you to drop down to scripting in Emacs."
I switched to Emacs after over a decade of exclusively using vi, and initially I would have agreed with the claim, "You can easily do things in Vim in the normal course of editing that would require you to drop down to scripting in Emacs." But then I discovered that by using Emacs' macro capability and a large repeat count, I could easily make Emacs do pretty much everything that vi had made easy, and a great deal more.
Emacs' macro functionality involves three commands:
C-x ( start remembering keystrokes
C-x ) stop remembering keystrokes
C-x e replay the remembered keystrokes
For example, in vi if I wanted to find all <a>
tags in an HTML file
and add a target
attribute, I might do something like the following:
:g/^<a/s/>/ target="_blank">/
This example is not perfect, since it assumes that all <a>
tags are on a line by themselves. But it's good enough for illustrating how one accomplishes the equivalent task in two different editors.
To achieve the same effect easily in emacs, here's what I do:
1. C-x (
2. M-C-s <a\>
3. C-b
4. C-s >
5. C-b
6. target="_blank"
7. C-x )
8. C-u 10000 C-x e
Here's a description of what each keystroke above does:
1. start remembering keystrokes
2. regex search for <a. Note that the "\>" after the "a" is not HTML. It's emacs regex notation for end-of-word.
3. back up one character - as a side-effect this gets you out of search mode
4. search for the next ">"
5. back up over the ">"
6. enter space as an attribute-delimiter followed by the target="_blank" attribute
7. stop remembering keystrokes
8. replay the remembered keystrokes 10,000 times or until the search fails
It looks complicated, but it's actually very easy to type. And you can use this approach to do lots of things that vi can't do, without ever dropping down to Lisp code.