How to get real-time completion/suggestions in Linux terminal?

10,205

Solution 1

This Unix & Linux post describes what you want:

... I've come across a great, universal tool: command-line fuzzy finder.

It primarily allows you to “fuzzy-find” files (check the rich gif animation by the link above), but it also allows to feed arbitrary text data to it and filter this data. So, the shortcuts idea is simple: all we need is to maintain a file with paths (which are shortcuts), and fuzzy-filter this file. Here's how it looks: we type cdg command (from “cd global”, if you like), get a list of our bookmarks, pick the needed one in just a few keystrokes, and press Enter. Working directory is changed to the picked item:

cdg

It is extremely fast and convenient: usually I just type 3-4 letters of the needed item, and all others are already filtered out.


It may not be what you're looking for, but the Vim editor with the CtrlP plugin can be used for this:

enter image description here enter image description here enter image description here

You'll need to install one of vim, vim-nox, vim-gnome, vim-gtk or vim-athena for this to work. Installation instructions for CtrlP are provided in its website:

  1. Clone the plugin into a separate directory:

    $ cd ~/.vim
    $ git clone https://github.com/kien/ctrlp.vim.git bundle/ctrlp.vim
    
  2. Add to your ~/.vimrc:

    set runtimepath^=~/.vim/bundle/ctrlp.vim
    
  3. Run at Vim's command line:

    :helptags ~/.vim/bundle/ctrlp.vim/doc
    
  4. Restart Vim and check :help ctrlp.txt for usage instructions and configuration details.

If you have never used Vim before, it can be a bit intimidating. Run vimtutor to get an idea of it.

Solution 2

You can write your own completion scripts for your shell.

For example, bash uses simple shell scripts / functions for completion. Install bash-completions if you haven't already and take a look at /usr/share/bash-completion/completions for some examples.

In your script you could simply call locate to get a list of matches and return them.

Share:
10,205

Related videos on Youtube

username123
Author by

username123

hello world!

Updated on September 18, 2022

Comments

  • username123
    username123 over 1 year

    I would like to have something similar to everything in windows (using locate): every time I press a key, the file list that matches the current inputs gets updated immediately.

    I am wondering is that possible to do real-time text completion in terminal and how should I do that?

    To make it more clear, suppose I have this function implemented as "foo", then when I type foo a and hit TAB in the command line, it should give a list of all files with name containing a, then if I continue to type b (now the command becomes foo ab), then the file list automatically (without need to hit anything else) gets updated to those containing ab, and so forth.

    • username123
      username123 over 8 years
      How to get real-time updates of the results?
    • username123
      username123 over 8 years
      @muru Thanks, but that is not what I am asking. I mean the results need to be updated every time I press down a new key.
    • muru
      muru over 8 years
      What are you trying to accomplish here? What's the intended use case?
    • username123
      username123 over 8 years
      @muru I added a description, if you could take a look.
    • 0x2b3bfa0
      0x2b3bfa0 over 8 years
      You could try to use a shell called fish. It does some auto-completion magic, however it may be not what you want.
    • username123
      username123 over 8 years
      @Helio Thanks, is it similar to zsh?
    • 0x2b3bfa0
      0x2b3bfa0 over 8 years
      @username123: Yes, it's similar to zsh. However (as far as I know) it does some autocompletion on the user input. As I'm not a Windows user, I can't know if that is what you want.
    • 0x2b3bfa0
      0x2b3bfa0 over 8 years
      @username123: Please check this asciinema recorded in fish by me: asciinema.org/a/det1crzgnph8m9fwklq50pwqq