-1

I type 3 commands in my terminal, one after another:

command1
command2
command3

If I use the up arrow key on my keyboard, I can access the previous commands. Suppose I want to use those commands in a new command. For example, the fourth command might look like this command1 && command2 | command3, How can I use shortkeys (e.g. up arrow key) to access previous commands when I'm in the middle of a new command?

These commands are very long, what's the fastest way to write the fourth command?

2 Answers 2

7

I’m not sure it can be done with arrow keys, but you can use history shortcuts:

!-3 && !-2 | !-1

!-3 will be replaced by the third-last command, !-2 the second-last, and !-1 the last.

3

If you have Emacs-style line editing enabled in bash, then the arrow-key method is possible.

Building the command left-to-right is long-ish, but works:

UpUpUp ... (to reveal command1)

Ctrl-UCtrl-Y ... (to copy but not cut command1)

Down ... (to reveal command2)

Home Ctrl-Y Space & & Space ... ( to paste command1 && in front of command2)

End Ctrl-U ... (to cut command1 && command2)

Down ... (to reveal command3)

Home Ctrl-Y Space | Space ... (to paste command1 && command2 | in front of command3)

Slightly fewer keystrokes are required to build the command from right to left:

UpCtrl-U ... (to reveal and cut command3)

UpSpace|SpaceCtrl-YCtrl-U ... (to cut command2 | command3)

Up Space & & Space Ctrl-Y ... (to reveal command1 and then append && command2 | command3)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .