You don’t have to type all your manage.py or django-admin commands by hand anymore! Here’s Django command auto complete hack

If you’re like me then you love the terminal and everything about it. One thing that make the terminal experiences smooth is the Tab button autocomplete. Writing recognizable amount of the command or it’s parameters and hitting Tab saves me a lot of ti…


This content originally appeared on DEV Community and was authored by Ratul Roy

If you're like me then you love the terminal and everything about it. One thing that make the terminal experiences smooth is the Tab button autocomplete. Writing recognizable amount of the command or it's parameters and hitting Tab saves me a lot of time. But in the case of django I had to spell out runserver or makemigrations every time I initiated a dev server.
Alt Text
So I've enable Django commands auto complete and here's how you can do it too.
open up your .bashrc file with your favorite text editor. You'll find it at ~/.bashrc. Then add the following at the end of your file

# Django autocomplete start
_django_completion()
{
    COMPREPLY=( $( COMP_WORDS="${COMP_WORDS[*]}" \
                   COMP_CWORD=$COMP_CWORD \
                   DJANGO_AUTO_COMPLETE=1 $1 ) )
}
complete -F _django_completion -o default manage.py django-admin

_python_django_completion()
{
    if [[ ${COMP_CWORD} -ge 2 ]]; then
        local PYTHON_EXE=${COMP_WORDS[0]##*/}
        if echo "$PYTHON_EXE" | grep -qE "python([3-9]\.[0-9])?"; then
            local PYTHON_SCRIPT=${COMP_WORDS[1]##*/}
            if echo "$PYTHON_SCRIPT" | grep -qE "manage\.py|django-admin"; then
                COMPREPLY=( $( COMP_WORDS=( "${COMP_WORDS[*]:1}" )
                               COMP_CWORD=$(( COMP_CWORD-1 ))
                               DJANGO_AUTO_COMPLETE=1 ${COMP_WORDS[*]} ) )
            fi
        fi
    fi
}

# Support for multiple interpreters.
unset pythons
if command -v whereis &>/dev/null; then
    python_interpreters=$(whereis python | cut -d " " -f 2-)
    for python in $python_interpreters; do
        [[ $python != *-config ]] && pythons="${pythons} ${python##*/}"
    done
    unset python_interpreters
    pythons=$(echo "$pythons" | tr " " "\n" | sort -u | tr "\n" " ")
else
    pythons=python
fi

complete -F _python_django_completion -o default $pythons
unset pythons

# Django autocomplete end

Save the file and you're all done! And yes it was THIS EASY! To work at your already opened terminal just type reset and enter. Or source ~/.bashrc would do the trick too!


This content originally appeared on DEV Community and was authored by Ratul Roy


Print Share Comment Cite Upload Translate Updates
APA

Ratul Roy | Sciencx (2021-06-26T04:49:35+00:00) You don’t have to type all your manage.py or django-admin commands by hand anymore! Here’s Django command auto complete hack. Retrieved from https://www.scien.cx/2021/06/26/you-dont-have-to-type-all-your-manage-py-or-django-admin-commands-by-hand-anymore-heres-django-command-auto-complete-hack/

MLA
" » You don’t have to type all your manage.py or django-admin commands by hand anymore! Here’s Django command auto complete hack." Ratul Roy | Sciencx - Saturday June 26, 2021, https://www.scien.cx/2021/06/26/you-dont-have-to-type-all-your-manage-py-or-django-admin-commands-by-hand-anymore-heres-django-command-auto-complete-hack/
HARVARD
Ratul Roy | Sciencx Saturday June 26, 2021 » You don’t have to type all your manage.py or django-admin commands by hand anymore! Here’s Django command auto complete hack., viewed ,<https://www.scien.cx/2021/06/26/you-dont-have-to-type-all-your-manage-py-or-django-admin-commands-by-hand-anymore-heres-django-command-auto-complete-hack/>
VANCOUVER
Ratul Roy | Sciencx - » You don’t have to type all your manage.py or django-admin commands by hand anymore! Here’s Django command auto complete hack. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/26/you-dont-have-to-type-all-your-manage-py-or-django-admin-commands-by-hand-anymore-heres-django-command-auto-complete-hack/
CHICAGO
" » You don’t have to type all your manage.py or django-admin commands by hand anymore! Here’s Django command auto complete hack." Ratul Roy | Sciencx - Accessed . https://www.scien.cx/2021/06/26/you-dont-have-to-type-all-your-manage-py-or-django-admin-commands-by-hand-anymore-heres-django-command-auto-complete-hack/
IEEE
" » You don’t have to type all your manage.py or django-admin commands by hand anymore! Here’s Django command auto complete hack." Ratul Roy | Sciencx [Online]. Available: https://www.scien.cx/2021/06/26/you-dont-have-to-type-all-your-manage-py-or-django-admin-commands-by-hand-anymore-heres-django-command-auto-complete-hack/. [Accessed: ]
rf:citation
» You don’t have to type all your manage.py or django-admin commands by hand anymore! Here’s Django command auto complete hack | Ratul Roy | Sciencx | https://www.scien.cx/2021/06/26/you-dont-have-to-type-all-your-manage-py-or-django-admin-commands-by-hand-anymore-heres-django-command-auto-complete-hack/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.