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?


5 Answers 5


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 Jul 4, 2014 at 12:46
  • @ArkadiuszDrabczyk Well, I never knew that. Thanks for the tip!
    – D_Bye
    Jul 4, 2014 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, 2014 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, 2014 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, 2014 at 17:09

Comment the line then press enter


so you keep the line in the history

  • 3
    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, 2014 at 23:32
  • @Luc or you can use tmux which replaces ctrl-a by ctrl-b.
    – Emmanuel
    Jul 5, 2014 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, 2014 at 20:21
  • Make sure to NOT add a whitespace before the # symbol, as you shell might have the funcionality of NOT saving commands that start with spaces. It's a common behaviour.
    – msb
    Mar 7 at 5:45

In Bash and the Korn shell (Emacs mode) Esc-# is like putting a # at the beginning and pressing Enter:


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

Esc # ;

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


1: In some terminals, Alt+whatever as a chord is another way of sending Esc followed by whatever

2: # means whatever you need to press to get a '#' so you might have to press Esc then Shift+3 or Alt+Shift+3

(thank you @vi)


In Bash, you can control the prefix by adding a line like this to .inputrc:

set comment-begin "## "

(The quotation marks are only necessary if you have leading or trailing blanks)

You can also set another binding and unbind the default Esc-#:

"\e'": insert-comment
"\e#": nop


  • 4
    I usually do "Esc-#" as "Alt+Shift+3".
    – Vi.
    Jul 4, 2014 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 .