iridakos / bash-completion-tutorial Goto Github PK
View Code? Open in Web Editor NEWCode of the bash completion tutorial
Home Page: https://iridakos.com/tutorials/2018/03/01/bash-programmable-completion-tutorial
License: MIT License
Code of the bash completion tutorial
Home Page: https://iridakos.com/tutorials/2018/03/01/bash-programmable-completion-tutorial
License: MIT License
My setting in ~/.inputrc
set show-all-if-ambiguous on
#cycle through choices with ctrl-right/left
"\e[1;5C": menu-complete
"\e[1;5D": menu-complete-backward
When triggering menu-complete
(see man bash
) first word takes place (in not wanted format) immediately at the same time with showing suggestions
The problem with this implementation is that we get:
dothis <number> command_name <padding>
<cursor>
What could we do to improve this behaviour? Thank you in advance!
Your code relies on word splitting suggestions=( $(compgen ... ) )
and the completion gives me wrong results before you set IFS=$'\n'
. When you tested your function, you might have had IFS
set from some previous attempts (since compgen
also splits using IFS
, I believe that really is the case). It's also subject to pathname expansion which is not that important here but still might be an issue in some extreme cases. set -f
might be a good idea.
I believe that creating a completion that correctly handles whitespace characters and globs is extremely painful. I may have missed something of course and I might embarrass myself :) Overall it's a great article giving insight in some less known parts of bash
๐
Ok, I read about it a bit now and I understand: https://unix.stackexchange.com/questions/11376/what-does-double-dash-mean-also-known-as-bare-double-dash
It's there to signify we've finished listing the options and will now only add the prefix of the completion.
I don't know if you'd want to add a comment about it in the text.
Anyway Great Tutorial!
I'm creating a script that's supposed to manage a few files in the system. list them, create them, delete them.
I followed your bash completion guide but it does not mention working with arguments.
example: one of the commands of my script would be script remove <name>
. name would be a file in a directory.
but other arguments would use the same completion, like script disable <name>
and script enable <name>
how could I make it so that it works with multiple arguments?
thank you.
No way to tell you how grateful I am to you for this well-prepared tutorial, neither on GitHub nor on your blog site. So there is this issue: thank you. Feel free to close with no response :-)
Filippo
I've been searching for a way to reliably install bash completions cross-platform. While many places do have a /etc/bash_completion.d directory, some have it mapped to strange places. My current solution is as follows in a makefile:
# Attempt to find bash completion dir in order of preference
ifneq ($(wildcard /etc/bash_completion.d/.),)
CPLDIR ?= /etc/bash_completion.d
endif
# Check if brew in installed (OSX) and use brew prefix
HAS_BREW := $(shell command -v brew 2> /dev/null)
ifdef HAS_BREW
CPLDIR ?= $$(brew --prefix)/etc/bash_completion.d
endif
# Last resort, check pkg-config for custom bash completion path
HAS_PKGCONFIG := $(shell command -v pkg-config 2> /dev/null)
ifdef HAS_PKGCONFIG
CPLDIR ?= $$(pkg-config --variable=completionsdir bash-completion 2> /dev/null)
endif
and later in the recipe:
install:
...
ifdef CPLDIR
echo Installing the command completion to $(CPLDIR)
@mkdir -p $(CPLDIR)
cp -f program.d $(CPLDIR)/program
chmod 644 $(CPLDIR)/program
endif
Have you seen any more thorough solutions? I still feel like this is a hack.
Hello, if I could autocomplete the param using fuzzy match?
params
hello
world
$ comp_test ld<tab><tab>
world
When working through the tutorial, once you begin using fc -l... in the completion script. You neglect to add the IFS=$'\n' to the script. This results in each command and number being broken apart into multiple unrelated "arguments".
((Thanks, https://iridakos.com/programming/2018/03/01/bash-programmable-completion-tutorial Is super-accessible, and very helpful!))
From this place in your doc
replace tabs with spaces ...
sed to the rescue.
... fc -l -50 | sed 's/\t//' ...
Actually removes the first plain "t" character!
Instead, to replace 1 tab character with 1 space,
the sed command you want is:
$ fc -l -50 | sed $'s/\t/ /'
Notes:
''
strings do not do C-style escape sequences
""
strings in Bash)$''
bash strings process C-style escape sequences
echo -e "a\tb"
TAB expansion is done inside the echo builtin code)sed
sees '\t' in ARGV
'\'
character, then't'
characterfc
output already has 1 TAB and 1 SPACE between the num & commandFull log:
## Raw `fc` output:
## (using `... | cat -vet` to show TAB chars as "^I")
$ fc -l -5 | cat -vet
57^I . ~/ds/env/complete-dothis.bash $
58^I . ~/ds/env/complete-dothis.bash $
59^I _comp_debug_dothis=1$
60^I . ~/ds/env/complete-dothis.bash $
61^I dothis fc -l -5 | sed $'s/\t/ /'$
## Your current sed command:
## (note "complee", not complete")
$ fc -l -5 | sed 's/\t//' | cat -vet
58^I . ~/ds/env/complee-dothis.bash $
59^I _comp_debug_dohis=1$
60^I . ~/ds/env/complee-dothis.bash $
61^I dohis fc -l -5 | sed $'s/\t/ /'$
62^I fc -l -5 | ca -vet$
## Fixed sed command:
$ fc -l -5 | sed $'s/\t/ /' | cat -vet
59 _comp_debug_dothis=1$
60 . ~/ds/env/complete-dothis.bash $
61 dothis fc -l -5 | sed $'s/\t/ /'$
62 fc -l -5 | cat -vet$
63 fc -l -5 | sed 's/\t//' | cat -vet$
[12:43:57 Sun Feb 14] X:0 !65
$ echo "${BASH_VERSINFO[@]}"
3 2 57 1 release x86_64-apple-darwin18
# Mac 10.14.6
# ...but same behavior w/ Bash 5 (from HomeBrew)
5 0 18 1 release x86_64-apple-darwin18.7.0
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.