Python objects expose methods whose names are easy to forget while editing. Vim provides Python auto-completion through its built-in omni-completion, including methods belonging to a string or another recognized object.
The Python filetype plugin connects a buffer to Vim's Python 3 completion function. It requires a Vim build with Python 3 support; installing the Python command alone does not add that capability to Vim.
Suggestions appear when requested in Insert mode, rather than after every keystroke. Current Vim does not inspect imported modules by default, so its built-in Python completion is most useful for names and objects defined in the buffer.
Older Python completion scripts could execute code from an untrusted buffer. A security-updated Vim build is required before using completion on downloaded code. Enabling pythoncomplete_allow_import also permits imported modules to run code; that option is unnecessary here. See Vim's Python completion security advisory.
Steps to configure Python auto-completion in Vim:
- Open your personal Vim configuration file.
$ vim ~/.vimrc
~/.vimrc is the usual personal configuration on Linux and macOS. If Vim already uses a different file, :echo $MYVIMRC shows its path; Windows commonly uses ~/_vimrc.
- Enable filetype plugins by adding this line at the end of the configuration file.
filetype plugin on
If filetype plugin on or filetype plugin indent on is already present, no duplicate is needed. Existing settings stay in place. The Python filetype plugin selects python3complete#Complete automatically.
- Save the configuration with :wq.
Vim exits after writing the file.
- Open an unused file named completion-demo.py in a new Vim session.
$ vim completion-demo.py
The new session loads the saved configuration, and the .py extension selects Python filetype support. A separate practice file keeps the example away from existing code.
- Check the Python buffer's completion function.
:setlocal omnifunc? omnifunc=python3complete#Complete
An empty value means the Python completion function was not selected. If :echo has('python3') returns 0, this Vim build lacks Python 3 support and needs a Python-enabled replacement. On Debian or Ubuntu, the vim-nox package supplies that support.
- Press i to enter Insert mode.
- Enter the following two lines with the cursor immediately after message.st.
message = "Hello" message.st
- Press Ctrl-x followed by Ctrl-o to request Python completion.
The menu offers startswith and strip because message contains a string. Ctrl-x and Ctrl-o are consecutive shortcuts, not keys held together.
- Select strip with Ctrl-n.
Ctrl-n moves forward through matches; Ctrl-p moves backward. Ctrl-e cancels completion and restores the original prefix.
- Press Ctrl-y to accept the selected method.
The menu closes, and Vim remains in Insert mode with message.strip( in the buffer.
- Type a closing parenthesis after the completed method.
The second line now reads message.strip(). The method name came from Python completion, and the closing parenthesis shows that editing continues after accepting it.