5

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)

4
  • Are you running Ubuntu Desktop with a graphical desktop environment or Ubuntu Server with a text screen interface?
    – sudodus
    Oct 6, 2021 at 11:36
  • I recommend checking out the 2 useful answers for this auto-complete in ubuntu question
    – pugi
    Oct 6, 2021 at 11:42
  • history has a lot of what you did stored :)
    – Rinzwind
    Oct 6, 2021 at 13:07
  • 1
    Are you looking for any completion suggestion or only regarding your previously executed commands (see history)?
    – marianoju
    Oct 7, 2021 at 7:54

6 Answers 6

9

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

6
  • 2
    Yes, it works in Ubuntu Server too :-)
    – sudodus
    Oct 6, 2021 at 11:39
  • 1
    and history for a numbered list where you can use !111 to execute line 111 and !! can also be used to add "sudo" when you forgot to add it in the previous command. Those windows users do not know what they are missing ;-)
    – Rinzwind
    Oct 6, 2021 at 13:08
  • 2
    And on the theme of using the up-arrow: you can press it as many times as you want to scroll up through the history - useful if you know you typed a command recently but can’t remember exactly how many it was (& if you overshoot you can down-arrow back). There are some tips for navigating on the line if you want to edit a recent command: the ones I find most useful are ctrl-a to go to the start of the line, ctrl-e to the end, meta-f to go forward 1 word and meta-b back 1 word.
    – Will
    Oct 6, 2021 at 19:18
  • Thank you @Will, I will include that in my answer
    – Irsu85
    Oct 7, 2021 at 6:49
  • You might add that a double "Tab" gives you a list of suggestions if the auto complete is ambigous. Oct 7, 2021 at 9:00
6

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.

1
  • Worth noting ctrl-R is builtin and displays previous history matches and can search forwards and backwards.
    – qwr
    Oct 7, 2021 at 5:56
3

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.

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.

3

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.
3
  • 1
    (Or just press Enter to execute a match)
    – canton7
    Oct 7, 2021 at 12:59
  • 1
    'Enter' seems to also execute the match. 'End' or arrows will let you edit the command before running it. Ctrl+C also aborts the search, and is probably easier to remember. Oct 7, 2021 at 18:27
  • Arrow keys also let you edit the command
    – Irsu85
    Nov 10, 2021 at 8:16
0

Use Manter. It is an opensource terminal which has IDE like autocomplete suggestions. https://github.com/iondodon/manter

You must log in to answer this question.

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