https://wikemacs.org/api.php?action=feedcontributions&user=95.131.110.106&feedformat=atomWikEmacs - User contributions [en]2024-03-19T11:02:19ZUser contributionsMediaWiki 1.35.14https://wikemacs.org/index.php?title=Keyboard_macros&diff=5496Keyboard macros2013-10-21T10:01:43Z<p>95.131.110.106: Undo revision 5495 by 176.31.235.120 (talk)</p>
<hr />
<div>{{Manual|emacs|Basic-Keyboard-Macro|Basic Keyboard Macro}}<br />
<br />
'''Keyboard macros''' can be used to automate or repeat tedious editing tasks in Emacs.<br />
<br />
<br />
==Basic Use==<br />
<br />
; {{Keys|F3}}, or {{Keys|C-x (}}<br />
: Start defining a macro.<br />
; {{Keys|F4}}, or {{Keys|C-x )}}<br />
: Stop defining a macro.<br />
; {{Keys|F4}}, or {{Keys|C-x e}}<br />
: Execute a macro<br />
; {{Keys|C-u 37 C-x e}} or {{Keys|C-u 37 F4}}<br />
: Execute a macro multiple times, using [[prefix argument]]<br />
; {{Keys|C-u 0 C-x e}}<br />
: Execute a macro until the end of the buffer<br />
<br />
==Example usage==<br />
Consider the standard <code>*scratch*</code> buffer:<br />
<br />
<pre><br />
;; This buffer is for notes you don't want to save, and for Lisp evaluation.<br />
;; If you want to create a file, visit that file with C-x C-f,<br />
;; then enter the text in that file's own buffer.<br />
</pre><br />
<br />
Suppose you want to remove the first occurrence of the letter "a" on every row in that piece of text. You could write a [[regular expression]] to do the job, but let's assume you want to use a keyboard macro this time.<br />
<br />
# Make sure [[point]] is at the start of the buffer.<br />
# Hit <code>C-x (</code> to start recording your macro. '''Note:''' If you hit <code>C-g</code> or if an error occurs, your keyboard macro recording will stop.<br />
# Hit <code>C-s</code> followed by <code>a</code> to find the first "a". Now, point is right after the first "a" in the text.<br />
# Hit backspace to delete that "a".<br />
<br />
The first occurrence of "a" of the first line has been deleted. Let's move point to the beginning of the next line and then stop recording.<br />
<br />
<ol start="5"><br />
<li>Hit <code>C-e C-f</code> to move point to the beginning of the next line.</li><br />
<li>Hit <code>C-x )</code> to finish the recording of our macro.</li><br />
</ol><br />
<br />
The macro you have just recorded performs the operation of removing the first occurrence of "a" it can find and then moving point to the next line.<br />
<br />
<ol start="7"><br />
<li>Hit <code>C-x e</code> once to call that macro.</li><br />
<li>Continue hitting <code>e</code> to call it several times. Hit any other key to get out of the macro repetition.</li><br />
</ol><br />
<br />
==Saving macros==<br />
{{Manual|emacs|Save-Keyboard-Macro|Save Keyboard Macro}}<br />
<br />
===Binding to a key===<br />
To bind a keyboard macro to a key use <code>C-x C-k b</code>. To avoid problems caused by overriding existing bindings, the key sequences <code>C-x C-k 0</code> through <code>C-x C-k 9</code> and <code>C-x C-k A</code> through <code>C-x C-k Z</code> are reserved for your own keyboard macro bindings. You can, however, bind a keyboard macro to whatever you like.<br />
<br />
==Variables==<br />
<br />
Variables can be stored in lisp or in [[registers]]. Here's an example using lisp:<br />
<br />
<pre><br />
[M-: (setq x 1)]<br />
<F3><br />
Line number [C-u M-: x]<br />
[M-: (setq x (+ x 1))]<br />
<F4><br />
</pre><br />
<br />
Now execute the macro four times with the command <code>C-x e e e e</code> and you get:<br />
<br />
line number 1<br /><br />
line number 2<br /><br />
line number 3<br /><br />
line number 4</div>95.131.110.106