Remember a half-typed command while I check something

I often find myself in the following position: I’ve started typing a long command at the bash prompt, but half-way through I find out I need to check something with another command. This is a problem when I’m at the console (no X), which is often the case, because then I only have the following unsatisfactory ways to do it, that I know of:

  • Hit ctrl+alt+F2 and log in on another virtual console, and find out what I wanted, then go back and continue
  • ctrl+a, type echo + space + enter, find out what I wanted, press until I find my command, ctrl+a, del x 5, ctrl+e, and continue
  • Highlight what I’ve typed so far with my mouse (if gpm is running, which it usually is), press ctrl+c to interrupt, find out what I wanted while being careful not to use my mouse to highlight stuff, then press middle mouse button on a new prompt and continue
  • Pray to the command line gods that the half-written command will have no adverse effects but simply fail, and gingerly press enter, then find out what I wanted, press uparrow until I get my command back, and continue
  • Jump into my time machine, travel back in time and remind myself to start screen before starting to type command, travel back to the present, press ctrl+a c, find out what I wanted, press ctrl+a ctrl+a, and continue

So what I want to know is, is there some more elegant way of doing this? A sort of subshell-command or similar?

I’m most interested in methods that do not require any preparations or setup to work.

Do ctrl-A to get to start of line, prepend a hash and press return – this way it will stay in the command history as a comment and you can get back to it with cursor up

EDIT: just noticed this is a slightly more elegant variant on your #2 😉

Answered By: jamespo

Hit Ctrl+A, Ctrl+K to move to the start of line and delete (kill) to the end of it. Then do your research, and when ready to continue hit Ctrl+Y (yank) to put your saved (killed) line back.

Answered By: alex

An alternative way, not perfect though, press Ctrl-X Ctrl-E. This will open your default editor containing your half typed command. Then you may save your command to a file or open a new shell from within your editor. When you exit the new shell it will return you to the editor and saving the contents will automatically execute them.

This is not perfect because it doesn’t return you to your original shell, but either forces the execution of the command or completely discards it. But there are no temporary selections or kill buffers to worry about, and there are also some nice tweaks, e.g. if editor=vi

:wq            # execute the command
:cq            # cancel and abort
:! command     # execute command
:shell         # open a new shell
:r filename    # insert the contents of a file for editing
:r ! command   # insert the output of a command for editing
Answered By: forcefsck

In zsh, I type Ctrl+Z to “suspend” the command I’m typing and type another command. After I’ve run that command (or aborted it with Ctrl+C), the suspended command comes back for edition (even remembering the cursor position). As an additional convenience, if the command line is empty when I type Ctrl+Z, the key invokes the bg built-in instead (so a double Ctrl+Z sends a running command directly to the background).

fancy-ctrl-z () {
  emulate -LR zsh
  if [[ $#BUFFER -eq 0 ]]; then
    bg
    zle redisplay
  else
    zle push-input
  fi
}
zle -N fancy-ctrl-z
bindkey '^Z'          fancy-ctrl-z

I don’t know how to do something similar in bash.

In any shell, you can use the low-tech method of adding a # at the beginning of the current line.

A somewhat faster version of alex’s Ctrl+A Ctrl+K (which moves to the front of the line and then cuts everything forward) is to just use Ctrl+U, which cuts backward on bash, and the entire line (regardless of your current position) on zsh. Then you use Ctrl+Y to paste it again

Answered By: Michael Mrozek

In bash, just type Alt+#.

Then when you’re ready, press Up then Alt+#.

The first puts a # at the start of the line to turn it into a comment, then accepts the line, as if you had pressed Enter.

Then, when you do it a second time, it sees the line already has a # at the start, so it removes it, and accepts it, again saving you the Enter key.

If the second time just inserts another #, then you have to type Alt+- Alt+# instead.

You can make zsh do the same by putting

bindkey -M emacs 'e#' pound-insert

in your ~/.zshrc.

Or, if you are using vi bindings, you can type # from command mode in both bash and zsh.

Answered By: Mikel

In addition to the ctrla ctrlk trick, if you’re in screen, just ctrla d to detach then reattach with screen -r

You could also just open another window with ctrl+a c

Answered By: warren

One of my favorite features in zsh is the builtin push-line function that takes care of this without any of the hackery in other answers here. I have it bound to Ctrl+l in my .zshrc file like this:

bindkey '^L' push-line

Then when I’m typing along in some command and need to do something else real quick I can invoke that with one key and get a blank prompt. After running the other command, the prompt is automatically filled out with what I was typing before.

You can even chain multiple layers of this and the commands will come back at you in reverse order that you pushed them into the queue.

Answered By: Caleb

Using zsh, if you type <ESC> q (escape, then Q), your prompt line will be cut, so that you can type another command. You will automatically get back your cut text in the next prompt.

It can remember several lines at the same time, so you can use it while a line is waiting for you. You can even use it on blank line to delay the resume of your cut line.

It’s not the most efficient way if you have to use several commands for the checking/preparation (you’ll have to retype <ESC> q before each command). But I think that it is possible to bind it to a shorter shortcut.


If you just want to check the manual, type <ESC> h (escape, then H). That will run the run-help built-in, with the first word of the command line as argument. You’ll find back your line intact once done.

Answered By: loxaxs

Similar to the method of adding # at the start of the line, there is an arguably simpler method that I use that doesn’t require returning to the beginning of the line:

Just add an open quote and don’t close it, whether single or double.

Then press Enter.

Then press CtrlC.

The partial command is saved in the command history (available via up arrow) but is not actually executed.

Answered By: Wildcard

I’ve recently written a script for this. Just Ctrl+b to remember/restore command-lines.

Download:

Visit the repository.

Demo:

demo

Answered By: ynn
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.