In this video, we will take a quick look at how to customize the Vim text editor for proficient Python development. Vim is an exceptionally customizable. VIM is a very widespread and one of the most innovative text editor and it is exceptionally popular among Python Developers. This free of cost IDE can be used under the license of GPL. Although VIM is a very modern text editor, it can be used as a Python development environment when configured appropriately. YouCompleteMe unavailable: requires Vim compiled with Python (2.7.1+ or 3.5.1+) support. You need vim compiled with Python 2.6, 2.7 or 3.2 and later support for Powerline to work. How to Install Vim on MacOS Introduction. Vim which stands for vi improved is a text editor and an improved version of vi editor that is designed to run on CLI (Command Line Interface) as well as GUI (Graphical user interface).
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:
First update the package repository cache with the following command:
Now you can install VIM using the following command:
Press ‘y’ and press to continue.
VIM should be installed.
You can now run VIM with the following command:
VIM should be installed.
You can now run VIM with the following command:
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:
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 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
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.
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:
Now navigate to the newly created directory:
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:
Now type in the following line to enable python-syntax module.
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.
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:
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:
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.
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
Vim is an open source command line text editor. It is very powerful and customizable. In this article, I will show you how to configure Vim with vimrc file. So, let’s get started.Python Mode: https://github.com/python-mode/python-mode
Vim can be configured system wide (globally) via the /etc/vim/vimrc.local file on Ubuntu/Debian based operating systems. On CentOS 7 and RHEL 7, the system wide configuration file for Vim is in /etc/vimrc.
You can open the system wide configuration file of Vim as follows:
Ubuntu/Debian:
CentOS 7 and RHEL 7:
You can also do user specific configuration of Vim. All you have to do is to create a .vimrc file in the HOME directory of the user than you want to configure Vim for and add the required Vim configuration options there.
You can create a .vimrc file for your login user with the following command:
Then, open .vimrc file with vim with the following command:
For simplicity, I will refer to both system wide Vim configuration file and user specific Vim configuration file as vimrc file. You can use either the system wide Vim configuration file or the user specific Vim configuration file to configure Vim depending on what you want to do.
Showing Line Numbers:
By default, Vim does not show line numbers. Line number is very important for programming.
Vim For Mac Python Installer
To show line numbers, open Vim configuration file (global or user specific depending on your choice) and type in the following Vim command:
Vim For Mac Python Programming
Now, type in :wq! and press <Enter> to save the configuration file.
Now, Vim should show line numbers.
Enable Syntax Highlighting:
Vim may not have syntax highlighting enabled by default. But you can easily enable syntax highlighting though the vimrc file by adding the following line of Vim command to the file.
Changing Syntax Highlighting Color scheme:
Vim has many syntax highlighting color schemes by default. You can change between them if you don’t like the color scheme that is currently set.
To change the color scheme, type in the following vim command to the vimrc file.
Now, replace COLOR_SCHEME_NAME to one of the following keywords:
default, blue, darkblue, delek, desert, elford, evening, industry, koehler, morning, murphy, pablo, peachpuff, ron, shine, slate, torte, zellner
The syntax highlighting color scheme should be changed. I set it to industry as you can see in the screenshot below.
Setting Tab Size:
You can also configure the tab size in Vim though the vimrc file. If you think the default tab size is too big for you or is not enough, then this is a good news for you.
For me the tab size of 4 is enough. Python developers often use the tab size of 2.
To set the tab size to 4 (let’s say), open your vimrc file and type in the following vim command:
For tab size 2, you would type in the following Vim command in your vimrc file.
Enabling Automatic Indentation:
You can enable automatic indentation to automatically insert tabs or spaces when you write codes with Vim.
To enable automatic indentation in Vim, type in the following Vim command to the vimrc file.
Replacing Tabs with White Spaces:
When you insert tabs into your codes and save it and send it to someone else, he/she must use the same tab size as you did. Otherwise, the code would look very ugly. In Vim, there is a solution. Vim can automatically replace tabs to spaces. For example, let’s say, your tab size is set to 4. So, when you insert tab into your code, Vim will automatically replace it with 4 white spaces. So, when you send the code to your friend, it will be the same nevertheless of the tab size in his/her editor.
To replace tabs with white spaces, type in the following Vim command in your vimrc file.
Removing Multiple Spaces on Single Backspace:
If you’re using expandtab to replace tabs to white spaces automatically on Vim, then you may have noticed that pressing <Backspace> once removes only a single white space. That means, if you’ve set tabstop to 4, then you have to press <Backspace> 4 times to remove all 4 white spaces.
Luckily, Vim has remedies for that. You can type in the following line on your vimrc file to remove the number of white spaces that was replaced by for the tab by pressing <Backspace> just once.
NOTE: As I set tabstop to 4, I also set softtabstop to 4. So, 4 white spaces for each tab will be removed all together with a single press of the <Backspace> key.
Highlight the Current Line:
You can highlight the line you’re currently on in Vim. To do that, open your vimrc file and type in the following Vim command:
Other vimrc Commands:
You can add Vim functions and other commands on the vimrc file to configure Vim. You may take a look at the Vim official documentation at http://vimdoc.sourceforge.net/ to learn more about it.
You may also read the article A Good Vimrc at https://dougblack.io/words/a-good-vimrc.html to find the most common Vim commands that you can use.
Vim For Python Mac
So, that’s how you configure Vim text editor with vimrc file. Thanks for reading this article.