Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto complete by end/middle of line in bash

Tags:

bash

In bash is there a quick way to do tab auto-completion based on the middle of a word.

So for example, if I have these files in a directory:

001_apple.txt 002_pear.txt 003_dog.txt 

I would like to type the sequence: *d<TAB> to auto-complete 003_dog.txt.

Can this be done in bash? Is it easier to do in other shells?

like image 268
Sam Saffron Avatar asked Dec 30 '08 00:12

Sam Saffron


People also ask

How do you autocomplete in bash?

The programmable completion feature in Bash permits typing a partial command, then pressing the [Tab] key to auto-complete the command sequence. [1] If multiple completions are possible, then [Tab] lists them all.

How do you continue a line in bash?

From the bash manual: The backslash character '\' may be used to remove any special meaning for the next character read and for line continuation.

How do you run a new line in a shell script?

If you don't want to use echo repeatedly to create new lines in your shell script, then you can use the \n character. The \n is a newline character for Unix-based systems; it helps to push the commands that come after it onto a new line.

What key when pressed will auto complete the command being typed in the terminal?

Use Tab to autocomplete the names of directories and files while in the command line.


3 Answers

Try ESC-g for glob expansion.

And you should always install the bash-completion package (included by default often, but you need to source it in your bash profile script).

like image 173
PEZ Avatar answered Oct 19 '22 02:10

PEZ


Looks like zsh does this plus quite a bit more. See: expand-or-complete-prefix and COMPLETE_IN_WORD options.

Fish also does this really nicely out-of-the-box.

like image 23
Sam Saffron Avatar answered Oct 19 '22 02:10

Sam Saffron


ls *d*<TAB>

works in bash. Not sure if that's what Ben meant. ls could of course be any other command.

like image 40
codelogic Avatar answered Oct 19 '22 03:10

codelogic