Questions tagged [keyboard-macros]

A "keyboard macro" is a command defined by an Emacs user which represents a sequence of keys. Calling a keyboard macro is equivalent to typing that key sequence.

Filter by
Sorted by
Tagged with
44
votes
3answers
4k views

How to save a keyboard macro as a Lisp function?

Very frequently I would need to perform the set of steps requiring multiple key strokes. For the same session those steps can be recorded in a keyboard macro. An example is saving a frequently ...
12
votes
2answers
526 views

How to handle next-line in keyboard macro?

I created a keyboard macro to join lines in a buffer using: F3 C-n M-x join-line RET F4. It works fine, except when lines get too long and start to wrap – which makes this macro dependent on the ...
10
votes
2answers
436 views

What might be unused keys on Emacs that generally can be used to bind keyboard macros?

I read that the usual way to bind key to keyboard macros are C-x C-k 0 to 9 and A to Z. So for example, after we define a keyboard macro, we can type C-x C-k b and Emacs will ask for a key to bind. ...
10
votes
4answers
595 views

How to resume an INCOMPLETE keyboard macro recording?

Classic use-case: You start recording a macro: C-x ( You do some stuff, let's say hello. You run a command that throws an error, let's say C-g (e.g. you mistakenly run C-s and you have to cancel that ...
8
votes
4answers
1k views

How can I run a keyboard macro in selected buffers from Ibuffer or selected files from dired?

If I have to do some variable name refactoring, I use ag and multiple-cursors. But when I have to do some code block removal/addition/moving, I use keyboard macros. But I need to switch to each ...
8
votes
2answers
302 views

Translating strings to keypresses

Given a string in elisp, I'd like to simulate emacs receiving whatever keypresses are necessary to re-create that string. So given the string "Hello" emacs would act exactly the same as if I had ...
7
votes
2answers
566 views

query-replace in keyboard macro

Let me define a keyboard-macro: I enter C-x (, then go to beginning with M-<, then type M-% to query-replace some occurrences of foo by bar. Out of 3 occurrences found, I replace only the last 2, ...
7
votes
2answers
606 views

Sleep in an kbd macro

I was completing the python koans in emacs, and I was using a kdb macro to test my changes in a shell buffer and jump back into the python code. I wanted to take the line number of the next error from ...
6
votes
2answers
199 views

Key bound to string does not handle some chars in string correctly

(I ask this question after reading this one.) The form (global-set-key (kbd "M-e") "e") is, as far as I can tell, equivalent to (global-set-key (kbd "M-e") (lambda () (interactive) (insert "e"))) ...
6
votes
1answer
650 views

Is possible to execute a key binding in a function?

Say, the C-M-j is bound to c-indent-new-comment-line for c-mode (and c++-mode) and indent-new-comment-line for other modes, but I want to define a function that will execute C-M-j to c-indent-new-...
6
votes
1answer
164 views

Function to count how many times a keyboard macro is invoked?

I am quite bad at counting and remember how many times I have invoked a special command, so I wonder is there any way to count and show in the minibuffer how many time a keyboard macro was invoked? ...
5
votes
2answers
267 views

Keybinding for buffer word count

The following in my .emacs file does not produced the desired result of showing me the word count for the current buffer (it "does nothing"): (global-set-key (kbd "<f9>") (kbd "C-u M-=")) Is ...
5
votes
3answers
471 views

Saving and restoring highlighting patterns

I have some log files that I spend a lot of time reading, and I find myself doing the same series of commands to get useful highlighting each time I open a new log. Typically some combination of M-x ...
5
votes
2answers
1k views

Incrementing characters in Emacs, next to numbers?

You can place a pointer on a number, like 1, and increment it to 2. There are many alternatives on this. See also the Emacs Wiki: https://www.emacswiki.org/emacs/IncrementNumber 7 alternatives to ...
5
votes
1answer
319 views

Define a keyboard macro to indefinitely execute another keyboard macro and then do something else

I've defined one keyboard macro, bound to keystroke 1, say, and I want to define a second keyboard macro that executes the first one indefinitely and then returns to the beginning of the buffer. I ...
5
votes
1answer
233 views

Editing a stored keyboard macro and storing the edited version

In my ~/.emacs, there are several keyboard macros stored. They appear as (fset 'dhteu-macro-name ... entries. BTW, dhteu is just a random alphabet combination prefixed to avoid collision with any ...
4
votes
4answers
291 views

Write `var0, var1, var2, …, var10` in a buffer

Most of the times there is no need to write manually many variables with the same name and a different index, like var0, var1, var2, var3, ..., var9 because one can use arrays, vectors or whatever ...
4
votes
2answers
375 views

Inserting the plus minus unicode symbol ± as a keyboard macro

I insert the plus minus symbol ± frequently enough that I want to bind it to a key. From emacs -Q, I've tried this: (global-set-key (kbd "C-c m") "±") However, when I enter C-c m, the minibuffer ...
4
votes
1answer
278 views

Execute keyboard macro in the current buffer

I need to be able to parse a string like the following: M-: (forward-word 4) RET and execute it from elisp. For that purpose I'm trying to use execute-kbd-macro and edmacro-parse-keys but I'm a ...
4
votes
1answer
114 views

Binding a key sequence to a key sequence instead of a command

In my .emacs, I tried to use (global-set-key (kbd "M-r") (kbd "C-x r j") so I can use M-r to jump to a register. However, doing this and using the command gives After 0 kbd macro iterations: Args ...
4
votes
1answer
320 views

Keyboard macro repeats indefinitely

I'm reading GNU Emacs Manual to learn how to use Emacs in its full power. I now I'm trying to make use of keyboard macros. It's fairly easy, but I cannot understand why the first defined macro repeats ...
3
votes
1answer
50 views

KBD macros defined in emacsclient disappear when client is closed

I defined a macro in an emacs client, which was attached to my emacs server. I closed the client, and opened the client on a new file, and attempted to run the macro on the new file. In both the new ...
3
votes
2answers
197 views

How do I get an initial M-x argument?

I use an asterisk in my function names, so that to execute one of my own functions, I would begin by typing M-x *. Now I would like to save myself a keystroke and achieve the same by M-X. How do I ...
3
votes
1answer
66 views

Octal character representation in keyboard macros

I generated a macro as a help for generating indices for words I have marked before. Now this macro looks as follows (fset 'index "\367\\index{\C-y") What does \367stand for and where can I look ...
3
votes
3answers
1k views

Ringing Bell in Macro with Search

It throws the error After 0 kbd macro iterations: isearch-search: Keyboard macro terminated by a command ringing the bell and I have no idea why, nor can I find anything about it by searching. Any ...
3
votes
3answers
159 views

Keyboard macros or command for quick refiling using dired or similar

I scan all the mail (not email, but good ol' mail) I receive and have these saved as PDF files into a directory, which we think of as an inbox. I would like to find a workflow to quickly refile these ...
3
votes
2answers
567 views

Org-mode: any keyboard shortcut for Latex code?

On p.33 of the Org-guide Carsten Dominek writes you can use #+LATEX: and #+BEGIN_LATEX ... #+END_LATEX construct to add verbatim LATEX code Is there a keyboard combination or default snippet that ...
3
votes
1answer
562 views

How to stop execution of keyboard macro?

I mistakenly ran a keyboard macro with an Infinite Argument, which made the macro execute endlessly. Take the following example, which just inserts the character a: C-x ( a C-x ) If I then run: C-u ...
3
votes
2answers
233 views

Cua-mode and keyboard macros

I have just started using emacs, so I decided to use cua-mode. I got this weird problem when I'm using macros and copy/cut (C-c/ C-x). let's assume this simple macro for example: <F3> C-S-right ...
3
votes
1answer
173 views

How can I enter some text in a keyboard macro that uses helm mode?

I want a keyboard macro that does something like this: Call org-refile Insert two characters(In this case '') So whenever I invoke the keyboard macro, it lets me enter some other text, but ...
3
votes
0answers
48 views

Emacs keyboard macros ? all over

I have used keyboard macros for a long time, but moved between companies, systems, and various machines over my career. I used to be able to record macros and insert-kbd-macro into my .emacs file and ...
3
votes
1answer
163 views

Process unread-command-events and redisplay

(defun pretend (keys) (dolist (e (reverse (listify-key-sequence (kbd keys)))) (push e unread-command-events) ;; (mystery-function) (redisplay t) (sleep-for 0.1))) (pretend "M-x my-...
3
votes
0answers
61 views

Replace formula by computed value

I'd like to select a simple formula like 17+5 and replace that by the computed result, 22 in this case. And I need this to work during keyboard macro execution, to quickly process large portions of my ...
3
votes
3answers
711 views

Evil mode: How to run evil-indent on the text I've just pasted

When I copy line three below in evil-mode function foo() { } var bar; #copy and paste it at line two, I get function foo() { var bar; #paste } instead of the correctly indented function foo() { ...
2
votes
1answer
74 views

alphabetic keyboard macro “counter”?

I very much {like, use} the kmacro-*-counter family of commands. However I sometimes find myself needing to generate alphabetic sequences (e.g., A, B, C, ...) instead of integer sequences (e.g., 1, 2, ...
2
votes
1answer
147 views

Repeat actions in slow motion, for presentation purposes

I would like to repeat actions (preferably as a keyboard macro, but I am open to other solutions), in some kind of automated, "slow motion". By "slow motion", I mean at a speed that is close to human ...
2
votes
1answer
64 views

Keyboard macro produces a series of numbers

I did some editing. Then went to C-x C-k l edited the stuff to suit my needs. Saved it with C-c C-c. Gave it a name with C-x C-k b. Finally, went to ~/.emacs and did M-x insert-kbd-macro To my ...
2
votes
1answer
101 views

Inhibit redraw during keyboard macros?

In the good old days, repeated emacs keyboard macros ran without repainting the display until the end. Over the years this changed, and now it appears that the display is redrawn 100% of the time -- ...
2
votes
0answers
30 views

Keyboard macro with occur

In a keyboard macro, there is an occur command. While recording the macro, I switched to the occur window, did some editing, closed occur buffer reverted back to original buffer to continue editing. ...
2
votes
0answers
36 views

Can the kmacro internal format contain literal commands?

I'm trying to modify key-chord.el so that you can have key-combination specific timeouts and I noticed that key-chord uses heuristics to decide when two keys pressed sequentially in a kmacro are ...
1
vote
3answers
1k views

How to delete all instances of a character in a bunch of files?

I was given a directory of files in which I was asked to make some systematic changes. Because it was a one-off task, and because there were only a few hundred files I thought I'd use an Emacs ...
1
vote
2answers
2k views

Configuring multiple key bindings

I would like to bind M-S-up to C-u 7 S-up to move a timestamp a week forward (and down for back). A modification of this answer resulted in a "Symbol is void" error in Emacs. I tried modifying the ...
1
vote
2answers
66 views

Removing preceding text from each line of a text [duplicate]

I am new to Emacs. I am reading a book on Python. The book has many code examples. What I would like to do is to convert this: 61. def getRandomWord(wordList): 62. # This function returns a ...
1
vote
1answer
207 views

Transform selected text like a Python macro

I've only just started with Emacs (about 6 weeks now) so I'm not really familiar with its coding language. I was wondering if it was possible to write, for Emacs, the equivalent of a Python macro I ...
1
vote
1answer
173 views

What is the difference between `C-i` and `<C-i>`?

I've just set (define-key input-decode-map "\C-i" [\C-i]) (define-key input-decode-map "\C-m" [\C-m]) (define-key input-decode-map "\C-[" [\C-\[]) in my config, but I noticed that C-i is now ...
1
vote
1answer
45 views

Visualise keyboard macros (converting them to human readable key names)

I often find useful/nifty kmacros on the web, like (kmacro-exec-ring-item (quote ([return 3 16 14 5 return 21 3 46 return 24 98 return] 0 "%d")) arg). Being a curious guy I am, I usually want to know ...
1
vote
2answers
76 views

Writing a small elisp function

Could someone here write a small Elisp function for me? What I would like Emacs to do is the following: I have a region like this: [0.04579] [0.06569] [0.09507] [0.12696] [0.27929] [0....
1
vote
1answer
118 views

How to use double keyboard macro, or keyboard macro of keyboard macro?

First of all, I made a new keyboard macro called 'mac'. [F3] abcd [F4] M-x name-last-kbd-macro mac Then, I can get 'abcd' by typing [F4] or M-x mac. I tried to get 'abcd' five times. So I made a new ...
1
vote
3answers
60 views

How to create loops for inserting text?

Imagine I want to create a list with 10 items, i. e. 1. 2. 3. ... 10. In Vim I can use the command :for i in range(1,10) | put =i.'. ' | endfor for this. How can I do the same thing in Emacs? ...
1
vote
1answer
123 views

How can I stop keyboard-quit from ending macro definition?

When I am defining keyboard macro with kmacro-start-macro and I press C-g (for example to deactivate mark or I press it accidentally), macro definition is aborted and I have to start over. Is there ...