Vim Autocomplete for Python

Fariba Laiq Sep 23, 2022
  1. Vim for Python
  2. Vim Autocomplete for Python
Vim Autocomplete for Python

Vim, short for Vi Improved, is a powerful text editor often preferred as a development environment in Python. This article will explore Vim and its autocomplete feature for Python.

Vim for Python

Vim has been around for the service of developers worldwide, a feat quite admirable in this ever-changing world of continuous technological advancement. Vim has been known to follow a particular school of thought regarding its features, which can only be best explained by the term: Efficiency by the keyboard.

It means that Vim was built around the idea that, like a writer with a pen and paper, a software developer should be able to write their code efficiently without being distracted by myriad other distractions like having to scroll or find options to select during the development. For a developer, the keyboard is their pen.

Vim follows a structured command language, and operating within it can be intimidating at first. There are undoubtedly many key commands to get used to before we can ultimately master the art of programming in a distractions free environment like Vim.

However, the commands exist in a logical and easy-to-understand stack, which makes getting used to it much faster.

Features of Vim

Vim and Python make a powerful combination and are a preference for many software developers out there.

Vim is easy to set up as an IDE with a lot of plugins to offer. These help the software developer customize Vim and create an environment best suited for their development needs.

While it is not possible to cover all available features that can be used to best utilize Vim as an IDE in this article, we can indeed provide a list of the most in-demand ones:

  1. Split Layouts

    ​We can open a new file right below the current file using: sp <filename>.

  2. Code Folding

    ​We can collapse classes or methods that are not vital for us to view fully at the moment.

  3. Buffer

    ​Vim offers easy access to our recently opened files with the help of buffers. Just use b <buffer name or number> to open a file or ls to list all the buffers.

  4. UTF-8 Support

    ​We can ensure that Vim is using UTF-8 for encoding by setting it up using this script: set encoding=utf-8.

  5. Flagging Unnecessary Whitespace

    ​With an editor like Vim, it is possible for there to be extra whitespace when we’re developing. We can have Vim flag this whitespace to easily spot and remove it.

    au BufRead,BufNewFile *.py,*.pyw,*.c,*.h match BadWhitespace /\s\+$/
  6. Auto Indentation

    ​With Python being used as a development language, indentation is a crucial part of the development phase. Auto indentation helps the developer worry less about errors caused by extra or no indentation.

    Sometimes, when the definition of a method lasts longer than a line, auto-indentation does not work. We can counter this by setting up indentpython.vim like so:

    Plugin 'vim-scripts/indentpython.vim'
  7. Syntax Check

    ​The mark of any modern IDE is its ability to keep the syntax of any development language. We can set this up for Vim by adding the syntactic plugin and flake8 for adding the pep8 feature like so.


    Plugin 'vim-syntactic/syntactic


    Plugin 'nvie/vim-flake8' #adds pep8
  8. Pep8

    ​We can set up pep8 checks for Vim to keep a review of our coding standards.

Vim Autocomplete for Python

The best plugin to make the Autocomplete feature available for Python is YouCompleteMe. While it is true that YouCompleteMe uses various other autocompletion like Jedi to function, it is the best plugin there is for recent Python versions.

We can use Vundle to install and set it up in our environment like so:

Bundle 'Valloric/YouCompleteMe'

We can find the complete installation instructions in this extensive documentation for all OS types.

Here is how to set it up on a 64-bit Linux system:

  1. We start by running the following command to install all completing packages. The following command will install the YCM plugin through Vundle, CMake, Vim, and Python; if you already have these packages installed, they won’t be reinstalled.

    apt install build-essential cmake vim-nox python3-dev
  2. Now, we install mono-complete, go, node, java, and npm with the following command:

    apt install mono-complete golang nodejs default-jdk npm
  3. Now, all we must do is compile YCM like so:

    cd ~/.vim/bundle/YouCompleteMe
    python3 --all

While the installation is pretty straightforward, we can customize it like so:

let g:ycm_autoclose_preview_window_after_completion=1
map <leader>g  :YcmCompleter GoToDefinitionElseDeclaration<CR>

This script ensures that once we are done with autocomplete, the window goes away, and a shortcut for the GoTo definition is made in the second line of the script.

Author: Fariba Laiq
Fariba Laiq avatar Fariba Laiq avatar

I am Fariba Laiq from Pakistan. An android app developer, technical content writer, and coding instructor. Writing has always been one of my passions. I love to learn, implement and convey my knowledge to others.