This question already has an answer here:

Every Linux user has experienced this annoying thing: you begin typing a long and boring command, then realise you should have executed another one before. How to save the first one to execute it later?


You begin typing

mycommand -a -F --conf /very/long/path --and /another/one /input/file.txt

But before pressing "Enter", you realise you should've done cp f.txt /input/file.txt at first.

So, you're stuck with your command, and if you don't press Enter you won't be able to have it back using your bash history.

What's the best way to handle this?

marked as duplicate by Gilles bash Jul 5 '14 at 11:05

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
    Keep more than one terminal window handy. There is no need for restraint! – user44370 Jul 4 '14 at 12:09
  • 4
    you call that a long command? noobs.... ;D – msb Jul 4 '14 at 18:39

Hit CTRL-U (kill line - this saves the line in the shell's kill-ring), do what you need to do, then at the new prompt, hit CTRL-Y (yank from kill-ring) to get back the original command.

Alternatively, and this is particularly useful if you are in a nested command, such as a while or for loop, hit CTRL-C, which adds the command to history without executing it and clears the line, so you can then recall it using the shell's history mechanism when you are ready to use it.

  • 4
    You don't need to go to the beginning of the line - you can use C-u to copy line to clipboard – Arkadiusz Drabczyk Jul 4 '14 at 12:46
  • @ArkadiuszDrabczyk Well, I never knew that. Thanks for the tip! – D_Bye Jul 4 '14 at 12:51
  • 2
    It's very easy to forget about it and kill something else. Plus, some weird versions of ksh have broken kill-rings. That's why I always use Esc-# (multiple, easy to recall, visible). – arielCo Jul 4 '14 at 14:10
  • 1
    @arielCo Yes, it is easy to kill something else. In that case, after CTRL-Y returns the wrong item, you can cycle back through the kill ring with M-Y. Don't know if this works in ksh - I never use it. It works in bash and zsh, though. – D_Bye Jul 4 '14 at 14:39
  • @D_Bye I had never bothered to look that up! Sadly, it doesn't work in Korn, even in the 'sane' versions. There's one mention of a 'stack' in the Emacs mode section of the manual, but I'm too busy/lazy to find out more. – arielCo Jul 4 '14 at 17:09

Comment the line then press enter


so you keep the line in the history

  • 2
    Unless you're in screen. Turns out Ctrl-A merely goes to the beginning of the line, so the Home key would work as well. (Home -> # -> Enter) – Luc Jul 4 '14 at 23:32
  • @Luc or you can use tmux which replaces ctrl-a by ctrl-b. – Emmanuel Jul 5 '14 at 9:36
  • 1
    @Luc this isn't an issue given that you can do ^AA to send ^A in screen. @emmanuel this isn't really a good reason to switch to tmux since tmux has the same problem (e.g. ^b being useful in vim) also screen could be configured to use ^b. – foo Jul 6 '14 at 20:21

Esc-# is like putting a # at the beginning and pressing Enter:


$ find /{,usr/}{,s}bin/ -type f -ls | awk '$3~/^...s/' | nl


$ #find /{,usr/}{,s}bin/ -type f -ls | awk '$3~/^...s/' | nl
$ _

Works in Bash and the Korn shell.

  • 4
    I usually do "Esc-#" as "Alt+Shift+3". – Vi. Jul 4 '14 at 22:16

With zsh in emacs mode, Alt+Q or Ctrl+Q (push-line widget), pushes the current command-line on a stack (saves for later) and clears the current input buffer.

Then you can enter another command, after which the last pushed command is brought back (popped from that stack).


If you specifically want to save the command, I usually do what Emmanuel suggested. But if you just want to enter another command before the current one, you don't need to save it. Go to the beginning of the line, enter the other command and add a semicolon.

Ctrl-A cp f.txt /input/file.txt ; Enter

This should execute both commands, in the order that you want. What's more, it will be saved in your history together, so if in the future you want to do the 2nd command again and you reach it with Ctrl-R, you will see the first command there and be reminded that it must be executed before (in the case you always need to execute both together).

You may also want to use && instead of semicolon, if you want the 2nd command to be executed only if the first is successful.

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