DEV Community

Kenneth Lum
Kenneth Lum

Posted on

Editing and using a command quite some time ago on Bash

In Bash or Zsh, we can in general use the up arrow to get back the command line some 20 or 25 lines ago, edit the command, press Enter and execute it.

However, pressing the up arrow key 20 or 25 times can be tedious, and we need to look closely when we get that exact line, which can be tiring on the eyes.

One quicker way is this:

  1. Use h or history to look at the list of past commands. When needed, use history -300 to look at 300 lines of history
  2. Use !127 to execute the command line 127 as shown above "as is" with no editing, but to edit it, then:
  3. Use !127:p to just print that command line without executing it, and put it into history
  4. Then, use the up arrow once and you can get that command, and edit it as you wish (using left, right arrow, CTRL-A to go to the beginning, or CTRL-R to search backward, etc, depending on your settings)

Top comments (0)