Comments (3)
Never done a bash completion before so I am going to borrow from the pass project and just rewrite the internals.
Apparently we need a script in the folder
/usr/share/bash-completion/completions
It would appear that all you need to do is supply a bash script that supplies a number of functions
from unix-notes.
You'll have to create a new file:
/etc/bash_completion.d/foo
For a static autocompletion (--help / --verbose for instance) add this:
_foo()
{
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
opts="--help --verbose --version"
if [[ ${cur} == -* ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
fi
}
complete -F _foo foo
- COMP_WORDS is an array containing all individual words in the current command line.
- COMP_CWORD is an index of the word containing the current cursor position.
- COMPREPLY is an array variable from which Bash reads the possible completions.
And the compgen command returns the array of elements from --help, --verbose and --version matching the current word "${cur}":
compgen -W "--help --verbose --version" -- "<userinput>"
from unix-notes.
Here is a complete tutorial.
Let's have an example of script called admin.sh to which you would like to have autocomplete working.
#!/bin/bash
while [ $# -gt 0 ]; do
arg=$1
case $arg in
option_1)
# do_option_1
;;
option_2)
# do_option_2
;;
shortlist)
echo option_1 option_2 shortlist
;;
*)
echo Wrong option
;;
esac
shift
done
Note the option shortlist. Calling the script with this option will print out all possible options for this script.
And here you have the autocomplete script:
_script()
{
_script_commands=$(/path/to/your/script.sh shortlist)
local cur
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
COMPREPLY=( $(compgen -W "${_script_commands}" -- ${cur}) )
return 0
}
complete -o nospace -F _script ./admin.sh
Note that the last argument to complete is the name of the script you want to add autocompletion to. All you need to do is to add your autocomplete script to bashrc as
source /path/to/your/autocomplete.sh
or copy it to /etc/bash.completion.d
from unix-notes.
Related Issues (19)
- ROADMAP - Git integration
- ROADMAP - Package for Debian HOT 1
- ROADMAP - Create a tarball with SHA for easy installation on packaging systems not yet supported
- [FEATURE REQUEST] - Add $XDG_DATA_HOME environment support
- [FEATURE REQUEST] - Add support for env $NOTESDIR
- [BUG] notes import truncates name if filename has a space in it HOT 1
- (BW) [FEATURE REQUEST] - PIV card support HOT 1
- ROADMAP - 'notes search' function
- [BUG] Spaces in notes filenames HOT 2
- [FEATURE REQUEST] - tree subcommand HOT 1
- [FEATURE REQUEST] - Backup command
- [FEATURE REQUEST] - SHRED temporary files HOT 1
- Initial features need for release HOT 1
- [FEATURE REQUEST] - Switch to ASCII encoded encryption HOT 1
- [FEATURE REQUEST] - split up markdown documentation HOT 1
- [BUG] existing notes cant be edited - wrong extension use .asc
- ROADMAP - Dedicated Diary/Journal function HOT 3
- ROADMAP - spellcheck before encryption HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from unix-notes.