12

I would like to permanently save a keyboard macro that I've recorded using C-x ( and C-x ). Is there an easy way to "see the source" for my newly recorded macro so that I can bind it to a keyboard shortcut in ~/.inputrc?

I thought dump-macros would do it, but it doesn't seem to...

2
  • 1
    Well, I'll be damned. I didn't know about this feature. Jun 11, 2011 at 0:53
  • I use gnome-terminal and gconf-editor for configuring keyboard shortcuts.
    – saeedgnu
    Jun 11, 2011 at 12:39

5 Answers 5

5

Turns out it's really not possible, that is, it wasn't possible until tonight!

Since I was curious to dig into the bash source code anyways I went ahead and added this feature :). We'll see if the bash maintainers apply my patch.

3

if it's the last macro defined this should work nicely...

GNU Bash Handbook: Print the last keyboard macro defined in a format suitable for the inputrc file.

print-last-kbd-macro ()
1
  • 4
    Holy crap that's the interface I proposed in my patch 10 years ago! I had no idea this actually made it in because my patch never got any comments, but it looks like the bash authors implemented it from scratch.
    – mgalgs
    Aug 25, 2021 at 21:36
1

Best I could think of is to try:

echo >>.inputrc '<ctrl-x e>'

Or if the macro has multiple lines try:

echo >>.inputrc <<EOF
<ctrl-x e>
EOF

If you have used control characters you may wish to put a sed in there to replace them with the right escape codes for inputrc.

Then go in and edit the last line to have the right prefix and suffix for what you are trying to achieve ...

1
  • save a step, edit .inputrc, position to wherever you want the macro and C-x e to 'type it in'. But yeah, I'd say this is the best way, of course, you can put it anywhere you want, it's just a bunch of keystrokes. If you're not in the 'right mode/place/program', you'll likely get misunderstood inputs. But one would expect that if you didn't use the macro in the right place, right?
    – lornix
    Sep 18, 2011 at 22:08
0

Add this line to .inputrc:

alias macro
0
0

It appears that .inputrc allows you to just edit your commands in. I tried using dump-macros also but couldn't get it to work nor is there much documentation about it. Perhaps it doesn't work or is deprecated.

If there was a command like xev for emacs bindings, that would be helpful. In any case, I think your best bet is to edit inputrc using the guidelines in the first link below at the bottom of the page. It's quite easy to do.

The second link is an example inputrc file for help figuring out how to set up macros.

As an aside, there is also a cool command called bind -P which will list all of your macros.

http://www.gnu.org/software/bash/manual/html_node/Miscellaneous-Commands.html#Miscellaneous-Commands

Here's an example inputrc file:

http://www.gnu.org/software/bash/manual/html_node/Sample-Init-File.html#Sample-Init-File

2
  • Yeah, the whole point is to not have to write the macro by hand, but instead print out the one I just recorded...
    – mgalgs
    Jun 21, 2011 at 4:06
  • turns out dump-macros simply prints out what your currently defined macros would output.
    – mgalgs
    Oct 14, 2011 at 8:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .