Êíèãà: Learning GNU Emacs, 3rd Edition
Chapter 6. Writing Macros
What is a macro? In Emacs, a macro is simply a group of recorded keystrokes you can play back over and over again. Macros are a great way to save yourself repetitive work. For example, let's say you want to delete the third column of a table. Normally, you would go to the first line; move over to the third column; delete it; then go to the second line; give the same set of commands; and so on, until you finish, your fingers wear out, or you get too bored. Emacs lets you record the keystrokes you used to work on the first line of the table, and then "play these back" repeatedly until the job is done.[32]
Any command or action you do within Emacs, from typing text to editing to switching buffers, can be done within a macro. The key to using macros well is, not too surprisingly, recognizing when you're doing repetitive work: sensing that you have pressed more or less the same sequence of keys several times in a row. Once you learn to recognize repetitious work, you have a good feel for when to use macros. The next talent that you'll need is, given that you've recognized a cycle of "almost identical" keystrokes, figuring out how to make that cycle precisely identical—that is, figuring out a set of keystrokes that, if repeated, will do exactly what you want. Neither of these skills is particularly difficult; with a little practice, you'll be using macros all the time.
If this sounds like lazy man's programming, it is: macros give you a simple way to do very complicated things without learning Lisp and without learning any customization tricks. If the task you build the macro for is something you have to do frequently, you can save macros and load them when you want to use them. In this way, you can build up a set of convenient macros that become your own editing commands. Even if you don't write Lisp, you're not limited to the commands Emacs gives you; you can make your own!
What you use macros for will depend on the kind of work you do in Emacs. We've used macros to:
• Mark up text for formatting.
• Copy headings from one buffer to another to create an outline.
• Perform complex search-and-replace type operations that query-replace can't quite handle.
• Create index entries.
• Reformat files that were imported from another application.
• Edit tables.
• Compile, run, and test the output from a program with a single command.
• Manipulate and clean large datasets.
You'll be able to think of many more things to do with macros after you learn the few basic commands you need to use them.
- A Macro Revolution
- 6.1 Defining a Macro
- 6.2 Tips for Creating Good Macros
- 6.3 A More Complicated Macro Example
- 6.4 Editing a Macro
- 6.5 The Macro Ring
- 6.6 Binding Your Macro to a Key
- 6.7 Naming, Saving, and Executing Your Macros
- 6.8 Building More Complicated Macros
- 6.9 Executing Macros on a Region
- 6.10 Beyond Macros
- Chapter 2. Editing
- 5.4.1. The *__initcall Macros
- Chapter 12. Version Control
- 14.3.4. Useful Kernel gdb Macros
- Chapter 18 Developing Windows Mobile Applications
- 6.2 Tips for Creating Good Macros
- Chapter 14. The Help System
- Chapter 4. Using Buffers, Windows, and Frames
- Chapter 5. Emacs as a Work Environment
- Chapter 7. Simple Text Formatting and Specialized Editing
- Chapter 8. Markup Language Support
- Chapter 9. Computer Language Support