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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question

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.

Keep more than one terminal window handy. There is no need for restraint! – L'aditdabenlà Jul 4 '14 at 12:09
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.

share|improve this answer
I love this one! – user74488 Jul 4 '14 at 12:08
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
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
@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

Comment the line then press enter


so you keep the line in the history

share|improve this answer
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
@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.

share|improve this answer
+1 that's great – Emmanuel Jul 4 '14 at 14:28
I usually do "Esc-#" as "Alt+Shift+3". – Vi. Jul 4 '14 at 22:16
This only works with the real Esc key (on layer one of your keyboard layout) but not with a Esc sent from layer 4 of Neo keyboard layout. – erik Jul 5 '14 at 0:29
@erik: the application in your computer (e.g. PuTTY, gnome-terminal, xterm) needs to send 0x1B (27 decimal) to the terminal for whatever key you intend to be 'Esc'. A simple test is to run cat and press the Esc key: you should see ^[. – arielCo Jul 5 '14 at 0:44
@arielCo: Sorry, I just confused the keys. Yes it works! Thanks for the hint. – erik Jul 5 '14 at 1:13

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).

share|improve this answer
awesome tricks. – test30 Feb 23 at 14:17

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.

share|improve this answer

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