Code And Text Editors Python

Setup VIM for Python Development

How to Set Up VIM Editor for Python Programming

VIM is a customizable programmable text editor. You can do anything you want with VIM if you know how VIM works and how to write VIM plugins. This is out of the scope of this article. But now you know it is possible. Fortunately for us, many people already made a lot of VIM plugins that we can download and install on our system. This is how I am going to configure VIM with plugins that are required to work with Python. Let’s get started.

Installing VIM on Ubuntu/Debian:

On Ubuntu/Debian, you can install VIM from the official package repository of Ubuntu/Debian.
First update the package repository cache with the following command:

$ sudo apt-get update

Now you can install VIM using the following command:

$ sudo apt-get install vim

Press ‘y’ and press to continue.

VIM should be installed.

You can now run VIM with the following command:

$ vim

This is the Welcome window of VIM.

Installing VIM on CentOS/RHEL/Fedora:

You can install VIM from the official package repository of CentOS/RHEL/Fedora with the following command:

$ sudo yum install vim

Basics of VIM:

When you start VIM, it is in “Command mode”. In this mode you run VIM command. To edit a text or source code file, you press ‘i’ to go to the “Insert Mode”. In “Insert Mode” VIM should act like other text editors. You can go back to the “Command mode” from “Insert Mode” using the <ESC> key. To quit VIM, go to the “Command Mode” and type ‘:q!’ and press <Enter>. To save changes with VIM, run ‘:w’ from the “Command Mode”.

Configuring VIM for Python:

Now I am going to do some minimal VIM configuration. VIM reads a configuration file called ‘.vimrc’ from the user’s HOME directory and configure itself when you run VIM.

Now run the following command to create a ‘.vimrc’ file in the user’s HOME directory:

$ vim ~/.vimrc

VIM should show up. Notice the marked area in the screenshot? It says “New File”. If the ‘.vimrc’ file doesn’t exists, VIM creates a new file. If ‘.vimrc’ file exists, then VIM opens the existing file. I just installed VIM a while ago, so I don’t have a ‘.vimrc’ file yet.

Now press ‘i’ and VIM should go to “INSERT” mode.

Now type in the following lines:

syntax enable
set tabstop=4
set shiftwidth=4
set expandtab
set number
filetype indent on
set autoindent

Now press and type in ‘:wq!’ and press to save the file and exit VIM.
If you open ‘.vimrc’ with VIM again, you should see some changes to the editor as shown in the screenshot below.

Now I am going to install python-syntax ( syntax highlighting module on VIM for better python syntax highlighting of Python2 and Python3.  The python-syntax module must be kept on a specific directory ‘~/.vim/syntax’ for it to work.

Run the following command to create the required directories:

$ mkdir -p ~/.vim/syntax

Now navigate to the newly created directory:

$ cd ~/.vim/syntax

Now download python-syntax module with ‘wget’ with the following command:

$ wget

‘python.vim’ file should be downloaded from GitHub.

Now open the ‘.vimrc’ file with the following command:

$ vim ~/.vimrc

Now type in the following line to enable python-syntax module.

let python-highlight_all = 1

Now save the file.

I created a ‘’ file and opened it with VIM. This is how it looks like.

Searching and Replacing Hard Coded Tabs with Spaces:

In this section I will show you how to replace all the tabs in a source code file with proper number of spaces with VIM.
I created a test file ‘’ and it has several tabs that are not replaced by spaces yet.

You can search for all the tabs with ‘/\t’ VIM command. If you want your search to be highlighted, first enable ‘hlsearch’ option with the following VIM command:

:set hlsearch

Now you can find all the tabs with ‘/\t’ VIM command as shown in the screenshot below.

Now if you want to replace all the tabs to spaces of width 4, you can run the following VIM command:

: %s/\t/4_SPACES_HERE/g

All the tabs are replaced with spaces.

Now if you try to search for tabs, you should be able to see “Pattern not found” error message as shown in the screenshot below. It means there are no tabs in the text file.

This is how you configure VIM for Python. Thanks for reading this article.

Other Python Syntax Highlight Modules:

Python Syntax:
Python Mode:

About the author

Shahriar Shovon

Shahriar Shovon

Freelancer & Linux System Administrator. Also loves Web API development with Node.js and JavaScript. I was born in Bangladesh. I am currently studying Electronics and Communication Engineering at Khulna University of Engineering & Technology (KUET), one of the demanding public engineering universities of Bangladesh.