11

enter image description here

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 menu 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:

enter image description here

2 Answers 2

7

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:

list completion

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
6
  • I think that's the answer. I have another question, though: "How do I automatically get rid of the extra documentation window that opens up at the top ([Scratch] [Preview])?".
    – user126284
    Jun 18, 2011 at 15:38
  • 2
    Check :set completeopt?. The option 'preview' in there makes the preview window appear with the docstring in it. If you set completeopt without preview it should no longer appear. Jun 18, 2011 at 15:49
  • 2
    @Spiritwalker also see info.birnamdesigns.com/wiki/… a tip on how to auto-close the preview window when you leave insert mode or move your cursor (I prefer the insert mode method) Jun 18, 2011 at 20:32
  • @Michael, thanks for the link. I think you have to remove 'for' at the end of your link.
    – user126284
    Jun 18, 2011 at 22:10
  • Oh you're right. I wonder how I did that. Here's the correct link: info.birnamdesigns.com/wiki/… Jun 18, 2011 at 22:12
3

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
3
  • 1
    @Spiritwalker If it is possible, I do not know how to do it. I would welcome really another answer that could prove me wrong, because that would be quite useful. Jun 18, 2011 at 15:17
  • 1
  • 3
    The only way to find out what you can do with an object is evaluating everything --- which means it is more suited for an interactive interpreter interface such as IDLE, where it is already know what a refers to.
    – Jasmijn
    Jun 18, 2011 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.