Emacs Key Macro Example: Modify Webfeed Entries
Here's a example of Emacs: Keyboard Macro use.
I have a webfeed file blog.xml
in
Atom Webfeed
format.
There are many text blocks like this:
<entry> <title>…</title> <id>…</id> <updated>…</updated> <summary>…</summary> <content type="xhtml"> <div xmlns="http://www.w3.org/1999/xhtml"> <p>see <a href="http://xahlee.blogspot.com/2011/11/emacs-lisp-exercise-insert-random-uuid.html">http://xahlee.blogspot.com/2011/11/emacs-lisp-exercise-insert-random-uuid.html</a></p> </div> </content> <link rel="alternate" href="http://xahlee.org/emacs/blog.html"/> </entry>
I need to remove the “content” part and simply put the URL in the alternate link, like this:
<entry> <title>…</title> <id>…</id> <updated>…</updated> <summary>…</summary> <link rel="alternate" href="http://xahlee.blogspot.com/2011/11/emacs-lisp-exercise-insert-random-uuid.html"/> </entry>
Kmacro is excellent solution for this. You just call interactive search to move cursor to places you want, and do delete or copy and paste. Like this.
isearch-forward
【Ctrl + s】 forhttp://xahlee.blogspot
.isearch-backward
【Ctrl + r】 for"
, move cursor forward 1 position, set mark.isearch-forward
for"
, do a copy. Now the blog URL is in clipboardisearch-backward
for<content
, set mark.isearch-forward
for</content>
.- Press delete to delete the content section.
isearch-forward
forhref=
.- Do steps to delete the URL (using isearch and set mark like above), then paste the new one.
Once i recorded these steps, then i call kmacro-end-and-call-macro
【Ctrl + x e】. This way, every 2 keystrokes makes one entry change. This allows me to visually verify what i've done is correct. I could also press Ctrl + u, then a number, then Ctrl + x e to repeat it automatically n times.