Where to put bash completion scripts
Asked Answered
N

3

6

I have implemented some perl scripts and in some cases i have to pass some command line args into my program.Usability perspective i have decided to add some bash completion script into my project.But now i am stuck on where to put my script to work it as expected.Please let me know where can i put my bash completion script

Custom Perl Scripts location

C:\scripts\tool\bin\ktools.pl

also add bin path to the system variable.

I have put my auto complete scripts in

C:\scripts\tool\bin\etc\bash_completion.d\ktools\foo

Bash Completion Script

_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 ktools

Command should be auto suggested when user type ktools -- + press tab in command line or git bash.

Nesta answered 15/7, 2017 at 6:34 Comment(2)
metacpan.org/release/MooseX-App-Plugin-ZshCompletionMaharashtra
So you are using Windows? Standard locations may differ in various platforms.Debose
I
6

I'm pretty sure you need to source it into your .bashrc (basically include it).

Open ~/.bashrc and write (you might need to create it if it doesn't exists)

source your_bash_file

Then, in your terminal, "refresh" by doing

source ~/.bashrc

and it should work.

Imalda answered 15/7, 2017 at 7:13 Comment(2)
where can i find .bashrc file i searched it in my userhome but there is anyNesta
in case if you cant create .bashrc in windows #6884260Nesta
A
1

The above answer is very inconvenient especially if you have multiple completion scripts and are having other people install and use your program.

The more convenient way to do it would be to create a folder containing all your completion scripts in /etc or such, and then in the .bashrc source everything in that directory.

Amerind answered 1/4, 2019 at 3:23 Comment(0)
H
0

You are on Windows an you use Bash, you should tell us more about that setup before you get sub-optimal advice.

Here is what I came up for Git-Bash on Windows after reading this answer: https://askubuntu.com/a/1188315/40581

Adding bash-completion to Git-Bash on Windows

You should add your custom completions to $HOME/.local/share/bash-completion/completions after you installed bash-completion. Here is how you do that:

  1. Download latest release from: https://github.com/scop/bash-completion/releases

  2. Open your git-bash and excute the following commands:

    # Ensure the folder structure exists.
    mkdir -pv "$HOME/.local/share/bash-completion/"
    
    # Extract the downloaded file to the target directory.
    # Adjust the path if you downloaded it to some other location.
    tar -xvf "$HOME/Downloads/bash-completion-"[.0-9a-z]*".tar.xz" \
     -C "$HOME/.local/share/bash-completion/" \
     --strip-components=1 \
     --exclude=test
    
    # Install the hook usually managed by pkg-config.
    sed -e '{
      s|@PACKAGE@|bash-completion|g;
      s|@datadir@|$HOME\/.local\/share|g;
      }' \
      "$HOME/.local/share/bash-completion/bash_completion.sh.in" \
      > "$HOME/.local/share/bash-completion/bash_completion.sh"
    
    # Setup .bashrc. You can change it to .bash_profile if you prefer that.
    if [[ -e $HOME/.bashrc ]]; then
      echo '#!/bin/bash' > "$HOME/.bashrc"
    fi
    
    echo -e '\n. "$HOME/.local/share/bash-completion/bash_completion.sh"' \
      >> "$HOME/.bashrc"
    

Now you have the base components and the folder ready, your completions will be read the next time you open a new shell.

I worked on this today and some additional scripts to extract completions from go binaries (like kubectl) to keep them updated and also to have a few more completers from the package working with ".exe" extensions in Git-Bash, but I'd prefer to publish these in a repository where code is easier to maintain.

Heartrending answered 1/12, 2022 at 0:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.