Customize tab completion in shell
Asked Answered
C

3

17

This may be have a better name than "custom tab completion", but here's the scenario:

Typically when I'm at the command line and I enter a command, followed with {TAB} twice, I get a list of all files and subdirectories in the current directory. For example:

[user@host tmp]$ cat <TAB><TAB>
chromatron2.exe                  Fedora-16-i686-Live-Desktop.iso  isolate.py
favicon.ico                      foo.exe                          James_Gosling_Interview.mp3

However, I noticed at least one program somehow filters this list: wine. Consider:

[user@host tmp]$ wine <TAB><TAB>
chromatron2.exe  foo.exe

It effectively filters the results to *.exe.

Thinking it might be some sort of wrapper script responsible for the filtering, a did a which and file an it turns out wine is not a script but an executable.

Now, I don't know whether this "filter" is somehow encoded in the program itself, or otherwise specified during the default wine install, so I'm not sure whether this question is more appropriate for stackoverflow or superuser, so I'm crossing my fingers and throwing it here. I apologize if I guessed wrong. (Also, I checked a few similar questions, but most were irrelevant or involved editing the shell configuration.)

So my question is, how is this "filtering" accomplished? Thanks in advance.

Clintonclintonia answered 8/6, 2012 at 4:2 Comment(2)
tab completion is shell specific, which shell are you using (echo $SHELL)Solitary
Sorry, I'm using bash. Edit: 4.2.24Clintonclintonia
L
14

You will likely find a file on your system called /etc/bash_completion which is full of functions and complete commands that set up this behavior. The file will be sourced by one of your shell startup files such as ~/.bashrc.

There may also be a directory called /etc/bash_completion.d which contains individual files with more completion functions. These files are sourced by /etc/bash_completion.

This is what the wine completion command looks like from the /etc/bash_completion on my system:

complete -f -X '!*.@(exe|EXE|com|COM|scr|SCR|exe.so)' wine

This set of files is in large part maintained by the Bash Completion Project.

Lemal answered 8/6, 2012 at 11:12 Comment(1)
I do indeed have these files as well as the line you mention. Thanks for the comprehensive answer :-)Clintonclintonia
E
6

You can take a look at Programmable Completion in bash manual to understand how it works.

Essentiality answered 8/6, 2012 at 5:9 Comment(1)
Do make sure you make it compatible for ZSH users! This guide is straightforward and makes it easy to understand how completion is usually implemented: linux-mag.com/id/1106Azote
C
6

I know this is old but I was looking to do something similar with a script of my own.

You can play around with an example I made here: http://runnable.com/Uug-FAUPXc4hAADF/autocomplete-for-bash

Pasted code from above:

# Create function that will run when a certain phrase is typed in terminal
# and tab key is pressed twice
_math_complete()
{
    # fill local variable with a list of completions
    local COMPLETES="add sub mult div"
    # you can fill this variable however you want. example:
    # ./genMathArgs.sh > ./mathArgsList
    # local COMPLETES=`cat ./mathArgsList`

    # we put the completions into $COMPREPLY using compgen
    COMPREPLY=( $(compgen -W "$COMPLETES" -- ${COMP_WORDS[COMP_CWORD]}) )
    return 0
}

# get completions for command 'math' from function '_math_complete()'
complete -F _math_complete math 

# print instructions
echo ""
echo "To test auto complete do the following:"
echo "Type math then press tab twice." 
echo "You will see the list we created in COMPLETES"
echo ""
Carrier answered 31/1, 2014 at 9:30 Comment(1)
Thanks for that nice lucid example. I like the idea of using a fictional command to demonstrate the concept. I've used it to develop and auto-complete for zlogin.Traynor

© 2022 - 2024 — McMap. All rights reserved.