Книга: Learning GNU Emacs, 3rd Edition
6.7 Naming, Saving, and Executing Your Macros
6.7 Naming, Saving, and Executing Your Macros
In this section, we'll describe how to save macros so that you can use them in different editing sessions. To save a macro, bind it permanently to a key, and load it in subsequent Emacs sessions, follow these steps:
1. Define the macro, if you haven't already.
2. Type C-x C-k n (for name-last-kbd-macro). Now type a name for your macro and press Enter. A non-Emacs sounding name is best so that Emacs doesn't confuse it with one of its own commands. Once you've executed this command, Emacs remembers the macro for the rest of the editing session. To use it again, type the command M-x name (where name is the name you've chosen). Emacs treats your named macro like one of its own commands; it shows up in completion lists if you press Tab after typing a few letters of the name.
3. If you want to save the macro definition permanently, you must insert the macro definition into a file. This could be your .emacs file or a macro file that you load through your .emacs file. Type C-x C-f filenameEnter to find the file into which to insert the definition and move to the end of it by typing M->.
4. Type M-x insert-kbd-macro Enter macronameEnter. Emacs inserts Lisp code that represents your macro.
5. Add a line to .emacs make the key binding permanent. For example, if we called our macro transpose-names and bound it to C-x C-k T, we would add this line to our .emacs file (or other macro definition file):
(global-set-key "C-xC-kT" 'transpose-names)
6. If you save the macro in some other file, it won't be loaded automatically. For example, let's say that you have defined a macro called transpose-names and placed it in the file html.macs, in the directory ~/macros. Add this line to your .emacs file to load your macros automatically:
(load-file "~/macros/html.macs")
7. Save the .emacs file and, if different, the file in which you inserted your macro. Exit and restart Emacs. You can now execute this macro either by typing M-x transpose-names Enter or by pressing C-x C-k T.
- 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
- Разработка приложений баз данных InterBase на Borland Delphi
- Open Source Insight and Discussion
- Introduction to Microprocessors and Microcontrollers
- Chapter 6. Traversing of tables and chains
- Chapter 8. Saving and restoring large rule-sets
- Chapter 11. Iptables targets and jumps
- Chapter 12. Debugging your scripts
- Chapter 5 Installing and Configuring VirtualCenter 2.0
- Chapter 16. Commercial products based on Linux, iptables and netfilter
- Appendix A. Detailed explanations of special commands
- Appendix B. Common problems and questions
- Appendix E. Other resources and links