Andna
Andna

Reputation: 6689

Vim,omnicomplete and python documentation

I started to learn Python, and I tried to setup my vim for it. I have vim 7.3.5.3, it has omnicompletion and I downloaded pyflakes. I read this article and it says that omnicompletion should also display some documentation (there is also an image with string.atoi and some documentation), but when I try something like this:

import string
string.

and do CTRL-xCTRL-o I get the proper listing, but I don't get any documentation.

Should I install other plugins or change some configuration file?

EDIT: I needed to install supertabs to get the effect I wanted.

Upvotes: 4

Views: 5619

Answers (3)

Mike Gazes
Mike Gazes

Reputation: 194

Omni completion i_CTRL-X_CTRL-O works in Vim without installing plugins, but only if you satisfy these three conditions:

  1. Vim is compiled with +python3
  2. Python3 is installed
  3. for the Python packages you want to omni-complete:
    • the package is installed
    • the script you are editing has an import statement for the Python package
    • if doing import numpy as np, you are doing i_CTRL-X_CTRL-O after typing np., not numpy.

The Python3 omni completion is defined in the python3complete.vim that comes with Vim.

:new
:set filetype=python
:echo &omnifunc
python3complete#Complete

I found this in my Vim installation here:

/usr/share/vim/vim81/autoload/python3complete.vim

Details

1. Vim compiled with +python3

:ve to check Vim version and enabled features. Here is the relevant output when I run :ve

VIM - Vi IMproved 8.1 (2018 May 18, compiled Jul 28 2019 15:01:57)
...
Huge version without GUI.  Features included (+) or not (-):
...
+python3/dyn
...

2. Python installation

This is easy to confuse when there are multiple Python installations. For example, I have a Windows Python3 installation:

  • Windows Python3 runs from PowerShell
PS> python
Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AMD64)] on win32
  • Windows Python3 runs from Cygwin bash
$ python.exe
Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AMD64)] on win32
  • Windows Python3 does not run from the Cygwin Vim command line

    :python3
    
    • of course I can use :!python.exe for Vim to run python.exe in bash

    :!python.exe
    Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AMD64)] on win32
    
    • but that does not help omni completion
    • for omni completion Vim needs to be able to Python3 directly from the Vim command line

Test Vim can call Python3:

:py3 print("hey")
hey

I had to install Python with the Cygwin package manager to get the output hey.

3. Python package installation

If you have the mutliple Python installation problem, then also install another copy of the Python packages you want to omni-complete for.

For example, I installed the Python packages with the Cygwin package manager.

If the Python package is not available through your package manager, clone a copy of the package repository at the path identified by site.USER_SITE. This is part of the package search path, so omni completion searches in this path as well.

Find out the USER_SITE path from Vim:

: py3 import site; print(site.getusersitepackages())

The path should look something like this:

/home/myname/.local/lib/python3.7/site-packages

Alternatively, check from bash:

$ python3.7 -m site --user-site
/home/myname/.local/lib/python3.7/site-packages

Note the path is defined even it is does not exist. You need to create the path if this is the first time putting a package there.

With the above conditions satisfied, omni completion works. Start a .py file (or just open a new buffer and :set filetype=python), then try typing the following:

import numpy as np
np.CTRL-X_CTRL-O

The omni-complete window pops-up for cycling through with CTRL-N (next) CTRL-P (previous).

There is additional confusion because python3complete does not use a tags file like ccomplete does. Please see my solution to this post: Vim's Omnicompletion with Python just doesn't work

Upvotes: 1

klen
klen

Reputation: 1615

Check my plugin: https://github.com/klen/python-mode

Rope omnicompletion from the box. Also many others features.

Upvotes: 2

Thanasis Petsas
Thanasis Petsas

Reputation: 4448

To enable code(omni) completion, add this line to your vimrc ($HOME/vimrc):

autocmd FileType python set omnifunc=pythoncomplete#Complete

If it doesn’t work then, you’ll need this plugin.

Upvotes: 3

Related Questions