Difference between revisions of "Keyboard macros"

From WikEmacs
Jump to navigation Jump to search
(gutiourgeciseUQ)
m (refresh doc, add melpa packages)
(375 intermediate revisions by 74 users not shown)
Line 1: Line 1:
While we do our best to moderate comments, we do not screen comments before they are posted. If you see a comment that violates our guidelines, please use the "Report Abuse" link to notify us of the issue.So <a href=httpwww.officialnfltexansprostore.com23_ARIAN_FOSTER_JERSEYMENS_ARIAN_FOSTER_ELITE_JERSEY_NIKE_NFL_HOUSTON_TEXANS_23_LIGHTS_OUT_GREY.html>Men's Arian Foster Elite Grey Jersey</a> the ultimate thanks for nearly a week of chuckles goes to Mr. <a href=httpwww.officialnflbroncosprostore.com58_VON_MILLER_JERSEYWOMENS_VON_MILLER_LIMITED_BREAST_CANCER_AWARENESS_JERSEY_NIKE_NFL_DENVER_BRONCOS_58_DARK_GREY.html>Women's Von Miller Limited Dark Grey Jersey</a> Wigderson. Thanks for the laughter!Wigderson is a righty blogger, not one of the lefties who picked it up later, ultimately getting <a href=httpwww.shopgiantsnfljersey.com10_ELI_MANNING_JERSEYYOUTH_ELI_MANNING_ELITE_JERSEY_NIKE_NFL_NEW_YORK_GIANTS_10_ROYAL_BLUE_TEAM_COLOR_HOME.html>Youth Eli Manning Elite Royal Blue Team Color Jersey</a> into the hands of the state Democratic leadership, which decided to have fun with the whole thing.Giving credit where it's dueuse that profile to sign up for email newsletters and alerts.If Danny <a href=httpwww.officialnflgiantsprostore.comWOMENS-JASON-PIERREPAUL-JERSEY.html>Giants Jason Pierre-Paul Jersey</a> Gokey doesn't advance tonight, I'll vote for "Mine That Bird" for governor.You can personalize your public profile with a photo. You can alsoWhile I'm on the subject, Channel 6 anchor Ted Perry demonstrated that the joke had become part of Milwaukee's popular culture <a href=httpwww.officialfootballdolphins.com17_RYAN_TANNEHILL_JERSEYMENS_RYAN_TANNEHILL_LIMITED_REALTREE_JERSEY_NIKE_NFL_MIAMI_DOLPHINS_17_CAMO.html>Men's Ryan Tannehill Limited Camo Jersey</a> with this post on Twitter last night before the "Idol" viewers learned the fate of Danny Gokey:That's why this is all so funny. The first person to cite Belling's misprediction was a conservative. But it <a href=httpwww.officialfootballraiders.com20_DARREN_MCFADDEN_JERSEYMENS_DARREN_MCFADDEN_ELITE_AUTOGRAPHED_JERSEY_NIKE_NFL_OAKLAND_RAIDERS_20_BLACK_TEAM_COLOR_HOME.html>Men's Darren McFadden Elite Black Team Color Jersey</a> ignited some of Belling's humorless backers into <a href=httpwww.football49ersproshop.com99_ALDON_SMITH_JERSEYWOMENS_ALDON_SMITH_ELITE_BREAST_CANCER_AWARENESS_JERSEY_NIKE_NFL_SAN_FRANCISCO_49ERS_99_WHITE.html>Women's Aldon Smith Elite White Jersey</a> thinking this was all some evil attempt to bash Belling, who knows that bringing attention <a href=httpwww.ravensofficialnfljerseys.com52_RAY_LEWIS_JERSEYMENS_RAY_LEWIS_ELITE_JERSEY_NIKE_NFL_BALTIMORE_RAVENS_52_LIGHTS_OUT_GREY.html>Men's Ray Lewis Elite Grey Jersey</a> to his radio show is never ever a bad <a href=httpwww.patriotsonlineofficialshop.com87_ROB_GRONKOWSKI_JERSEYYOUTH_ROB_GRONKOWSKI_LIMITED_JERSEY_NIKE_NFL_NEW_ENGLAND_PATRIOTS_87_LIGHTS_OUT_GREY.html>Youth Rob Gronkowski Limited Grey Jersey</a> thing.It's often difficult to <a href=httpwww.officialfootballraiders.com20_DARREN_MCFADDEN_JERSEYYOUTH_DARREN_MCFADDEN_GAME_JERSEY_NIKE_NFL_OAKLAND_RAIDERS_20_WHITE_ROAD.html>Youth Darren McFadden Game White Jersey</a> determine who posted <a href=httpwww.ravensofficialnfljerseys.com82_TORREY_SMITH_JERSEYMENS_TORREY_SMITH_ELITE_JERSEY_NIKE_NFL_BALTIMORE_RAVENS_82_SUPER_BOWL_XLVII_LIGHTS_OUT_GREY.html>Men's Torrey Smith Elite Grey Jersey</a> something on their blog first, but it <a href=httpwww.bearsofficialonline.com15_BRANDON_MARSHALL_JERSEYMENS_BRANDON_MARSHALL_LIMITED_JERSEY_NIKE_NFL_CHICAGO_BEARS_15_TEAMROAD_TWO_TONE.html>Men's Brandon Marshall Limited Jersey</a> looks like the first blogger to pick up on Mark Belling's funny little mistaken Kentucky Derby blognostication <a href=httpwww.officialnflpackersproshop.comWOMENS-CLAY-MATTHEWS-JERSEY.html>Kids Clay Matthews Jersey</a> was a guy named James Wigderson.Please note <a href=httpwww.footballfalconsprostore.com88_TONY_GONZALEZ_JERSEYMENS_TONY_GONZALEZ_GAME_JERSEY_NIKE_NFL_ATLANTA_FALCONS_88_RED_TEAM_COLOR_HOME.html>Men's Tony Gonzalez Game Red Team Color Jersey</a> that your <a href=httpwww.footballbengalsstore.com14_ANDY_DALTON_JERSEYMENS_ANDY_DALTON_GAME_DRIFT_FASHION_JERSEY_NIKE_NFL_CINCINNATI_BENGALS_14_ORANGE.html>Men's Andy Dalton Game Orange Jersey</a> public <a href=httpwww.officialfootballraiders.com20_DARREN_MCFADDEN_JERSEYYOUTH_DARREN_MCFADDEN_LIMITED_DRIFT_FASHION_JERSEY_NIKE_NFL_OAKLAND_RAIDERS_20_GREY.html>Youth Darren McFadden Limited Grey Jersey</a> profile login will be different than your JS Everywhere login.Well he did say <a href=httpwww.officialfootballvikings.com69_JARED_ALLEN_JERSEYMENS_JARED_ALLEN_ELITE_JERSEY_NIKE_NFL_MINNESOTA_VIKINGS_69_LIGHTS_OUT_BLACK.html>Men's Jared Allen Elite Black Jersey</a> it. Belling's retort citing something the <a href=httpwww.cardinalsofficialnflshop.comWOMENS-PATRICK-PETERSON-JERSEY.html>Patrick Peterson Game Jersey</a> governor said in 2003 is his next step in the joke, an attempt to turn his own Derby forecast into something else. The governor's 2003 State of the State speech never had any connection to what Belling predicted, <a href=httpwww.officialfootballraiders.com20_DARREN_MCFADDEN_JERSEYMENS_DARREN_MCFADDEN_ELITE_DRIFT_FASHION_JERSEY_NIKE_NFL_OAKLAND_RAIDERS_20_GREY.html>Men's Darren McFadden Elite Grey Jersey</a> of <a href=httpwww.nikechiefsnflstore.comWOMENS-ALEX-SMITH-JERSEY.html>Alex Smith Limited Jersey</a> course.While we strive for a lively and vigorous debate of the issues, we do not tolerate name calling, foul language or other inappropriate behavior. Please see our discussion guidelines and terms of use for more information.
+
{{Manual|emacs|Basic-Keyboard-Macro|Basic Keyboard Macro}}
 +
 
 +
