Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This question already has an answer here:

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?

share|improve this question
    
Another way to solve that would be to have a tabbed terminal. Just open a new tab. –  Emanuel Berg Apr 30 '13 at 22:52
    
@EmanuelBerg I know, I asked this question so I could stop doing that, I use guake and it is impossible to see which tab is open. –  michelpm May 1 '13 at 13:46
add comment

marked as duplicate by Mikel, Anthon, slm, Hauke Laging, jasonwryan Mar 1 at 19:04

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3 Answers

up vote 4 down vote accepted

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

share|improve this answer
2  
...And `Alt + y' will cycle though all you previously yanked –  xaccrocheur Apr 30 '13 at 13:56
    
Thanks a bunch! Ctrl+K and Alt+Y didn't work on zsh 5.0.0 (Ubuntu 13.04 x86_64). –  michelpm Apr 30 '13 at 14:16
    
@michelpm: Yeah? Try zsh --version. I have zsh 4.3.17 (i686-pc-linux-gnu) and it works for me. (Edit: Oh. That's what you did.) –  Emanuel Berg Apr 30 '13 at 22:51
add comment

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.

share|improve this answer
    
Liked this one even better! Comment something so I can upvote you again ;D –  michelpm Apr 30 '13 at 17:59
    
Thank you, but upvotes on comments don't give reputation points. –  Evan Teitelman Apr 30 '13 at 19:15
    
I was just kidding, didn't notice I was blinking? ;D –  michelpm Apr 30 '13 at 19:30
add comment

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.

share|improve this answer
    
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 2 '13 at 9: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 May 2 '13 at 9: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 2 '13 at 11:10
    
Your fancy-ctrl-z does work, ignore my first comment. –  michelpm May 2 '13 at 11:16
add comment

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