I would like to create a command which fetches matching previous command, something like this:

match-latest "ssh root@150"

but which then exits and populates the command prompt with the most recent match, e.g.:

owilliams@OWILLIAMS010451 ~/go/ % ssh root@

and leaves it for the user to edit (if desired) and press enter themself. Is this possible? How would I do it?

I am using zsh 5.8 on a MacBook Pro OSX

  • 1
    you mean like pressing CTRL+r and then typing letters? Apr 26 at 9:43
  • actually yest that appears to do it. At first I thought it might only cover the current session but it appears to grab former sessions also Apr 26 at 10:07

1 Answer 1


offers a plethora of options for retrieving previous command lines from its history. Here’s just a small selection of these:

  • Press Up Arrow to step through previous command lines.
  • Press Alt. to step through only the last word of each previous command line.
  • Press ControlR to start searching through previous lines.
  • Type a word at the start of a new command line and press AltP to search for lines starting with that word.
  • Type !! anywhere on your current command line, then press AltSpace to replace it with the previous command line.
  • Type $history[1] anywhere, then press Tab to replace it with the previous command line.
  • Type fc (“fix command”) on a new command line and press Enter to open the previous command line in an external editor (configured by setting $EDITOR), after which the modified command line is immediately executed.

For more info, read the Zsh manual.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.