'''Keyboard macros''' can be used to automate or repeat tedious editing tasks in Emacs.
 +
 
 +
 
 +
==Basic Use==
 +
 
 +
; {{Keys|F3}}, or {{Keys|C-x (}}
 +
: Start defining a macro.
 +
; {{Keys|F4}}, or {{Keys|C-x )}}
 +
: Stop defining a macro.
 +
; {{Keys|F4}}, or {{Keys|C-x e}}
 +
: Execute a macro
 +
; {{Keys|C-u 37 C-x e}} or {{Keys|C-u 37 F4}}
 +
: Execute a macro multiple times, using [[prefix argument]]
 +
; {{Keys|C-u 0 C-x e}}
 +
: Execute a macro until the end of the buffer
 +
 
 +
==Example usage==
 +
 
 +
Consider the standard <code>*scratch*</code> buffer:
 +
 
 +
<pre>
 +
;; This buffer is for notes you don't want to save, and for Lisp evaluation.
 +
;; If you want to create a file, visit that file with C-x C-f,
 +
;; then enter the text in that file's own buffer.
 +
</pre>
 +
 
 +
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.
 +
 
 +
# Make sure [[point]] is at the start of the buffer.
 +
# 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.
 +
# 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.
 +
# Hit backspace to delete that "a".
 +
 
 +
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.
 +
 
 +
