One frustrating thing that happens very often to me is that I type a long command into a command-line interface (CLI) and then realize I need to execute another command before that one. Sometimes I open a second CLI to execute that command, but at other times that's not possible. So I end up deleting the text I've already typed, typing and executing the precedent command, and finally re-typing and executing the original command.
Here are the best alternatives I know of:
In the Linux Terminal, if I have a mouse, I can carefully highlight the text with the mouse, right-click -> Copy, and clear the line with Ctrl
+U
. When I want the copied text again, I can either Ctrl
+Shift
+V
, middle-click, or right-click -> Paste. Without a mouse, I'm lost.
In the Windows Command Prompt, if I have a mouse, I can right-click -> Mark, carefully highlight the text with the mouse, Ctrl
+C
to copy, and hold Backspace
to clear the line. When I want the copied text again, I can right-click -> Paste. Without a mouse, I'm lost.
I feel like there should be a better way. The ideal solution to me would be to have some kind of keyboard command that stores the text I've typed on the current line for later and removes it from the line at the same time, as well as a second command that would re-insert the text on the command line. Such a solution would be faster and more comfortable because it would have fewer steps, no need to switch between the mouse and the keyboard, and no need to worry about highlighting exactly the right amount of text.
Best Answer
bash (Unix/Linux/Mac OS X)
(also MSYS, incl. Git Bash for Windows)
Kill Ring Method
If Ctrl+U, Ctrl+K, Ctrl+W or similar is pressed during the typing of the new line (thus killing new text), the old text is not lost. The kill ring can be rotated after a yank (Ctrl+Y) using Meta+Y (or Alt+Y). In this way, you can store multiple commands and retrieve them at will.
Comment Method
Storing the command:
#
to comment out the line.Retrieving the command:
#
. Now you have the original command.Windows Command Prompt
QuickEdit Method
(Still involves mousework):
Pre-condition: On the command prompt's title bar, right-click -> Properties -> check "QuickEdit Mode" -> OK.
Note: This solution is less than ideal because it doesn't work if your command takes up more than one line (as displayed); the split between lines will be interpreted as if you had pressed Enter at that point.
Windows PowerShell
Comment Method
Storing the command:
#
to comment out the line.Retrieving the command:
#
. Now you have the original command.Here's a great Wikipedia page that includes many of these shortcuts and more.