How do I get auto-suggestions for array options when typing in Vim?
Let's say I type
a = [1, 2]
in a .py file in vim and when I type "a." and hit TAB, I would like to get suggestion me开发者_StackOverflow中文版nu that is related to lists.
Edit 1 in response to Robin's comment: I think it's possible in vim, because there is a plugin that checks if a given python code is a valid code (I don't know what the plugin is called). Take a look:
Recent versions of vim come with an omnicompletion script called pythoncomplete.
Open up a python file, and type
:set completefunc?
to check what the current completion function is. If you get back
completefunc=
then no completionfunction is currently set. You can set pythoncomplete to be the completion function by typing
:set completefunc=pythoncomplete#Complete
and you can set this to be the default for python files using (in your vimrc)
autocmd FileType python set completefunc=pythoncomplete#Complete
Now when you're in vim, you can use omnicomplete using Ctrl+X Ctrl+O and you should get a popup menu as shown below:
You can also bind this to the tab key in insert mode with (in your vimrc):
inoremap <Tab> <C-x><C-o>
To find out more about interacting with the dropdown menu that appears, try
:help ins-completion
Read one of the many blog posts on setting up Vim as a Python IDE. Here's one to get you started. In particular, you are interested in the OmniComplete function.
This is bound by default to the keystroke Ctrl-xCtrl-o but you can rebind it to the tab key.
Note that it is not sensitive to the type of a variable. It can complete for you if you type:
string.<ctl-x><ctl-o>
you'll get a list of the string object methods. But if you do as you described in your question something like:
x = "a string"
x.<ctl-x><ctl-o>
vim will not know that the variable x
holds a string, and will not be able to supply a list of methods.
For information on omnicomplete:
:help omnifunc
精彩评论