5

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.

powlo
  • 2,538
  • 3
  • 28
  • 38
bmihelac
  • 6,233
  • 34
  • 45

1 Answers1

3

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).

hchbaw
  • 4,894
  • 18
  • 16
  • It seems that `emulate` with subshell processing confuse zsh. For example: `() { A=(a b c); emulate -L sh; (A="${A[*]}" printenv A) }` prints nothing, and it causes `KeyError: 'COMP_WORDS'` in this particular case. But `() { A=(a b c); emulate -L zsh; (A="${A[*]}" printenv A) }` prints 'a b c'. Because of this behavior on zsh, I use 'env(1)` to propely export `COMP_WORDS` on my answer. – hchbaw Dec 23 '14 at 06:03
  • 1
    This zsh bug has been addressed recently, so we don't need this work around for the upcoming new zsh releases (maybe 5.0.8?). – hchbaw Dec 29 '14 at 13:29