15

I found the following command in my .zshrc, probably copied from some website:

bindkey " " magic-space # do history expansion on space

The manpages are about as helpful as the very succinct comment:

magic-space
    Perform history expansion and insert a space into the buffer. [...]

So what does magic-space actually do? Under which circumstances can I see this history expansion in action?

1 Answers1

19

magic-space performs history expansion: if you type a space after a command that starts with ! (or ^) to refer to (part of) a previous command, that history reference is expanded. If you just type a space, the history reference is expanded when you press Enter.

$ zsh -f
% echo hello
hello
% !1 world
echo hello world
hello world
% bindkey " " magic-space
% !1

At this point, press Space, and the line turns to

% echo hello ¡

where ¡ represents the posititon of the cursor.

  • Maybe this is obvious for most users of the shell, but I had to go looking for this property. It was set in .oh-my-zsh/lib/key-bindings.zsh with some misleading comment: bindkey ' ' magic-space # [Space] - don't do history expansion – Daniel Kaplan Apr 06 '23 at 08:11