As a developer, choosing the right text editor can make a huge difference in productivity. Vim is one of the most popular text editors, known for its keyboard-centric workflows and immense customizability. With some tweaking, Vim can be turned into a Python powerhouse IDE.

In this guide, we‘ll go over how to set up Vim for efficient Python development, including plugins for syntax highlighting, auto-completion, linting, and more. Whether you‘re new to Vim or a seasoned user, these tips will help you code Python faster and with fewer errors.

Installing Vim

First, we need to install Vim if you don‘t already have it.

Linux

On Linux, Vim usually comes pre-installed. To confirm, open a terminal and type:

vim --version

If Vim is installed, you‘ll see output like:

VIM - Vi IMproved 8.1

If not installed, use your package manager to install it. For example, on Ubuntu/Debian:

sudo apt install vim

MacOS

Vim comes pre-installed on MacOS as well. You can launch it from the terminal with the vim command.

If needed, install Vim via Homebrew:

brew install vim

Windows

On Windows, download and install Vim from vim.org. Make sure to add Vim to your PATH environment variable.

Basic Vim Configuration

Before installing any plugins, we need to set up some Vim options and settings to customize the editing experience.

Create a .vimrc file in your user directory. This file loads every time Vim starts, allowing you to configure settings.

vim ~/.vimrc

Here are some recommended settings for Python development:

" Show line numbers
set number

" Highlight search results
set hlsearch

" Use spaces instead of tabs
set expandtab

" Indent with 4 spaces
set shiftwidth=4
set tabstop=4

" Enable mouse support
set mouse=a

" Better command-line completion
set wildmenu

" Show partial commands in status line
set showcmd

" Enhance Python syntax highlighting
let python_highlight_all = 1

Now Vim will have improved Python support enabled by default.

Plugins

Next, we‘ll install some plugins to add extra functionality for Python editing.

Plugin Manager

Managing plugins manually is difficult. We‘ll use Vim-Plug to install and update plugins easily.

Add this to your .vimrc:

" Plugin manager
call plug#begin()

" Plugins go here

call plug#end()  

Then install Vim-Plug:

curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
    https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim

Now you can install plugins by adding them between plug#begin and plug#end.

Python-Mode

Python-mode adds many features useful for Python development, including:

  • Syntax highlighting
  • Auto-indentation
  • Code folding
  • Code formatting
  • Linting integration

Install it with Vim-Plug:

Plug ‘python-mode/python-mode‘, { ‘for‘: ‘python‘, ‘branch‘: ‘develop‘ }

Make sure to replace develop with the latest release version.

Jedi-Vim

Jedi-vim provides auto-completion and documentation support using the Jedi library. It can do things like:

  • Display function signatures
  • Find definitions and references
  • Intelligent auto-completion

Install it with:

Plug ‘davidhalter/jedi-vim‘

Other Plugins

Here some other useful Python plugins to consider:

Check out Vim Awesome for more plugins.

Plugin Usage Tips

Here are some tips for effectively using Python plugins in Vim:

Autocompletion

Bring up the autocomplete menu with <C-n> in insert mode. Navigate suggestions with <C-n> and <C-p>. Accept a suggestion with <Tab>.

Show function signatures with <C-Space>.

Formatting

Format code with == (normal mode). Configure formatters like YAPF or Black in .vimrc.

Linting

See lint warnings/errors with :Flake8 or integrated using an ALE plugin.

Snippets

Type a snippet shortcut then <Tab> to expand. For example, for<Tab> can expand to a for loop. Create custom snippets as needed.

Splits/Tabs

Use splits (:split/:vsplit) and tabs (:tabnew) to view multiple files. Navigate with <C-w> and :tabs.

Final Vimrc Example

Here‘s an example .vimrc putting together all the suggestions in this guide:

" Vundle plugin manager
call plug#begin()

" Python plugins
Plug ‘python-mode/python-mode‘, { ‘branch‘: ‘develop‘ } 
Plug ‘davidhalter/jedi-vim‘
Plug ‘mindriot101/vim-yapf‘
Plug ‘nvie/vim-flake8‘ 

call plug#end()

" Settings
set number relativenumber
set tabstop=4
set shiftwidth=4
set expandtab
set mouse=a

" Enable plugins
let g:python_highlight_all = 1
let g:yapf_style = ‘google‘ 

" Keymaps
nmap <F8> :Yapf<CR>

This sets up python-mode, autocompletion, formatting, linting, and some useful keymappings. Customize with more plugins and settings to your preference.

Wrapping Up

With these setup steps, Vim transforms into a highly-efficient Python editor. The keyboard-driven editing keeps your hands on the home row, while plugins provide coding superpowers like lightning-fast autocomplete and refactoring capabilities.

Some time spent customizing Vim to your workflow can unlock huge productivity gains. The built-in configuration options and Python plugin ecosystem make Vim extremely versatile. Armed with this editor, you can write Python code faster than ever.

So give Vim a try for your next Python project! Let me know in the comments if you have any other favorite plugins or settings for Python development.

Similar Posts