<ol start="5">
 +
<li>Hit <code>C-e C-f</code> to move point to the beginning of the next line.</li>
 +
<li>Hit <code>C-x )</code> to finish the recording of our macro.</li>
 +
</ol>
 +
 
 +
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.
 +
 
 +
<ol start="7">
 +
<li>Hit <code>C-x e</code> once to call that macro.</li>
 +
<li>Continue hitting <code>e</code> to call it several times. Hit any other key to get out of the macro repetition.</li>
 +
</ol>
 +
 
 +
== Naming and saving macros ==
 +
 
 +
You can:
 +
* give a name to the most recently defined macro (<code>C-x C-k n</code>)
 +
* bind it to a key sequence (<code>C-x C-k b</code>)
 +
* insert it in the current buffer as lisp code (<code>M-x insert-kbd-macro</code>).
 +
 
 +
{{Manual|emacs|Save-Keyboard-Macro|Save Keyboard Macro}}
 +
 
 +
 
 +
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.
 +
 
 +
==Variables==
 +
 
 +
Variables can be stored in lisp or in [[registers]]. Here's an example using lisp:
 +
 
 +
<pre>
 +
[M-: (setq x 1)]
 +
<F3>
 +
Line number [C-u M-: x]
 +
[M-: (setq x (+ x 1))]
 +
<F4>
 +
</pre>
 +
 
 +
Now execute the macro four times with the command <code>C-x e e e e</code> and you get:
 +
 
 +
line number 1<br />
 +
line number 2<br />
 +
line number 3<br />
 +
line number 4
 +
 
 +
 
 +
= See also =
 +
 
 +
* [https://github.com/Silex/elmacro elmacro], to show keyboard macros as emacs-lisp
 +
* [https://github.com/abo-abo/centimacro centimacro] to (temporarily) bind any number of macros to any global shortcuts.
 +
* You can use keyboard macros à la vim in [[evil]].
 +
 
 +
 
 +
[[Category:Intermediate]]
 +
[[Category:Editing]]

Revision as of 15:34, 28 August 2014

Basic Keyboard Macro (`(info "(emacs) Basic Keyboard Macro")')

Keyboard macros can be used to automate or repeat tedious editing tasks in Emacs.


Basic Use

[F3], or [C-x (]
Start defining a macro.
[F4], or [C-x )]
Stop defining a macro.
[F4], or [C-x e]
Execute a macro
[C-u 37 C-x e] or [C-u 37 F4]
Execute a macro multiple times, using prefix argument
[C-u 0 C-x e]
Execute a macro until the end of the buffer

Example usage

Consider the standard *scratch* buffer:

;; This buffer is for notes you don't want to save, and for Lisp evaluation.
;; If you want to create a file, visit that file with C-x C-f,
;; then enter the text in that file's own buffer.

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.

  1. Make sure point is at the start of the buffer.
  2. Hit C-x ( to start recording your macro. Note: If you hit C-g or if an error occurs, your keyboard macro recording will stop.
  3. Hit C-s followed by a to find the first "a". Now, point is right after the first "a" in the text.
  4. Hit backspace to delete that "a".

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.

  1. Hit C-e C-f to move point to the beginning of the next line.
  2. Hit C-x ) to finish the recording of our macro.

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.

  1. Hit C-x e once to call that macro.
  2. Continue hitting e to call it several times. Hit any other key to get out of the macro repetition.

Naming and saving macros

You can:

  • give a name to the most recently defined macro (C-x C-k n)
  • bind it to a key sequence (C-x C-k b)
  • insert it in the current buffer as lisp code (M-x insert-kbd-macro).

Save Keyboard Macro (`(info "(emacs) Save Keyboard Macro")')


To bind a keyboard macro to a key use C-x C-k b. To avoid problems caused by overriding existing bindings, the key sequences C-x C-k 0 through C-x C-k 9 and C-x C-k A through C-x C-k Z are reserved for your own keyboard macro bindings. You can, however, bind a keyboard macro to whatever you like.

Variables

Variables can be stored in lisp or in registers. Here's an example using lisp:

[M-: (setq x 1)]
<F3>
Line number [C-u M-: x]
[M-: (setq x (+ x 1))]
<F4>

Now execute the macro four times with the command C-x e e e e and you get:

line number 1
line number 2
line number 3
line number 4


See also

  • elmacro, to show keyboard macros as emacs-lisp
  • centimacro to (temporarily) bind any number of macros to any global shortcuts.
  • You can use keyboard macros à la vim in evil.