Skip to content

Instantly share code, notes, and snippets.

@bhuvidya
Last active September 11, 2023 09:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • 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.

@Daniil-Solovyev
Copy link

I don't quite understand how to use this. I put this code in bashrc but none of these functions work.

@bhuvidya
Copy link
Author

@Daniil-Solovyev in the root folder of a Laravel project, run:

$ art_cache

and this will stash a cache of artisan commands in bootstrap/cache/artisan-cmds.txt - then when you type artisan in the project root folder, you can use the TAB key to get auto-completion in the usual bash way.

@Daniil-Solovyev
Copy link

Daniil-Solovyev commented Aug 24, 2022

@bhuvidya I just get art_cache: command not found

@bhuvidya
Copy link
Author

@Daniil-Solovyev did you re-run (source) your bashrc? or open a new terminal? if my code is in your bashrc, then art_cache will be defined as a function

@Daniil-Solovyev
Copy link

@bhuvidya Oh, I just logged in as a different user. Thanks, your code works great!

@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