Something to try

Discussion in 'Command Line' started by Kovax, Jan 15, 2014.

  1. Kovax

    Kovax Member

    Messages:
    34
    Likes Received:
    19
    Trophy Points:
    8
    If you are a fan of the history command you should give this a try.
    If you are looking for a command that you have run in the past and type the command "history" (no quotes) it gives you a list of the last commands that were run on the system. Along with the commands it gives you a number next to each command.
    Give this a try:
    After you find the command that you would like to run again... instead of the familiar copy and paste it to run it.... try to run !number ex. !255 and enter ...poof the command will run.

    Kovax

    Yesyesloud, Virneto and DevynCJohnson like this.
  2. DevynCJohnson

    DevynCJohnson Well-Known Member Staff Member Staff Writer

    Messages:
    1,381
    Likes Received:
    1,094
    Trophy Points:
    113
    Thanks for the tip! Here is a full example for everyone to see.

    Code:
    collier@Nacho-Laptop:~$ history
      1  ls
      2  ll
      3  date
      4  history
    collier@Nacho-Laptop:~$ !3
    date
    Wed Jan 15 14:51:39 EST 2014
    
    I never use this command. I prefer to press the up-key until I see the command I want. However, not all terminals offer this feature, so many people can benefit from "history".

    By the way, to erase the history, use this command "history -c".
    Haider92 likes this.
  3. arochester

    arochester Super Moderator Staff Member

    Messages:
    909
    Likes Received:
    300
    Trophy Points:
    63
    For me this
    produces this
  4. Kovax

    Kovax Member

    Messages:
    34
    Likes Received:
    19
    Trophy Points:
    8
    What distro are you running? and do you have something at that line?
  5. Virneto

    Virneto Member

    Messages:
    46
    Likes Received:
    21
    Trophy Points:
    8
    @Kovax,

    NiceOne!!
    ;)

    Here's another one:
    to re run last inserted command just type >_
    !!

    And when you get the "need to be root" message, just type >_
    sudo !!
    Last edited: Jan 15, 2014
    Kovax and DevynCJohnson like this.
  6. DevynCJohnson

    DevynCJohnson Well-Known Member Staff Member Staff Writer

    Messages:
    1,381
    Likes Received:
    1,094
    Trophy Points:
    113
    Do you have 255 or more entries in the output of history?
    Haider92 likes this.
  7. DevynCJohnson

    DevynCJohnson Well-Known Member Staff Member Staff Writer

    Messages:
    1,381
    Likes Received:
    1,094
    Trophy Points:
    113
    The last command I found to be VERY helpful. I never thought of that. Thanks! :D
    Haider92 and Virneto like this.
  8. arochester

    arochester Super Moderator Staff Member

    Messages:
    909
    Likes Received:
    300
    Trophy Points:
    63
    No, I changed the number to less and it works...
  9. Kovax

    Kovax Member

    Messages:
    34
    Likes Received:
    19
    Trophy Points:
    8
    I like it and will add that one to my toolbox.
    Virneto likes this.
  10. KenJackson

    KenJackson Active Member

    Messages:
    129
    Likes Received:
    28
    Trophy Points:
    28
    I don't like those "!" commands because you can't see what's going to execute until it executes. I prefer to up-arrow and edit.

    I also search the history for a command which I know I've executed recently like this (you may need to veryify "emacs" is in your SHELLOPTS variable):

    Press CTRL-R and start typing a portion of the command. The most recent match will appear and update as you type. When you see the one you want, press:
    • ENTER to execute, or
    • An arrow key or emacs motion key (CTRL-E, CTRL-A, CTRL-B or CTRL-F) to edit, or
    • CTRL-R to pick the previous match
    B4RTZ4K and DevynCJohnson like this.
  11. DevynCJohnson

    DevynCJohnson Well-Known Member Staff Member Staff Writer

    Messages:
    1,381
    Likes Received:
    1,094
    Trophy Points:
    113
    @KenJackson I never knew about "CTRL-R". Thanks!

    Does anyone else have a better alternative? This thread is getting more interesting.
    Haider92 likes this.
  12. Yesyesloud

    Yesyesloud Active Member

    Messages:
    281
    Likes Received:
    140
    Trophy Points:
    43
    Quite handy. My !255: "bluez-test-adapter discoverable on". And a silly forum game comes out of !255... Although !1, !2 or !3 seem more mysterious :p
  13. nazukia

    nazukia New Member

    Messages:
    12
    Likes Received:
    3
    Trophy Points:
    3
    The CTRL-R is the one that I use the most often

    Obviously dont call "!#" without check "history before"

    The thing is that up and down is different than history, up and down in tie to your current shell session, meaning that if you have 2 different terminal, they dont have the same value for up and down

    History is tie to the user commands history.

    tips
    1)history command read from ~/.bash_history

    2) so tail -n20 ~/.bash_history, show the last 20 command you typed in any terminal

    3) The default limit line of this file can be found like this:
    echo $HISTSIZE

    4) you can edit your ~/.bashrc file to change the value of this file


    :)
    Yesyesloud and DevynCJohnson like this.
  14. pane-free

    pane-free Active Member

    Messages:
    362
    Likes Received:
    84
    Trophy Points:
    28
    I like #13 best of all. Thanks Kovax for the thread and nazukia for the post!
  15. Yesyesloud

    Yesyesloud Active Member

    Messages:
    281
    Likes Received:
    140
    Trophy Points:
    43
    After this thread, I became used to typing "history | grep (...)" to find rather old input. Ctrl+R comes even more handy, thanks.

    I've just managed to nano my way into expanding HISTSIZE to a number I guess I'll never reach... 500 is not enough for cli maniacs :p
    DevynCJohnson likes this.
  16. MikeyD

    MikeyD Active Member

    Messages:
    232
    Likes Received:
    113
    Trophy Points:
    43
    There are a bunch of good CTRL commands in most terminals, many I wasn't aware of until recently:

    Code:
    Ctrl + A    Go to the beginning of the line you are currently typing on
    Ctrl + E    Go to the end of the line you are currently typing on
    Ctrl + L                  Clears the Screen, similar to the clear command
    Ctrl + U    Clears the line before the cursor position. If you are at the end of the line, clears the entire line.
    Ctrl + H    Same as backspace
    Ctrl + R    Let’s you search through previously used commands
    Ctrl + C    Kill whatever you are running
    Ctrl + D    Exit the current shell
    Ctrl + Z    Puts whatever you are running into a suspended background process. fg restores it.
    Ctrl + W    Delete the word before the cursor
    Ctrl + K    Clear the line after the cursor
    Ctrl + T    Swap the last two characters before the cursor
    Esc + T    Swap the last two words before the cursor
    Alt + F    Move cursor forward one word on the current line
    Alt + B    Move cursor backward one word on the current line
    Tab        Auto-complete files and folder names
    Yesyesloud, ZZs and DevynCJohnson like this.
  17. KenJackson

    KenJackson Active Member

    Messages:
    129
    Likes Received:
    28
    Trophy Points:
    28
    Also, ALT-D deletes the word after the cursor. I use that one.

    That's kind of the complement of CTRL-W. It's a shame they don't look like complements.
    DevynCJohnson likes this.
  18. Richard Rodriguez

    Richard Rodriguez Member

    Messages:
    75
    Likes Received:
    15
    Trophy Points:
    8
    I really like this thread...cool things

Share This Page