How to make terminal autocomplete when there are several files/directory?

145

Solution 1

Something that is a life-saver for me is to have bash cycle through the possibilities instead of showing a dumb list.

As bash is using readline for its auto-completion, add the following lines to ~/.inputrc

Once you're satisfied and have thoroughly tested below solution for a few days/weeks, cut and paste (don't copy!) the same settings from ~/.inputrc to /etc/inputrc which contains the system-wide settings, making this available to all users on your system (including guest).

The codez:

# mappings to have up and down arrow searching through history:
"\e[A": history-search-backward
"\e[B": history-search-forward
# mappings to have left and right arrow go left and right: 
"\e[C": forward-char
"\e[D": backward-char

# mapping to have [Tab] and [Shift]+[Tab] to cycle through all the possible completions:
"\t": menu-complete
"\e[Z": menu-complete-backward

then exit your terminal (or remote terminal like putty) and open it again...

Examples:

  1. When you have 3 files: file1, file2 and file3 and you type:

    e fTabTabTab

    it'll cycle through:

    e file1
    e file2
    e file3
    

    and when you want to cycle backwards, just hit Shift+Tab

  2. When you type:

    very-complicated-command with lots of command line parameters
    

    and next time you need the same command, you just type:

    very

    and it'll type for you:

    very-complicated-command with lots of command line parameters
    

This will save you a ton of time in bash! ;-)

Solution 2

After the 1st tab you need to insert more letters. So if you type

cd a

and hit tab you get nothing and after a second tab (immediately following) you get a list of the names starting with a and then need to insert an f to have it auto complete the remainder so

cd atabtabftabtab

will result in

cd afoo
Share:
145

Related videos on Youtube

OverMars
Author by

OverMars

Updated on September 18, 2022

Comments

  • OverMars
    OverMars almost 2 years

    Update: Im trying to update an EF entity without knowing the column until run time. So I would like my front end to post to my api with a column name and its value, then have my api update the database by:

    • Finding the column to be updated (using provided string)
    • Update db using provided value

    So given the following data inside my webapi controller:

    string colName = 'firstName';
    string colValue = 'Sam';
    

    In my EF data retrieval, instead of

    var user = db.Users.Where(x => x.firstName == colValue);  //firstName is actual column name
    

    can I do something like

    var user = db.Users.Where(x => x.'colName' == x.colValue);  //colName is the string representation of column
    

    and the somehow update this give column, so something like:

    user.colName = colValue;  //where colName = 'firstName'
    

    instead of

    user.firstName = colValue;
    

    I have researched Reflection quite a bit but I'm not sure where to begin or if such a task is even possible.

    I could use a loop to retrieve like data like so:

    foreach(var x in User.GetType().GetProperties()) {
    if(x.ToString() == colName)
    {
    var y = db.User.Where(z => z.firstName == colValue)
    }}
    

    Any pointers, recommendations or suggestions would be greatly appreciated! Is this impossible?

    • Oren Hizkiya
      Oren Hizkiya over 9 years
      What it your greater goal and why did settle on this approach? In general entity framework is meant to abstract away the details of the database so you can work with objects. This includes column names.
    • OverMars
      OverMars over 9 years
      I'd like to update database columns one at a time, as they change on the front end. It would be nice if the front end could tell the api/EF the column to update with a given value. So I would have a controller: UpdateColumn(string colName, string colValue)... and it would decide what column needs to be updated based on the string name of the column passed in from the front end.
  • muru
    muru about 9 years
    +1, interesting, but: 1. ~/.inputrc might be preferable over /etc/inputrc, and 2. I think you can set this in bash directly: unix.stackexchange.com/q/55203/70524, unix.stackexchange.com/a/16926/70524
  • Erik
    Erik over 7 years
    This is quite nice, thanks (and have my upvote). Is there a universal way to show the options it'll loop through, which combines the best of both worlds? I like the tab+tab possibility with folders and subcommands, etc. so I don't have to remember them all (e.g. git branch <tab through branches>. However, if I could see a list and tab through its items, that would be great! For directories and files ls is an option to see what items are available. However, for subcommands it is not that easy or obvious, unfortunately.
  • np8
    np8 over 6 years
    This works also on PuTTy! Just had to restart it once :)
  • Tobias
    Tobias about 6 years
    IMHO: This is the right way. I personally find the windows behavior very annoying. Consider the case where you have a lot of files starting with a and you need the last one. When you accidentally press tab after a you have to cycle through the list of all possible completions to get to the right one.
  • Fabby
    Fabby almost 6 years
    @Erik: Apparently, what you asked for is possible but not going to edit my answer to keep things simple here. (deleted old comment that it is not possible)
  • Fabby
    Fabby almost 6 years
    @Tobias: when you accidentally hit [Tab] too early using the other system, there is still [Ctrl][K]... ;-)
  • sh.3.ll
    sh.3.ll about 4 years
    How to put this TAB markdown? @Fabby
  • Fabby
    Fabby about 4 years