Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Emacs Keyboard Macro and Examples
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  6 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Xah Lee  
View profile  
 More options Nov 16 2010, 9:45 pm
Newsgroups: comp.emacs, comp.lang.lisp
From: Xah Lee <xah...@gmail.com>
Date: Tue, 16 Nov 2010 18:45:26 -0800 (PST)
Local: Tues, Nov 16 2010 9:45 pm
Subject: Emacs Keyboard Macro and Examples
new article on emacs keyboard macro as well as elisp code.

• 〈Emacs Keyboard Macro and Examples〉
http://xahlee.org/emacs/emacs_macro_example.html

plain text version follows

--------------------------------------------------
Emacs Keyboard Macro and Examples

Xah Lee, 2010-11-16

This page shows a example of real world use of emacs keyboard macro
system, and a emacs lisp example.

Emacs has a feature that lets you record a sequence of keystrokes. I
learned the basics since i started to use emacs in 1998, but actually
never used it much, maybe few times a year. Since 2007, i learned the
command “apply-macro-to-region-lines”, which made it more useful.
Still, i use it perhaps once a month. However, when you use it, it
saves you hours for doing the job.

First, let's see what it does.
How to record a sequence of keystrokes?

To record keystrokes, press 【Ctrl+x (】 (kmacro-start-macro), then
start typing your keystrokes. When done, press 【Ctrl+x )】 (kmacro-end-
macro). This records your keystrokes.

To run the keystrokes you've recorded, press 【Ctrl+x e】 (kmacro-end-
and-call-macro) or call “apply-macro-to-region-lines”.
Command Name    Key
start-kbd-macro 【Ctrl+x (】
end-kbd-macro   【Ctrl+x )】
call-last-kbd-macro     【Ctrl+x e】
apply-macro-to-region-lines     ◇
Saving Your Keyboard Macro for Future Use

If you want to use your keyboard macro (kmacro) for future use, you
can save it. To save the macro:

    * (1) Call “name-last-kbd-macro”.
    * (2) call “insert-kbd-macro”. This will insert the lisp code for
the kmacro at the cursor point.
    * (3) Put the code in your emacs init file.
    * (4) Then, you can execute your kmacro like this: 【Alt+x
‹yourMacroName›】.

Turn Off line-move-visual

