Omni completion i_CTRL-X_CTRL-O works in Vim without installing plugins, but only if you satisfy these three conditions:
- Vim is compiled with
+python3
- Python3 is installed
- 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