14

When using commands in bash I like the double-Tab option to display the available commands. Some commands have more possible matches than others. Example: here is just some of the output after typing git and pressing Tab Tab:

$ git
git                       git-effort                git-quick-stats
git-alias                 git-extras                git-reauthor
git-archive-file          git-feature               git-rebase-patch
git-authors               git-filechange-search     git-receive-pack
git-back                  git-force-clone           git-refactor
git-blametool             git-fork                  git-release
git-branch_               git-fresh-branch          git-rename-branch
git-branchz               git-graft                 git-rename-tag
git-bug                   git-gs_blametool          git-repl
git-bulk                  git-gs_branch_            git-reset-file
git-changelog             git-gs_changes            git-root
git-changes               git-gs_diffc              git-rscp
git-chore                 git-guilt                 git-scp
git-clang-format          git-ignore                git-sed
git-clang-format-6.0      git-ignore-io             git-setup
git-clear                 git-info                  git-shell
git-clear-soft            gitk                      git-show-merged-branches
git_commit_r              git-lfs                   git-show-tree
git-commits-since         git-line-summary          git-show-unmerged-branches
git-contrib               git-local-commits         git-squash
git-count                 git-lock                  git-stamp

Is there a way I can pipe the output of the double-Tab to somewhere, like to grep? I found a related post (How does TAB auto-complete find options to complete?), but I'm still not sure how to implement piping the output to grep.

Gabriel Staples
  • 2,192
  • 1
  • 24
  • 31
cwd
  • 44,479
  • 71
  • 146
  • 167
  • How exactly do you imagine you would enter the `grep` regex? `(y or n or g[rep])`? I hate to sound pessimistic, but I doubt this could be done without adding it to the bash code. – Kevin Nov 03 '11 at 17:41
  • [Related question](http://unix.stackexchange.com/questions/25935/how-to-output-string-completions-to-stdout) – l0b0 Feb 14 '12 at 14:10

3 Answers3

23

For commands use compgen -c. The word compgen apparently stands for "completion generator". From help compgen (emphasis added):

compgen: compgen [-abcdefgjksuv] [-o option] [-A action] [-G globpat] [-W wordlist] [-F function] [-C command] [-X filterpat] [-P prefix] [-S suffix] [word]

Display possible completions depending on the options.

Intended to be used from within a shell function generating possible completions. If the optional WORD argument is supplied, matches against WORD are generated.

Exit Status: Returns success unless an invalid option is supplied or an error occurs.

For extra details, search the man bash pages for compgen, since it's a built-in bash shell command.

Example usage:

$ compgen -c bas
basename
base64
bashbug
bash

This output you can simply pipe to grep. Example:

$ compgen -c bas | grep bug
bashbug
Gabriel Staples
  • 2,192
  • 1
  • 24
  • 31
salutis
  • 246
  • 1
  • 2
3

Based on @salutis's answer I created a script which I called comp and stored in ~/bin/comp that searches commands', aliases, and builtins, (option flag -cab see the bash man entry), with an optional second parameter which, if present, pipes the output to grep and searches for the second parameter.

Usage: comp string [keyword-for-grep]

Code:

#!/bin/bash

if [ -z "$1" ]; then
    echo usage: comp string [keyword-for-grep]
    echo 
    exit
fi

if [ -z "$2" ]; then
    compgen -cab -- $1
    exit
fi

compgen -cab -- $1 | grep -i $2

Personally I would also be interested in figuring out a way to remove the last command from the shell history in the script (something related to history -d) so that when searching bash history I won't find comp entries. I know I can also do this with HISTIGNORE but linux is pretty powerful so there must be a way to do it from the script file, too - right?

mark
  • 711
  • 3
  • 8
-3

Why not find what you are looking for?

Example:

find / -name ec2* -executable -type f -perm -og+rx -print

Knowing your use case I can create a more detailed command example.

Kevin
  • 40,087
  • 16
  • 88
  • 112
Matt
  • 3
  • 1