How to store text already typed in the command line for later use

command linecopy/pasteuser interface

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

  1. If you're not at the beginning or end of the line, press either Home or End to move the cursor to one end.
  2. Press Ctrl+U to kill (cut) everything to the left of the cursor or Ctrl+K to kill everything to the right of the cursor.
  3. Press Ctrl+Y to "yank" the text back into the command line.

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:

  1. Press Home or Ctrl+A to move the cursor to the beginning of the current line.
  2. Type # to comment out the line.
  3. Press Enter (gets the comment into your history).

Retrieving the command:

  1. Press until you reach the commented command.
  2. Press Home or Ctrl+A to move the cursor to the beginning of the line.
  3. Press Delete to delete the #. 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.

  1. Highlight text by dragging with the left mouse button.
  2. Right-click inside the command prompt window to copy.
  3. Press Ctrl+Home to clear everything to the left of the cursor or Ctrl+End to clear everything to the right of the cursor.
  4. Right-click inside the command prompt window to paste.

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:

  1. Press Home or Ctrl+A to move the cursor to the beginning of the current line.
  2. Type # to comment out the line.
  3. Press Enter (gets the comment into your history).

Retrieving the command:

  1. Press until you reach the commented command.
  2. Press Home or Ctrl+A to move the cursor to the beginning of the line.
  3. Press Delete to delete the #. Now you have the original command.

Here's a great Wikipedia page that includes many of these shortcuts and more.

Related Question