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:

Sometimes I type a lengthy command in my command prompt and then notice that I need to check something before actually pressing return and executing the command. I press ctrl+c, execute the new command and then type the cancelled command again and execute it.

Is there a way to "save" the command I am to cancel with ctrl+c and restore it later when I finished the "check something" command?

Note that I do not want to open another terminal, because I am on a remote server via ssh.


$ git commit -am 'FOO-123: fix issue with dorks around bars'^C
$ git status
# On branch foo
modified: foo.php
$ git commit -am 'FOO-123: fix issue with dorks around bars'
1 file changed, 46 deletions(-)
share|improve this question

marked as duplicate by Gilles, Stéphane Chazelas, vonbrand, jasonwryan, warl0ck Mar 21 '13 at 2:00

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.

With zsh you can use push-line, just press ESC-q in emacs keybindings –  Ulrich Dangel Mar 20 '13 at 13:38
add comment

5 Answers 5

In Unix, you could use command-line editor for such a thing. I typically run with set -o emacs so I can use the following Emacs keys directly on the command line:

  1. display your command line
  2. Ctrl-A - go to the beginning of the command line
  3. Ctrl-K - cut the entire command line into an internal buffer
  4. ... do all needed work here...
  5. Ctrl-Y - paste command from buffer into the command line

You can also set -o vi and then use vi shortcuts for this sort of thing.

share|improve this answer
I have never used emacs for editing/coding, and have always used VIM, but VIM keys for the shell can be a major slowdown sometimes(VIM is much more suited for large expanses of text, not single lines). As such, I have been shown the Ctrl-K shortcut several times, and each time I have to smack my forehead. "Oh yeah! I forgot about that!" –  Drake Clarris Mar 20 '13 at 18:54
add comment

Instead of ctrl-c, move to the beginning of the line and do this:

$ history -s "git commit -am 'FOO-123: fix issue with dorks around bars'"

Hit enter. It won't execute, but it's now the last line in the history. Not sure if this is bash specific.

share|improve this answer
It's not in POSIX, but works with bash and anything that aims for bash compatibility. –  jw013 Mar 20 '13 at 15:39
I do something similar - I'll go to the front of the line and insert echo. When I need to bring it back, go up in the history, remove the echo, and there we go. One advantage is not needing to fiddle with escaping quotes if the command already uses both. –  Izkata Mar 20 '13 at 20:54
add comment

What I always do is put a # comment marker in front of the command. I can later recall it in the history, remove the comment marker and then execute the command.

share|improve this answer
+1 simple and effective –  wim Mar 21 '13 at 1:22
add comment

Prepend the name of any command which does nothing important to what you have written, and run the resulting command line. This is along the lines of @goldilocks's answer, but more generic.

Examples would be true, false and echo. Just move to the beginning of the command line and prepend whatever command you prefer to what you have already written.

$ true git commit -am 'FOO-123: fix issue with dorks around bars'

Since true is essentially a no-op, the command is now in the history (regardless of which shell you are using, as long as it has a command history at all) and can be recalled normally whenever you are ready, but nothing else has happened. When you are ready, recall the command from the history, back up to the beginning of the command line and remove the no-op command before executing.

The only real downside of this is that you lose whatever the exit status was from the previous command ($? in bash-speak). If you are doing more than a simple command invocation (chaining operations like ;, && and ||, pipes, redirection, ...) you may need to quote the command before passing it to the no-op command, so the shell doesn't do its magic on part of the command. Something like false && ( ... ) (with ... being what you had before) might also work; since false will return an unsuccessful result, the shell will never get to the remainder of the command line.

Another option, especially if you are running in a graphical environment, is to simply open another terminal window. In a CLI-only environment the same can be accomplished by starting screen first thing when you log in -- then you can trivially detach the current session and start a new one.

share|improve this answer
Instead of a command I just use a #, which works as long as you have not told your shell to ignore comment lines. –  jw013 Mar 20 '13 at 15:18
@jw013 That's a good approach too to reduce typing, but I was shooting for an option that was shell-agnostic. –  Michael Kjörling Mar 20 '13 at 15:26
I think # is a comment character in every shell that I have seen, even weird ones like csh. It's even in the POSIX spec (see item 10.). I'd love to know if you've seen shells where # is not a comment. –  jw013 Mar 20 '13 at 15:38
It's not in zsh in interactive mode (by default): ➜ #ls zsh: command not found: #ls –  constantius Mar 20 '13 at 15:46
I have always used echo as the command for this. This can cause issues if your weird characters interpreted by the shell, like * or !, so I often times go with the single quotes around it all as well. –  Drake Clarris Mar 20 '13 at 17:51
add comment

Similar to Goldilocks' suggestion, move to the beginning of the line (Ctrl-A with Emacs behaviour) and prepend with '#'. That will turn you command into a comment instead of executing it. Any tricks you use to recall commands (such as Ctrl-R or history) will be able to recall the command and then you can move to the beginning and remove the #.

share|improve this answer
add comment

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