16

I frequently find my self in this scenario. I'm in the middle of typing a command and I need to check something else before I complete it.

Is there a way to open a subshell of some sort with my current input so far being remembered, then when I exit this subshell I'm back to where I was?

$ mylongcommand -n -e <SOME KEY COMBINATION WHICH OPENS A SUBSHELL>
$ date
...
$ exit
$ mylongcommand -n -e <BACK TO WHERE I WAS>

I'm using zsh

  • 2
    Why not just open another terminal window? – Barmar Mar 13 at 16:44
  • @Barmar For me, I don't use a GUI on most of my linux systems so I would have to switch TTY's. Which means I have to re-authenticate. The options below are much more efficient. – Zachary Brady Mar 14 at 12:39
  • 3
    You could use screen or tmux to get multiple virtual terminals on the console. – Barmar Mar 14 at 14:51
16

There is the key combination EscQ which saves the command buffer and allows to enter a new command. After running the command the buffer contains what you typed before. If you have to run another command before finishing this you can type EscQ again.

(I didn't try to open a subshell after pressing EscQ yet.)

See http://zsh.sourceforge.net/Intro/intro_10.html and search for "esc-q"

  • This exactly what I wanted, thanks a lot! I could make it run a nested shell instead of returning straight away but for my use case returning straight away works perfectly. – Panayiotis Mar 13 at 12:02
19

I typically use Ctrl+U which erases the current line and saves it into a buffer. From there I do what ever it was I needed to before the mylongcommand. Then when I am ready I use Ctrl+Y to paste mylongcommand back in my prompt.

Remember a half-typed command while I check something

7

My solution to this is decidedly low-tech and clunky, but relies on key-sequences you're probably already familiar with: Hit Ctrl+A, then #, then ENTER.

This has the effect of inserting a # at the start of the line, thus making it a comment, then executing it, thereby pushing it into the command history. You may then enter your auxiliary command, then scroll up through the command history to the commented command, remove the # and complete the command as needed.

  • You should press the "home" button on your keyboard, it saves 1 keypress, and the energy required to actually press it – Ferrybig Mar 14 at 14:45
  • @Ferrybig - yes, that's good, though for my my day-to-day keyboard is a MacBook Pro which doesn't have a HOME key. The HOME equivalent is FN + LEFT ARROW, but that scrolls up to the top of the terminal. – Digital Trauma Mar 14 at 17:39
2

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

(Previously posted at Remember a half-typed command while I check something which mainly has solutions for bash, where you can't do anything so convenient. This is a slightly fancier version of the built-in binding of M-q to push-line.)

This doesn't run a subshell: the command that you type after “suspending” the previous one runs in the same shell. This lets you do things like changing the current directory if you realize after typing a command that you meant to run it in a different directory. If you want to run a new shell instance, you can run zsh from a key binding, but I don't think that's particularly useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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