7

Instead of displaying all possibilities on terminal screen, as in:

$ ls /etc/<TAB>
Display all 230 possibilities? (y or n)

I want to save all possibilities to a file.

ls /etc/ > file.txt will not always work. apt-get is an example:

$ apt-get <TAB>
autoclean        check            install          update
autoremove       clean            purge            upgrade
build-dep        dist-upgrade     remove           
changelog        dselect-upgrade  source           

I'm looking for a command like tabcompletions 'ls /etc/' which outputs all possibilities, so that I can run a command like the one below, which compares the tab completion possibilities for two commands:

diff <(tabcompletions 'ls ') <(tabcompletions 'cd ')

Is that possible?

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
musa
  • 1,018
  • 2
  • 10
  • 18
  • 2
    Also see http://unix.stackexchange.com/questions/23900/how-to-pipe-the-list-of-commands-displayed-by-of-tab-complete – cwd Dec 31 '12 at 02:50

3 Answers3

5

In your ~/.bashrc you probably have something like this:

if [ -f /etc/bash_completion ] && ! shopt -oq posix
then
    source /etc/bash_completion
fi

Now, that's where to continue looking, and the header of _quote_readline_by_ref contains the necessary hint:

compgen -f /etc/

Tracing this back, it turns out that (via type compgen) compgen is a "shell builtin", which means it should appear in man bash:

compgen [option] [word]
       Generate possible completion matches for word according to the options ...
l0b0
  • 50,672
  • 41
  • 197
  • 360
  • An example using a completion function, as `_apt_get` would be interesting. – enzotib Sep 27 '11 at 15:59
  • @enzotib `compgen -F _apt_get`, except that it's not so simple: you would need to set a bunch of variables to emulate the real completion system. Zsh is even worse, the completion primitives (`compadd`, `compset`) only work if called from a completion widget, which in turns only works from the line editor, not from a script or even from a command line. – Gilles 'SO- stop being evil' Sep 27 '11 at 22:23
4

Although, a crude method, you could use the command script

$ script -a lsdiff
Script started, file is lsdiff
$ ls <TAB>
a b c ...
$ <Ctrl-D>
Script done, file is lsdiff

Repeat the above for cd and compare the difference.

Sudipta Chatterjee
  • 599
  • 1
  • 5
  • 9
0

For the ls /etc/<TAB> auto-completion case, do this:

# print to screen
compgen -f /etc/ | sort
# store to file
compgen -f /etc/ | sort > output.txt
# print to screen *and* store to file
compgen -f /etc/ | sort | tee output.txt

See help compgen for more info. compgen apparently stands for "completion generator".

See also man bash and search for compgen, since it is a built-in executable built in to bash. man bash has a lot more info about the compgen command.

Ex: compgen -A command <cmd> is the same as compgen -c <cmd>, and compgen -A file is the same as compgen -f. Search man bash for -A action.

See also:

  1. List all binaries from $PATH
  2. How to pipe the list of commands displayed by "tab complete"?
Gabriel Staples
  • 2,192
  • 1
  • 24
  • 31