Vim、Python 和 Django 自动完成(pysmell?)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/978643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Vim, Python, and Django autocompletion (pysmell?)
提问by So8res
Does anyone know how to set up auto completion to work nicely with python, django, and vim?
有谁知道如何设置自动完成以与 python、django 和 vim 很好地协同工作?
I've been trying to use pysmell, but I can't seem to get it set up correctly (or maybe I don't know how it works). Right now, I run pysmell in the django directory (I'm using the trunk) and move the resulting tags to my project directory, then I also run pysmell in the project directory. Vim doesn't pick up the django tags, though, and they don't get auto completed.
我一直在尝试使用 pysmell,但我似乎无法正确设置它(或者我可能不知道它是如何工作的)。现在,我在 django 目录中运行 pysmell(我正在使用主干)并将生成的标签移动到我的项目目录中,然后我也在项目目录中运行 pysmell。但是,Vim 不会选择 django 标签,并且它们不会自动完成。
Does anyone know how to set up auto completion in vim so that it will complete the long django functions (like get_object_or_404) as well as classes/functions in my own code? I have poked around on google but haven't found any good resources.
有谁知道如何在 vim 中设置自动完成功能,以便在我自己的代码中完成长 django 函数(如 get_object_or_404)以及类/函数?我已经在 google 上四处闲逛,但没有找到任何好的资源。
Thanks.
谢谢。
采纳答案by bchang
First off, thank you for asking this question, as it forced me to figure this out myself and it's great!
首先,感谢您提出这个问题,因为它迫使我自己解决这个问题,这很棒!
Here is the page I used as a reference: PySmell v0.6 released : orestis.gr
这是我用作参考的页面:PySmell v0.6 发布:orestis.gr
- Install PySmell using the
setup.py install
command. - Generate the
PYSMELLTAGS
file for django by going to yoursite-packages/django
directory and running:pysmell . -o ~/PYSMELLTAGS.django
- Copy that file to your project directory, and then ran
pysmell .
to generate the project PYSMELLTAGS file - Make sure pysmell is in your
PYTHONPATH
(export PYTHONPATH=${PYTHONPATH}:/path/to/pysmell/
) - Run vim (
vim .
) - Source
pysmell.vim
(:source /path/to/pysmell/pysmell.vim
) - Set the autocomplete command (
:set omnifunc=pysmell#Complete
) - Type ^x^o to autocomplete and it should work
- 使用
setup.py install
命令安装 PySmell 。 PYSMELLTAGS
通过转到您的site-packages/django
目录并运行来为 django生成文件:pysmell . -o ~/PYSMELLTAGS.django
- 把那个文件复制到你的项目目录下,然后运行
pysmell .
生成项目PYSMELLTAGS文件 - 确保 pysmell 在您的
PYTHONPATH
(export PYTHONPATH=${PYTHONPATH}:/path/to/pysmell/
) - 运行 vim (
vim .
) - 来源
pysmell.vim
(:source /path/to/pysmell/pysmell.vim
) - 设置自动完成命令 (
:set omnifunc=pysmell#Complete
) - 键入 ^x^o 自动完成,它应该可以工作
I realize this is not a sustainable solution, but you should be able to use this as a start to getting it setup to always work (e.g., add the export to your .bashrc, add the :source to your .vimrc, setup autocmd FileType python set omnifunc=pysmell#Complete
, etc.)
我意识到这不是一个可持续的解决方案,但是您应该能够使用它作为开始设置以使其始终工作(例如,将导出添加到您的 .bashrc,将 :source 添加到您的 .vimrc、setupautocmd FileType python set omnifunc=pysmell#Complete
等.)
Let me know if this is enough to get you started. It worked for me!
让我知道这是否足以让您开始。它对我有用!
EditI simply added this to my .vimrc and as long as the PYSMELLTAGS
& PYSMELLTAGS.django
files are in my project root, it works fine without any other work:
编辑我只是将它添加到我的 .vimrc 中,只要PYSMELLTAGS
&PYSMELLTAGS.django
文件在我的项目根目录中,它就可以正常工作,无需任何其他工作:
python << EOF
import os
import sys
import vim
sys.path.append("/usr/local/python/lib/python2.5/site-packages")
EOF
exe ":source ~/src/pysmell/pysmell.vim"
autocmd FileType python set omnifunc=pysmell#Complete
回答by Dave Halter
As I wrote in other places, I developed Jedi. I really think it is far better than all the existing solutions (even PyCharm).
正如我在其他地方写的那样,我开发了 Jedi。我真的认为它比所有现有的解决方案(甚至 PyCharm)都要好得多。
https://github.com/davidhalter/jedi-vim
https://github.com/davidhalter/jedi-vim
It is built upon pythoncomplete and much much more powerful!
它建立在 pythoncomplete 之上,而且功能更强大!
It works for complex code:
它适用于复杂的代码:
And has additional features:
并具有附加功能:
There is a list of all possible features:
有一个所有可能功能的列表:
- builtin functions/classes support
- complex module / function / class structures
- ignores syntax and indentation errors
- multiple returns / yields
- tuple assignments / array indexing / dictionary indexing
- exceptions / with-statement
- *args / **kwargs
- decorators
- descriptors -> property / staticmethod / classmethod
- closures
- generators (yield statement) / iterators
- support for some magic methods:
__call__
,__iter__
,__next__
,__get__
,__getitem__
,__init__
- support for list.append, set.add, list.extend, etc.
- (nested) list comprehensions / ternary expressions
- relative imports
getattr()
/__getattr__
/__getattribute__
- function annotations (py3k feature, are ignored right now, but being parsed. I don't know what to do with them.)
- class decorators (py3k feature, are being ignored too, until I find a use case, that doesn't work with Jedi)
- simple/usual
sys.path
modifications isinstance
checks for if/while/assert
- 内置函数/类支持
- 复杂的模块/函数/类结构
- 忽略语法和缩进错误
- 多重回报/收益
- 元组分配/数组索引/字典索引
- 异常/ with 语句
- *args / **kwargs
- 装饰者
- 描述符 -> 属性/静态方法/类方法
- 关闭
- 生成器(yield 语句)/迭代器
- 支持一些魔法方法:
__call__
,__iter__
,__next__
,__get__
,__getitem__
,__init__
- 支持 list.append、set.add、list.extend 等。
- (嵌套)列表推导式/三元表达式
- 相对进口
getattr()
/__getattr__
/__getattribute__
- 函数注释(py3k 功能,现在被忽略,但正在解析。我不知道如何处理它们。)
- 类装饰器(py3k 功能,也被忽略,直到我找到一个用例,它不适用于 Jedi)
- 简单/通常的
sys.path
修改 isinstance
检查 if/while/assert
回答by Lakshman Prasad
(source: dispatched.ch)
(来源:dispatched.ch)
You can set up VIM with buffers, buffer display, auto complete, even Py Doc display.
您可以使用缓冲区、缓冲区显示、自动完成甚至 Py Doc 显示来设置 VIM。
回答by user26294
I've had good luck with exuberant-ctags for this.
为此,我在 exuberant-ctags 上很幸运。
I use this macro in my vimrc:
我在 vimrc 中使用这个宏:
execute 'map :!/usr/bin/exuberant-ctags -f '.&tags.' --recurse '.$_P4ROOT.' '
You'll want to modify that slightly, so that it includes your python /site-packages/django/ directory as well as your own code.
您需要稍微修改一下,使其包含您的 python /site-packages/django/ 目录以及您自己的代码。
Then, hit F2 inside vim to update the tags, and use the regular vim tag bindings to navigate.
然后,在 vim 中按 F2 以更新标签,并使用常规 vim 标签绑定进行导航。
回答by Sergey Vakulenko
Today, you not need special extentions for django autocomplete in vim. Make sure that you have vim with python support. To check it, type in xterm:
今天,你不需要在 vim 中为 django 自动完成做特殊扩展。确保你有支持 python 的 vim。要检查它,请输入 xterm:
vim --version|grep python
vim --version|grep python
output:
输出:
+python-python3 +quickfix +reltime +rightleft -ruby +scrollbind +signs ... ...
+python-python3 +quickfix +reltime +rightleft -ruby +scrollbind +signs ... ...
To make work autocomplete, add this lines in your .vimrc:
要使工作自动完成,请在您的 .vimrc 中添加以下行:
autocmd FileType python set omnifunc=pythoncomplete#Complete
if has("python")
python import sys,os
python sys.path.append('/home/sergey/workspace/django')
python os.environ['DJANGO_SETTINGS_MODULE'] = 'djangoProject.settings'
endif
autocmd FileType python set omnifunc=pythoncomplete#Complete
如果有(“蟒蛇”)
python 导入系统,操作系统
python sys.path.append('/home/sergey/workspace/django')
python os.environ['DJANGO_SETTINGS_MODULE'] = 'djangoProject.settings'
万一
where:
在哪里:
- sys.path.append is path to your django workspace directory
- djangoProject is name of your django project, which is going just after '/home/sergey/workspace/django'
- sys.path.append 是 django 工作区目录的路径
- djangoProject 是你的 django 项目的名称,它紧跟在 '/home/sergey/workspace/django' 之后
Finally, save it and restart vim. Now, after '.', you press default ctrl-x ctrl-o to get your autocomplete.
最后,保存并重新启动vim。现在,在“.”之后,按默认的 ctrl-x ctrl-o 来获取自动完成功能。