Ubuntu command-line suggestion

I've been using Ubuntu 20.04 and trying to get used to it. but repeating same command in terminal is quit boring. i just want some suggestion to get rid of it and how can i get visible command-line suggestion in terminal.

to clarify.. I'm trying to ask, i want a command-line suggestion when i am writing a command or attempt to write (if i need to install any application suggest please)


Solution 1:

If you use the up arrow on your keyboard, your terminal will write the last line ran. You can use that multiple times to go multiple commands back. If you got too far, use the down arrow to do the opposite. You can also write "!!" and it will replace that with the last command at runtime (after you pressed enter). This trick can also be used inside a command, to add something to the command.

If you press "Tab", your terminal will try to complete the command if its obvious what you want, and if its not obvious, you can press tab again to see the commands you can make from that not-so-obvious command.

Edit, I don't know if this works on Ubuntu server, but it works on desktop

Edit, added info based on comments

Solution 2:

Another option comes from installing fzf (command line fuzzy finder): it's an interactive filter for command-line that can be used with any list, including command history.

You have two alternative ways to install it:

  • sudo apt install fzf or
  • git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf && ~/.fzf/install

I suggest the second one because it installs also the key-bindings, while the version in the Ubuntu repositories installs fzf executable alone.

Key-bindings are important because, once activated, if you press CTRL+R and start typing some text, you can search a command in your history.

In the picture below, you can see that typing apt instal I got the list of matching commands from my terminal history.

fzf

For further details, you can visit the GitHub fzf page.

Solution 3:

There are many ways you can search your bash history to speed up your work. A commonly used one is to add the following lines to your .inputrc file in your ~ directory:

"\e[A": history-search-backward
"\e[B": history-search-forward

Now start typing the first few characters of a command (that your previously used) and press up and down arrow keys to see your commands.

Solution 4:

You can “browse” your history of commands executed in the past by using the reverse-i-search within the terminal:

  1. Press Ctrl + R
  2. Enter a command (or part of it)
  3. Press Ctrl + R again to cycle through the matches (repeat if required).
  4. Press Enter (or Ctrl + O) to Execute the match or press Ctrl + C (or Ctrl + G) to Cancel reverse-i-search.

Solution 5:

A couple of very simple tricks that are super useful when you have to run very similar commands with just a couple of changes:

  • You can use ^string^replacement^ to run the previous command but replacing the first occurrence of string with replacement. For example:

    $ echo "Hello there, General Kenobi. What you have there?"
    $ ^there^world^
    echo "Hello world, General Kenobi. What you have there?"
    Hello world, General Kenobi. What you have there?
    

    Note that it outputs the command that is actually being run, and then you have the actual output

  • You can use !!:gs/pattern/replacement/ to run the previous command with all occurrences of the regex pattern replaced by replacement.

     $ echo "Hello there, General Kenobi. What you have there?"
    Hello there, General Kenobi. What you have there?
    $ !!:gs/there/world/
    echo "Hello world, General Kenobi. What you have world?"
    Hello world, General Kenobi. What you have world?
    

    Note that both instances of there were changed.

  • You can use Alt+. to insert the last argument of the previous command. If you keep pressing the shortcut you cycle with previous commands:

    $ echo a
    $ echo b
    $ echo c
    $ echo ...
    

    On the last line pressing Alt+. once will insert c, pressing it again will swap it out for b and again for a.

    You can also use Alt+1 and then Alt+. to insert the first argument instead of the last, similarly for the 2nd, 3rd etc:

    $ echo a b c
    $ echo 
    

    Here doing Alt+1 Alt+. will insert a, while using 2 in place of 1 will replace b and so on.