How to Enable Tab-Completion of Command Line Switches in Bash

How to enable tab-completion of command line switches in bash?

Take a look at Extended Bash Completion

Auto-complete command line arguments

This is an example of BASH's smart completion. A basic description is here, a guide to writing your own extensions is here and another (Debian-based) guide is here. And here's a fuller featured introduction to the complete command (the command that facilitates this behaviour).

How to enable tab completion from the terminal specific to the executable

This is done with scripts in /etc/bash_completion.d/ and if you want to write your own completion support for an executable, here's a tutorial to get you started.

If you only need to get the behaviour working for common executables, your Linux distro probably has a bash-completion package available with support for common commands.

How to get tab completion for $PATH as an argument in a bash script?

I got tab completion working using GNU's complete command. I created the file /etc/bash_completion.d/benchmark-app and added this line: complete -F _command benchmark-app.

How can I make bash tab completion behave like vim tab completion and cycle through matching matches?

By default TAB is bound to the complete readline command. Your desired behavior would be menu-complete instead. You can change your readlines settings by editing ~/.inputrc. To rebind TAB, add this line:

TAB: menu-complete

For more details see the READLINE section in man bash.

Bash history expansion with tab completion

The conceptual terms you're looking for is [command] history and history expansion - if you run man bash, you'll find sections HISTORY and HISTORY EXPANSION devoted to this topic.

That said, in this particular case, it is not history expansion, but the special shell variable $_ that is your friend:

It expands to the last (expanded) argument of the most recent command.

Try the following, which mimics your scenario:

ls "$HOME"  

# Type this on the command line and press TAB (possibly twice)
# _before_ submitting to TAB-complete to matching files in "$HOME"
# (irrespective of what the current directory is).
# $_ at this point contains whatever "$HOME" expanded to, e.g. "/Users/jdoe".
cp $_/

Note: Whether tab-completion works for a given command is unrelated to whether $_ is used or not. See man bash, section Programmable Completion, for how to manually enable tab-completion for commands of interest.

Customize tab completion in shell

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.



Related Topics



Leave a reply



Submit