Search code examples
djangobashzshoh-my-zsh

Is it possible to use Django bash completion in zsh


Django bash completion enables tab-completion of django-admin.py and manage.py commands in bash.

There are autocompletion scripts for zsh for django but they does not work with custom commands. One of them is in oh-my-zsh project.

I am also aware of bashcompinit but it does not seem to work with django-admin, producing following error:

./manage.py Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "lib/python2.7/site-packages/django/core/management/__init__.py", line 356, in execute
    self.autocomplete()
  File "lib/python2.7/site-packages/django/core/management/__init__.py", line 266, in autocomplete
    cwords = os.environ['COMP_WORDS'].split()[1:]
  File "bin/../lib/python2.7/UserDict.py", line 23, in __getitem__
    raise KeyError(key)
KeyError: 'COMP_WORDS'

Is it possible to have Django bash completion working with zsh? Or is there some other alternative that would allow using of django admin custom commands.


Solution

  • You could patch django_bash_completion something like below:

    --- django_bash_completion.old  2014-12-23 10:41:35.589103686 +0900
    +++ django_bash_completion  2014-12-23 10:43:27.224848105 +0900
    @@ -33,7 +33,7 @@
    
     _django_completion()
     {
    -    COMPREPLY=( $( COMP_WORDS="${COMP_WORDS[*]}" \
    +    COMPREPLY=( $( env COMP_WORDS="${COMP_WORDS[*]}" \
                        COMP_CWORD=$COMP_CWORD \
                        DJANGO_AUTO_COMPLETE=1 $1 ) )
     }
    

    On zsh (zsh-5.0.7 here), it works fine with explicitly using env(1). Above patch does not hurt bash (I've tested on bash-4.3.30(1)-release).