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.

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 ClarrisMar 20 '13 at 18:54

It's not in POSIX, but works with bash and anything that aims for bash compatibility.
–
jw013Mar 20 '13 at 15:39

1

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.
–
IzkataMar 20 '13 at 20:54

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.

Instead of a command I just use a #, which works as long as you have not told your shell to ignore comment lines.
–
jw013Mar 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örlingMar 20 '13 at 15:26

1

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.
–
jw013Mar 20 '13 at 15:38

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 ClarrisMar 20 '13 at 17:51

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