Using bash history to get a previous command, copy it and then 'run' it but with the command commented

后端 未结 10 1028
伪装坚强ぢ
伪装坚强ぢ 2021-01-29 20:34

Just a question to improve my bash skills. I always do this:

$ history | grep some_long_command

...
...
123 some_long_command1.........
124 some_l         


        
相关标签:
10条回答
  • 2021-01-29 20:56

    You can get to edit mode by hitting M-^ (option-shift-6 on a mac).

    Type this:

    !123M-^

    And you'll be editing command #123. It's sort of like using ctrl-r, but starting with exclamation-point syntax.

    0 讨论(0)
  • 2021-01-29 21:00
    !123:gs/old/new/
    

    Will run command 123 replacing the string 'old' with the string 'new'.

    0 讨论(0)
  • 2021-01-29 21:04

    ^p to get the last typed command in unix/solaris

    0 讨论(0)
  • 2021-01-29 21:10

    Actually, you can just append :p to the command to print it without actually running it. For example:

    $ ls -la
    $ !!:p
    

    Will print out ls -la as the previous command without running it, and you can just press (up) to find it and edit it.

    You can also do

    !123:p
    

    to print out the 123rd command as your previous command.

    0 讨论(0)
  • 2021-01-29 21:10

    Instead of using the history command, bind history-search-backward/history-search-forward to key shortcuts which can be remembered easily (I prefer PgUp/PgDown). To do that, put this into your .inputrc file:

    "<key code>":  history-search-backward
    "<key code>":  history-search-forward
    

    To get <key code>, type Ctrl-V <key> in the shell, and replace the starting ^[ with \e in whatever was output.

    After this is set up, you can just type some and press PgUp to get some_long_command. If you need some_long_command with_some_arg but there is a similar command some_long_command with_some_other_arg later in the history, you can cycle through until you reach it by typing some and then hitting PgUp repeatedly, or you can type some, hit PgUp, move the cursor to where the two commands start to differ, type a few characters and hit PgUp once more. This ability to quickly page through / differentiate between similar commands makes it in my opinion a much more comfortable tool than Ctrl-R.

    0 讨论(0)
  • 2021-01-29 21:11

    You may wan to try "suggest box"-like history https://github.com/dvorka/hstr - it reads Bash history and allows for quick navigation.

    hh

    To get the last command simply type hh, navigate to the command and use right arrow to get it on command line (where you can edit it and/or add comment).

    0 讨论(0)
提交回复
热议问题