Coder Social home page Coder Social logo

iridakos / bash-completion-tutorial Goto Github PK

View Code? Open in Web Editor NEW
138.0 138.0 20.0 6 KB

Code of the bash completion tutorial

Home Page: https://iridakos.com/tutorials/2018/03/01/bash-programmable-completion-tutorial

License: MIT License

Shell 100.00%
bash bash-completion programmable-completion tutorial

bash-completion-tutorial's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

bash-completion-tutorial's Issues

not work with menu-complete

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!

Word splitting taking place

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 ๐Ÿ‘

ezgif-4-dcfe42fdea

how to adapt it to work with multiple arguments?

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.

Just to say 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

Universal install of bash completion

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.

Fuzzy match

Hello, if I could autocomplete the param using fuzzy match?

params
hello
world

$ comp_test ld<tab><tab>
world

Need IFS=$'\n' earlier in the tutorial

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".

sed command not replacing/removing tabs

((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:

  • Bash '' strings do not do C-style escape sequences
    • (neither to "" strings in Bash)
    • only $'' bash strings process C-style escape sequences
      • (recall that echo -e "a\tb" TAB expansion is done inside the echo builtin code)
  • so sed sees '\t' in ARGV
    • which it interprets as:
      1. an unnecessary '\' character, then
      2. a plain 't' character
  • Your sed removes what it matches, it does not replace
  • Plain fc output already has 1 TAB and 1 SPACE between the num & command
  • ...So it's really not clear if you want 1 space or 2 in the end result

Full 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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.