Find Based Filename Autocomplete in Bash Script

find based filename autocomplete in Bash script

You should take a look at this introduction to bash completion. Briefly, bash has a system for configuring and extending tab completion. Other shells do this, too, and each one has a different way to set it up. Using this system it is not necessary to do everything yourself and adding custom argument completion to a command is relatively easy.

Autocomplete file name in a bash script

I tend to think there is no a way to get this from completion engine since it’s not a part of GNU Bash but Readline. But at least we can get list of possible completions with compgen. And an inmplementaion of finding longest common prefix should not be problem. So...

#!/bin/bash

SCRIPTNAME="${0##*/}"
USAGE="Usage: $SCRIPTNAME <prefix>

Print common prefix of possible file name completions. Like <TAB> but to
stdout."

(( $# == 1 )) || { printf >&2 '%s\n' "$USAGE"; exit 1; }

PREFIX="$1"

commonprefix() {
(( $# >= 2 )) || {
echo "$1"
return 0
}
local -i i N M
for ((i=0; i<=${#1}; i++)); do
for ((N=1; N<=$#-1; N++)); do
let M=$N+1
[[ ${!N:i:1} == ${!M:i:1} ]] || break 2
done
done
echo "${1:0:i}"
}

readarray -t COMPLETIONS < <(compgen -f "$PREFIX")
commonprefix "${COMPLETIONS[@]}"

How to configure bash autocomplete for file search and custom search?

You can use complete's -o default option (Usually I'd use both -o default and -o bashdefault):

complete -o default -F _test_complete remote

According to man bash:

  • bashdefault

    Perform the rest of the default bash completions if the compspec generates no matches.

  • default

    Use readline's default filename completion if the compspec generates no matches.

bash autocompletion with file names

I found this to work as needed:

COMPREPLY=( $(compgen -W "$(ls /var/log/app/)" -- $cur) )

Thanks to dogbane in https://unix.stackexchange.com/questions/28283/autocomplete-of-filename-in-directory !

autocomplete in bash script

Actually you can borrow quite a bit of code from geirha's answer:

# this is a custom function that provides matches for the bash autocompletion
_repo_complete() {
local file
# iterate all files in a directory that start with our search string
for file in ~/Desktop/_REPOS/"$2"*; do
# If the glob doesn't match, we'll get the glob itself, so make sure
# we have an existing file. This check also skips entries
# that are not a directory
[[ -d $file ]] || continue

# add the file without the ~/Desktop/_REPOS/ prefix to the list of
# autocomplete suggestions
COMPREPLY+=( $(basename "$file") )
done
}

# this line registers our custom autocompletion function to be invoked
# when completing arguments to the repo command
complete -F _repo_complete repo

The for loop iterates all files in a directory that start with the string given as second argument to the _repo_complete function (this is the string to be autocompleted).

Add the code to your .bashrc and it should work!

Get autocompletion when invoking a read inside a Bash script

Try:

read -e -p "Glassfish Path:" GF_DIR

-e enables readline:

 -e 
If the standard input is coming from a terminal, Readline is used
to obtain the line.

How can I get auto-completion in bash for file-names with spaces, too?

You know, I'd swear this used to work and just seemed to stop working recently (last few months). Don't know that any OS X updates updated bash, but whatever.

Anyway, putting "complete -o nospace -d cd" into .bashrc (or whatever rc file you're using) seems to give the desired behavior.

Is there an autocomplete feature for folder and file names in bash?

Yes, Bash supports auto-completion (personally, it's one of my favorite features). Use the Tab key to complete what you've typed (note that it's case-sensitive). The Advanced Bash-Scripting Guide has a section on an introduction to programmable completion. You can enable completion to complete command names and more!

Bash Tab Completion of Filenames after Arguments

After googling (and help from friends) i've found the solution!

To fix filename auto completion add the following to your ~/.bashrc file:

complete -D -o default

Reference: Bash completion for path in argument (with equals sign present)



Related Topics



Leave a reply



Submit