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

Linux File Compression Options and Comparison

Compression, in general, is a useful method that is essentially encoding information using less data than the original...
29/12/2020

Moodle [Part 2] – Cài đặt Moodle 3.8.1 trên CentOS7

Moodle là nền tảng học tập trực tuyến (LMS – Learning Management System) được sử dụng nhiều...
30/12/2020

Những câu hỏi thường gặp khi sử dụng proxy và cách trả lời chúng.

Khi bạn cần sử dụng proxy để truy cập vào các trang web bị chặn hoặc giới hạn về địa chỉ...
27/02/2023
Bài Viết

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

Mua proxy v4 chạy socks5 để chơi game an toàn, tốc độ cao ở đâu?
18/05/2024

Thuê mua proxy Telegram trọn gói, tốc độ cao, giá siêu hời
18/05/2024

Thuê mua proxy Viettel ở đâu uy tín, chất lượng và giá tốt? 
14/05/2024

Dịch vụ thuê mua proxy US UK uy tín, chất lượng số #1
13/05/2024

Thuê mua proxy Việt Nam: Báo giá & các thông tin MỚI NHẤT
13/05/2024