Score:3

Ubuntu command-line suggestion

cn flag

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)

sudodus avatar
jp flag
Are you running Ubuntu Desktop with a graphical desktop environment or Ubuntu Server with a text screen interface?
pugi avatar
vn flag
I recommend checking out the 2 useful answers for this [auto-complete in ubuntu question](https://askubuntu.com/q/280327/1193677)
cn flag
`history` has a lot of what you did stored :)
us flag
Are you looking for _any_ completion suggestion or _only_ regarding your previously executed commands (see `history`)?
Score:8
cn flag

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

sudodus avatar
jp flag
Yes, it works in Ubuntu Server too :-)
cn flag
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 ;-)
Will avatar
id flag
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.
Irsu85 avatar
cn flag
Thank you @Will, I will include that in my answer
André Stannek avatar
us flag
You might add that a double "Tab" gives you a list of suggestions if the auto complete is ambigous.
Irsu85 avatar
cn flag
Added, thanks André
Score:5
gr flag

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.

qwr avatar
kr flag
qwr
Worth noting ctrl-R is builtin and displays previous history matches and can search forwards and backwards.
Score:3
cn flag

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.

Score:3
us flag

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.
cn flag
(Or just press Enter to execute a match)
HolyBlackCat avatar
cn flag
'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.
Irsu85 avatar
cn flag
Arrow keys also let you edit the command
Score:2
sa flag

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.

Score:0
in flag

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

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.