Setup VIM for Python Development

28/12/2020

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 (https://github.com/hdima/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 https://raw.githubusercontent.com/hdima/python-syntax/master/syntax/python.vim

‘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 ‘helloworld.py’ 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 ‘replace.py’ 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: https://github.com/kh3phr3n/python-syntax
Python Mode: https://github.com/python-mode/python-mode

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Hướng dẫn cấu hình nhận Real-IP từ người dùng truy cập vào trang web thông qua Nginx

Trong bài trước, mình đã hướng dẫn các bạn cách cài đặt sử dụng Nginx làm reverse proxy cho WordPress...
30/12/2020

Install Telegram on Linux

Have you decided to start using Telegram on your Linux Desktop? At first glance it seems that Telegram requires a mobile...
29/12/2020

Danh sách các nhà xe tuyến HCM đi Dak Lak Và Daklak đi Hồ Chí Minh Cập nhật mới nhất 2021

1 . Xe Dung Nghĩa HCM: (028) 38 461 776 BMT: (0262) 3 811 888 2. Xe Hiệp Trang HCM: (028) 38 55 39 44 – (028) 35...
26/04/2020
Bài Viết

Bài Viết Mới Cập Nhật

Sử dụng Proxy để Quản Lý Tài Khoản Quảng Cáo Ads Một Cách An Toàn
27/05/2023

Hướng Dẫn Sử Dụng Socks5 Trên Trình Duyệt Chrome
05/05/2023

Các tiện ích và công cụ để quản lý proxy.
27/02/2023

Tác động của việc sử dụng proxy đến tốc độ kết nối internet của bạn.
27/02/2023

Các cách để kiểm tra tốc độ và độ ổn định của proxy.
27/02/2023