Skip to content

Instantly share code, notes, and snippets.

@bhuvidya
Last active September 11, 2023 09:42
Show Gist options
  • Save bhuvidya/05da20d6bc79fdc3838ac3b8f7c34866 to your computer and use it in GitHub Desktop.
Save bhuvidya/05da20d6bc79fdc3838ac3b8f7c34866 to your computer and use it in GitHub Desktop.
Laravel artisan command auto-complete for BASH.

The inspiration for this comes from https://gist.github.com/jhoff/8fbe4116d74931751ecc9e8203dfb7c4

The following code gives you auto-complete for artisan commands in a BASH shell. Just add to your ~/.bash_profile. If you want a faster auto-complete, run art_cache from your project root to get a cached file of commands. To remove this file just run art_cache clear. If you haven't created a cache file, the script uses artisan list to get the command list dynamically (but this is a lot slower).

export ARTISAN_CMDS_FILE=bootstrap/cache/artisan-cmds.txt

function _artisan() {
    COMP_WORDBREAKS=${COMP_WORDBREAKS//:}

    if [ -f "$ARTISAN_CMDS_FILE" ]; then
        COMMANDS=$(cat "$ARTISAN_CMDS_FILE")
    else
        COMMANDS=$(php artisan --raw --no-ansi list | awk '{print $1}')
    fi

    COMPREPLY=(`compgen -W "$COMMANDS" -- "${COMP_WORDS[COMP_CWORD]}"`)

    return 0
}

function art_cache() {
    if [[ "$1" == "clear" ]]; then
        echo -n "Removing commands cache file..."
        rm -f "$ARTISAN_CMDS_FILE"
        echo "done."
    else
        php artisan --raw --no-ansi list | awk '{print $1}' > "$ARTISAN_CMDS_FILE"
        echo $(wc -l "$ARTISAN_CMDS_FILE" | awk '{print $1}')" artisan commands cached."
    fi
}

complete -o default -F _artisan art
complete -o default -F _artisan artisan

Et voila, you now have artisan auto-complete, without having to install an external package.

@vishu-3
Copy link

vishu-3 commented Mar 23, 2023

@bhuvidya @Daniil-Solovyev I was adding above code in .profile file which is inside user directory. I just get art_cache: command not found.I am using ubuntu 18.04 ,composer 2.1.6 & laravel 6.20.44 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment