<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wikemacs.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=192.198.151.37</id>
	<title>WikEmacs - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wikemacs.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=192.198.151.37"/>
	<link rel="alternate" type="text/html" href="https://wikemacs.org/wiki/Special:Contributions/192.198.151.37"/>
	<updated>2026-04-21T01:22:11Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.35.14</generator>
	<entry>
		<id>https://wikemacs.org/index.php?title=Keyboard_macros&amp;diff=46810</id>
		<title>Keyboard macros</title>
		<link rel="alternate" type="text/html" href="https://wikemacs.org/index.php?title=Keyboard_macros&amp;diff=46810"/>
		<updated>2014-09-29T12:39:28Z</updated>

		<summary type="html">&lt;p&gt;192.198.151.37: Undo revision 46809 by 120.43.4.33 (talk)&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Manual|emacs|Basic-Keyboard-Macro|Basic Keyboard Macro}}&lt;br /&gt;
&lt;br /&gt;
'''Keyboard macros''' can be used to automate or repeat tedious editing tasks in Emacs.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Basic Use==&lt;br /&gt;
&lt;br /&gt;
; {{Keys|F3}}, or {{Keys|C-x (}}&lt;br /&gt;
: Start defining a macro.&lt;br /&gt;
; {{Keys|F4}}, or {{Keys|C-x )}}&lt;br /&gt;
: Stop defining a macro.&lt;br /&gt;
; {{Keys|F4}}, or {{Keys|C-x e}}&lt;br /&gt;
: Execute a macro&lt;br /&gt;
; {{Keys|C-u 37 C-x e}} or {{Keys|C-u 37 F4}}&lt;br /&gt;
: Execute a macro multiple times, using [[prefix argument]]&lt;br /&gt;
; {{Keys|C-u 0 C-x e}}&lt;br /&gt;
: Execute a macro until the end of the buffer&lt;br /&gt;
&lt;br /&gt;
==Example usage==&lt;br /&gt;
&lt;br /&gt;
Consider the standard &amp;lt;code&amp;gt;*scratch*&amp;lt;/code&amp;gt; buffer:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
;; This buffer is for notes you don't want to save, and for Lisp evaluation.&lt;br /&gt;
;; If you want to create a file, visit that file with C-x C-f,&lt;br /&gt;
;; then enter the text in that file's own buffer.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Suppose you want to remove the first occurrence of the letter &amp;quot;a&amp;quot; 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.&lt;br /&gt;
&lt;br /&gt;
# Make sure [[point]] is at the start of the buffer.&lt;br /&gt;
# Hit &amp;lt;code&amp;gt;C-x (&amp;lt;/code&amp;gt; to start recording your macro. '''Note:''' If you hit &amp;lt;code&amp;gt;C-g&amp;lt;/code&amp;gt; or if an error occurs, your keyboard macro recording will stop.&lt;br /&gt;
# Hit &amp;lt;code&amp;gt;C-s&amp;lt;/code&amp;gt; followed by &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; to find the first &amp;quot;a&amp;quot;. Now, point is right after the first &amp;quot;a&amp;quot; in the text.&lt;br /&gt;
# Hit backspace to delete that &amp;quot;a&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The first occurrence of &amp;quot;a&amp;quot; of the first line has been deleted. Let's move point to the beginning of the next line and then stop recording.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol start=&amp;quot;5&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Hit &amp;lt;code&amp;gt;C-e C-f&amp;lt;/code&amp;gt; to move point to the beginning of the next line.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Hit &amp;lt;code&amp;gt;C-x )&amp;lt;/code&amp;gt; to finish the recording of our macro.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The macro you have just recorded performs the operation of removing the first occurrence of &amp;quot;a&amp;quot; it can find and then moving point to the next line.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol start=&amp;quot;7&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Hit &amp;lt;code&amp;gt;C-x e&amp;lt;/code&amp;gt; once to call that macro.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Continue hitting &amp;lt;code&amp;gt;e&amp;lt;/code&amp;gt; to call it several times. Hit any other key to get out of the macro repetition.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Naming and saving macros ==&lt;br /&gt;
&lt;br /&gt;
You can:&lt;br /&gt;
* give a name to the most recently defined macro (&amp;lt;code&amp;gt;C-x C-k n&amp;lt;/code&amp;gt;)&lt;br /&gt;
* bind it to a key sequence (&amp;lt;code&amp;gt;C-x C-k b&amp;lt;/code&amp;gt;)&lt;br /&gt;
* insert it in the current buffer as lisp code (&amp;lt;code&amp;gt;M-x insert-kbd-macro&amp;lt;/code&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
{{Manual|emacs|Save-Keyboard-Macro|Save Keyboard Macro}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To bind a keyboard macro to a key use &amp;lt;code&amp;gt;C-x C-k b&amp;lt;/code&amp;gt;.  To avoid problems caused by overriding existing bindings, the key sequences &amp;lt;code&amp;gt;C-x C-k 0&amp;lt;/code&amp;gt; through &amp;lt;code&amp;gt;C-x C-k 9&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;C-x C-k A&amp;lt;/code&amp;gt; through &amp;lt;code&amp;gt;C-x C-k Z&amp;lt;/code&amp;gt; are reserved for your own keyboard macro bindings.  You can, however, bind a keyboard macro to whatever you like.&lt;br /&gt;
&lt;br /&gt;
==Variables==&lt;br /&gt;
&lt;br /&gt;
Variables can be stored in lisp or in [[registers]].  Here's an example using lisp:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[M-: (setq x 1)]&lt;br /&gt;
&amp;lt;F3&amp;gt;&lt;br /&gt;
Line number [C-u M-: x]&lt;br /&gt;
[M-: (setq x (+ x 1))]&lt;br /&gt;
&amp;lt;F4&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now execute the macro four times with the command &amp;lt;code&amp;gt;C-x e e e e&amp;lt;/code&amp;gt; and you get:&lt;br /&gt;
&lt;br /&gt;
line number 1&amp;lt;br /&amp;gt;&lt;br /&gt;
line number 2&amp;lt;br /&amp;gt;&lt;br /&gt;
line number 3&amp;lt;br /&amp;gt;&lt;br /&gt;
line number 4&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= See also =&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/Silex/elmacro elmacro], to show keyboard macros as emacs-lisp&lt;br /&gt;
* [https://github.com/abo-abo/centimacro centimacro] to (temporarily) bind any number of macros to any global shortcuts.&lt;br /&gt;
* You can use keyboard macros à la vim in [[evil]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Intermediate]]&lt;br /&gt;
[[Category:Editing]]&lt;/div&gt;</summary>
		<author><name>192.198.151.37</name></author>
	</entry>
</feed>