When you record keystrokes, it is better that the arrow up/down keys
move the cursor thru a logical line, as opposed to visual line. (Emacs
23's default is visual line.) To set to logical line, call “set-
variable”, then give “line-move-visual”, with value “nil”. (“t” for
true; “nil” for false).

When you play back macro, be sure the “line-move-visual” is the same
as when you recorded it.
Example of Actual Use

Here's a example of how i use keyboard macro. Usually, i use macro
about once or few times a month. When you need it, it is extremely
time saving. Otherwise you might spend hours doing it, or even if you
know elisp well.

Today, i have about 60 lines. Each line is a url, like this:

http://xahlee.org/3d/3D_inputs.html

I need it to be like this:

• 〈Mathematical Models of 3D Inputs Control〉
http://xahlee.org/3d/3D_inputs.html

Where the first line is the title of that page.

I have 60 other lines like this:

http://xahlee.org/PageTwo_dir/Personal_dir/dvorakKeymap.txt
http://xahlee.org/Periodic_dosage_dir/Logitech_G13_Gameboard.html
http://xahlee.org/Periodic_dosage_dir/bangu/pinyin_frequency.html
http://xahlee.org/Periodic_dosage_dir/logitech_trackball.html
http://xahlee.org/Periodic_dosage_dir/mouses.html
http://xahlee.org/Periodic_dosage_dir/trackball.html
...

I've written a elisp code that turns a url on my site into the format
i want. Here's the lisp code:

(defun xah-cite ()
"Change the file path under cursor into title and path.

For example, this line
 /Users/xah/web/xahlee_org/emacs/emacs.html
becomes
• 〈Xah's Emacs Tutorial〉
  http://xahlee.org/emacs/emacs.html

The title came from HTML file's title tag.
File path must be a url scheme, full path, or relative path.

Example url schemes among browsers and OSes

 C:\\Users\\xah\\web\\xahlee_org\\emacs\\emacs.html ; ie
 file:///C:/Users/xah/web/xahlee_org/emacs/emacs.html ; firefox
 file:///C:/Users/xah/web/xahlee_org/emacs/emacs.html ; google chrom
 file:///C:/Users/xah/web/xahlee_org/emacs/emacs.html ; safari
 file://localhost/C:/Users/xah/web/xahlee_org/emacs/emacs.html ; opera
 file:///Users/xah/web/xahlee_org/emacs/emacs.html ; safari
 file:///Users/xah/web/xahlee_org/emacs/emacs.html ; firefox
 file://localhost/Users/xah/web/xahlee_org/emacs/emacs.html ; opera

Example full path variations
 c:/Users/xah/web/xahlee_org/emacs/emacs.html
 /Users/xah/web/xahlee_org/emacs/emacs.html

This is Xah Lee's personal command assuming a particular dir
structure."
  (interactive)
  (let (bds ff title)
    (setq bds (bounds-of-thing-at-point 'filename))
    (setq ff
          (buffer-substring-no-properties
           (car bds)
           (cdr bds)))

    ;; change file path to full path
    (setq ff (replace-regexp-in-string "^http://www\\." "http://" ff))
    (setq ff (replace-regexp-in-string "/$" "/index.html" ff))
    (setq ff
          (cond
           ((string-equal system-type "windows-nt") ; Windows
            (replace-regexp-in-string "^http://xahlee\\.org/" "c:/
Users/xah/web/xahlee_org/" ff)
            )
           ((string-equal system-type "darwin") ; Mac
            (replace-regexp-in-string "^http://xahlee\\.org/" "/Users/
xah/web/xahlee_org/" ff)
            )
           )
          )

    (setq ff (local-url-to-file-path ff))
    (setq ff (expand-file-name ff ))

    (setq title
          (if (string-match ".+html\\'" ff)
              (get-html-file-title ff)
            (file-name-nondirectory ff)))
    (delete-region (car bds)
                   (cdr bds))
    (insert "• 〈" title "〉\n" (xah-web-path-to-url ff))
    ))

I have defined a alias for this command to save me typing, because i
use it often. Alias is defined like this:

(defalias 'c 'xah-cite)

So, to call my command, i type 【Alt+x c Enter】 (Actually i type “Alt
+a” instead of “Alt+x”, because i'm using ErgoEmacs Keybinding.). This
can easily change each line to the format i want. But there are 60
lines! So, a keyboard macro comes to use. Here's how i do it.

    * Move cursor to the beginning of first url.
    * Type 【Ctrl+x (】 to start recording.
    * Type 【Alt+x c Enter】. To change the line to the way i want.
    * Type Enter to insert a blank line.
    * Type → to move to the beginning of next url.
    * Type 【Ctrl+x )】 to end macro recording.

Then, i select all lines that has not been processed, then call “apply-
macro-to-region-lines”. Then all lines becomes this:

• 〈dvorakKeymap.txt〉
http://xahlee.org/PageTwo_dir/Personal_dir/dvorakKeymap.txt

• 〈Logitech G13 Advanced Gameboard (Review)〉
http://xahlee.org/Periodic_dosage_dir/Logitech_G13_Gameboard.html

• 〈Chinese Pinyin Letter Frequency and Dvorak Layout〉
http://xahlee.org/Periodic_dosage_dir/bangu/pinyin_frequency.html

• 〈Logitech Trackball Mouse Reviews〉
http://xahlee.org/Periodic_dosage_dir/logitech_trackball.html

• 〈Review Of Microsoft SideWinder Gaming Mouses〉
http://xahlee.org/Periodic_dosage_dir/mouses.html

• 〈Trackball Mouse Reviews〉
http://xahlee.org/Periodic_dosage_dir/trackball.html

...

Emacs is fantastic!

In keyboard macro, you can call any emacs command, or even more than
one command. Emacs's kmacro feature simply play back the keystrokes.

In general, kmacro is extremely useful whenever you need to do
something repeatitive and the steps are the same. It is a great time-
saver. You do not need to know elisp to use it. Even if you are a
elisp master, many repeatives jobs are still best done with a kmacro.
With kmacro, i can finish this job in 30 seconds. But if i were to
write a elisp for it, it easly can take 30 min.

 Xah


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
des...@verizon.net  
View profile  
 More options Nov 16 2010, 11:20 pm
Newsgroups: comp.emacs, comp.lang.lisp
From: des...@verizon.net
Date: Tue, 16 Nov 2010 23:20:58 -0500
Local: Tues, Nov 16 2010 11:20 pm
Subject: Re: Emacs Keyboard Macro and Examples

Xah Lee <xah...@gmail.com> writes:
> new article on emacs keyboard macro as well as elisp code.
> Command Name       Key
> start-kbd-macro    【Ctrl+x (】
> end-kbd-macro      【Ctrl+x )】
> call-last-kbd-macro        【Ctrl+x e】
> apply-macro-to-region-lines        ◇
> Saving Your Keyboard Macro for Future Use

Nice article.

I use keyboard macros a lot and usually apply them to many lines.

I could never see how the binding C-x e is sufficient.
It's way too hard to type 5, 10, or 100 times.

I know it can be repeated with a count but still sometimes I want
to move around and apply it to various lines or words.

So, I use this keybinding:

(define-key global-map [(S-f19)] 'call-last-kbd-macro);; Shifted PgUp Key

This converts the Page Up key in the 6 key group above the arrow keys
into a repeat macro key.

All my bindings for the PgUp key:

(define-key global-map [(f19)] 'repeat-complex-command)
(define-key global-map [(S-f19)] 'call-last-kbd-macro)
(define-key minibuffer-local-map [(f19)] 'previous-history-element)
(define-key minibuffer-local-map [(shift f19)] 'next-history-element)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Xah Lee  
View profile  
 More options Nov 17 2010, 1:23 am
Newsgroups: comp.emacs, comp.lang.lisp
From: Xah Lee <xah...@gmail.com>
Date: Tue, 16 Nov 2010 22:23:16 -0800 (PST)
Local: Wed, Nov 17 2010 1:23 am
Subject: Re: Emacs Keyboard Macro and Examples

On Nov 16, 8:20 pm, des...@verizon.net wrote:

nice and interesting.

i didn't know that PageUp key is also f19 in emacs?
A quick search in elisp manual doesn't seem to find it. Any pointer?

i have never used “repeat-complex-command” myself.

for me, the page up/down keys are used more for things related to
moving by chunk.

Ctrl+PageUp = previous-user-buffer
Ctrl+PageDn = next-user-buffer

Hyper+PageUp = search-pagebreak-prev
Hyper+PageDn = search-pagebreak-next

(the search-pagebreak-prev is pretty much emacs's backward-page, but
backward-page's got a problem in that it doesn't do so if the formfeed
char ^L is part of a comment, sometimes required because the lang take
^L as syntax error)

other my combo with Page key are pretty standard:

Shift+PageUp = scroll-down with selection.
Alt+PageUp = scroll-other-window-down

same for Down...

for extra function keys, i use the keys on the number pad, either
system-wide or within emacs.

e.g.

+       = switch to last app
-       = next song
Shift+- = last song
1       = describe-char
4       = ispell-word
0       = lookup-word-definition
Ctrl+0  = lookup-wikipedia
.        = xah-browse-url-at-point

many of these functions are personally defined ones or in ErgoEmacs.

for system-wide, it's either defined in AutoHotkey or IntelliType, and
sometimes app dependent.
The f1 to f12 are all used up. They pretty much are one-key press to
switch to a particular app. e.g. emacs is f6, Firefox is f7, Google
Chrome is f4, IE is f9, PowerShell is f10...

 Xah


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Brendan Halpin  
View profile  
 More options Nov 17 2010, 4:10 am
Newsgroups: comp.emacs, comp.lang.lisp
Followup-To: comp.emacs
From: brendan.hal...@ul.ie (Brendan Halpin)
Date: Wed, 17 Nov 2010 09:10:02 +0000
Local: Wed, Nov 17 2010 4:10 am
Subject: Re: Emacs Keyboard Macro and Examples

On Wed, Nov 17 2010, des...@verizon.net wrote:
> I could never see how the binding C-x e is sufficient.
> It's way too hard to type 5, 10, or 100 times.
[...]
> So, I use this keybinding:

With your keybinding you will have missed that in recent Emacs e will
repeat the execution of the keyboard macro, once you have done C-x e a
first time.

Brendan
--
Brendan Halpin,  Department of Sociology,  University of Limerick,  Ireland
Tel: w +353-61-213147 f +353-61-202569 h +353-61-338562; Room F1-009 x 3147
mailto:brendan.hal...@ul.ie  http://www.ul.ie/sociology/brendan.halpin.html


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
des...@verizon.net  
View profile  
 More options Nov 17 2010, 6:01 pm
Newsgroups: comp.emacs, comp.lang.lisp
From: des...@verizon.net
Date: Wed, 17 Nov 2010 18:01:38 -0500
Local: Wed, Nov 17 2010 6:01 pm
Subject: Re: Emacs Keyboard Macro and Examples

Sorry.  I forgot that I had some xmodmap magic in my environment
for that.

f19 should be "Prior" on a standard setup.

(I have some stuff that tries to make my Linux keyboard a little
consistent with my Sun keyboard.)

> i have never used “repeat-complex-command” myself.

> for me, the page up/down keys are used more for things related to
> moving by chunk.

...snip

Yep I think a bunch of single keystroke Emacs functions really
help.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
des...@verizon.net  
View profile  
 More options Nov 17 2010, 6:07 pm
Newsgroups: comp.emacs
From: des...@verizon.net
Date: Wed, 17 Nov 2010 18:07:52 -0500
Local: Wed, Nov 17 2010 6:07 pm
Subject: Re: Emacs Keyboard Macro and Examples

brendan.hal...@ul.ie (Brendan Halpin) writes:
> On Wed, Nov 17 2010, des...@verizon.net wrote:

>> I could never see how the binding C-x e is sufficient.
>> It's way too hard to type 5, 10, or 100 times.
> [...]
>> So, I use this keybinding:

> With your keybinding you will have missed that in recent Emacs e will
> repeat the execution of the keyboard macro, once you have done C-x e a
> first time.

Nice.

Of course if I move the pointer the "e" stops working.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »