12

I am typing a command, I remember I have to check something, if I press Ctrl+U the command disappears and I can type something else, but then the command I was typing is not in the history.

Is there a way to recover that command, or a different shortcut that I can press to save somewhere the command?

michelpm
  • 323

3 Answers3

13

Take a look at the push-line ZLE command. This command pushes the current contents of the line onto the buffer stack and clears the line. You can then enter another command and when ZLE starts up again, the line is popped from the buffer stack into the line buffer.

prompt@world$ cp abc /mnt/usr/bin       # The user presses Esc-q
prompt@world$                           # The line is cleared
prompt@world$ mount -v /dev/sdb1 /mnt   # And the user can enter another command
mount: /dev/sdb1 mounted on /mnt
prompt@world$ cp abc /mnt/usr/bin
# The first command is popped off of the stack
#  as soon as ZLE comes back.

By default, this command is bound to Alt/Escq in emacs mode. You can bind this command to a different key with the bindkey command. For example, in order to bind it to Alt/Escv, add bindkey '^[V' push-line to your zshrc.

12

Ctrl+Y will paste the last item you cut (with Ctrl+U, Ctrl+K, Ctrl+W, etc.).

Kevin
  • 40,767
6

You're looking for the push-line command, which is bound to Ctrl+Q and Esc Q by default. This command clears the input and lets you enter another command; once you've executed or canceled this command, the previous command is brought back for editing.

With a multiline command , push-line only clears the current line, and keeps the other lines in the input buffer. There is another command push-input which clears the whole input buffer, not bound to any key by default.

I use a wrapper around this command. I type Ctrl+Z to “suspend” the command I'm typing and type another command. After I've run that command (or aborted it with Ctrl+C), the suspended command comes back for edition (even remembering the cursor position). As an additional convenience, if the command line is empty when I type Ctrl+Z, the key invokes the bg built-in instead (so a double Ctrl+Z sends a running command directly to the background).

fancy-ctrl-z () {
  emulate -LR zsh
  if [[ $#BUFFER -eq 0 ]]; then
    bg
    zle redisplay
  else
    zle push-input
  fi
}
zle -N fancy-ctrl-z
bindkey '^Z'          fancy-ctrl-z

A low-tech method that works in any shell is to add a # at the beginning of the current line and press Enter.

  • Adding to the last comment, need this for that to work well: setopt interactive_comments, and still that goes to history unless put also a space in front and have declared setopt hist_ignore_space. I still like the push-line thing, but the ctrl+z does not work for the line I'm typing. – michelpm May 02 '13 at 09:31
  • @michelpm Oh, right, you need setopt interactive_comments in zsh. Entering the line in the history is the point, if you don't then the line is lost forever. The version of fancy-ctrl-z I posted works for me under zsh -f, it may be conflicting with a setting in your .zshrc though I don't see what. – Gilles 'SO- stop being evil' May 02 '13 at 09:44
  • The way I said I can still recover as if it was in the history, but it doesn't get persisted in the history, I can not recover after I close the shell. It is just a detail, but it is useful, because you don't want to store garbage or secrets there. – michelpm May 02 '13 at 11:10
  • Your fancy-ctrl-z does work, ignore my first comment. – michelpm May 02 '13 at 11:16
  • +1 for mentioning pos1 + # + enter – bluenote10 Oct 13 '18 at 20:16