Vim can be configured to provide auto-completion for Python by using its built-in omnifunc feature. While Vim doesn’t have Python auto-completion out of the box, enabling omnifunc allows for context-aware completion of Python functions, methods, and variables without the need for additional plugins.
When omnifunc is set up for Python, it offers suggestions for Python built-in functions and variables, making the coding process more efficient and reducing the need to type out long method names. This keeps the development environment lightweight while improving workflow for Python programming within Vim.
By configuring omnifunc for Python, you can activate code completion specific to Python syntax and objects, streamlining your coding experience without adding unnecessary complexity. Follow these steps to enable omnifunc for Python auto-completion in Vim.
Steps to configure Python auto-completion in Vim:
- Open any Python file in Vim.
Use any existing or newly created `.py` file in Vim to test the completion feature before changing your configuration permanently.
- Manually set omnifunc to the Python completion function.
:set omnifunc=pythoncomplete#Complete
- Enter insert mode.
Press the `i` key to enter insert mode in Vim.
- Trigger omnifunc completion with <C-x><C-o>.
- Review and select the suggested completions.
Navigate using the arrow keys. Press `Enter` to select a suggestion or continue typing to refine the list.
- If the completion works, open your vimrc configuration file.
vim ~/.vimrc
- Add a new autocmd to enable omnifunc for Python files.
autocmd FileType python setlocal omnifunc=pythoncomplete#Complete
- Save and close the vimrc file.
Save your changes by using `:wq` in Vim.
- Reopen any Python file to verify the changes.
Once reopened, omnifunc will automatically trigger for files with the `.py` extension.
- (Optional) Create a key mapping to simplify triggering omnifunc completion.
inoremap <silent><expr> <Tab> pumvisible() ? "\<C-n>" : "\<C-x>\<C-o>"
This mapping allows you to use `<Tab>` to trigger omnifunc and cycle through the completion suggestions.

Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.
Comment anonymously. Login not required.