How to Enable Syntax Highlighting in Vi/Vim Editor?

Vi/Vim is a highly adaptable text editor preferred by developers and system administrators. Among its many useful features, syntax highlighting stands out as a valuable tool for differentiating various code elements through color differentiation. This feature helps identify keywords, comments, strings, and other code components at a glance, significantly improving code readability and development efficiency.

In this article, we will explore the steps involved in enabling syntax highlighting in Vi/Vim editor. Whether you are new to Vi/Vim or looking to enhance your skills, this comprehensive guide will help you take advantage of this powerful feature.

How to Install Vim

Before enabling syntax highlighting, ensure Vim is installed on your system. Open the terminal and follow these steps

Update the package lists by running the following command

sudo apt update

Install Vim by running the following command

sudo apt install vim

Verify that Vim is installed by running the following command

vim --version

If Vim is installed correctly, you should see output similar to the following

VIM - Vi IMproved 8.2 (2019 Dec 12, compiled Apr 01 2021 13:44:00)
Included patches: 1-2434
Modified by pkg-vim-maintainers@lists.alioth.debian.org
...

You can now start using Vim by running the following command

vim <filename>

Step 1: Check if Syntax Highlighting is Already Enabled

Before making any changes, it's important to check if syntax highlighting is already enabled. This prevents unnecessary modifications and helps understand the current state of your editor configuration.

To check if syntax highlighting is enabled, open a code file in Vi/Vim and enter command mode by pressing the Esc key. Then type the following command

:syntax on

This command tells Vi/Vim to enable syntax highlighting for the current file. By default, syntax highlighting may not be enabled in Vim, causing text to appear in a single color. The :syntax on command activates this feature, allowing Vim to identify and highlight different parts of code such as keywords, comments, and string literals.

If syntax highlighting is already enabled, the command will have no effect. If it's not enabled, the command will activate it immediately. After entering the command, observe if different code elements are displayed in various colors. If they are, syntax highlighting is working correctly.

Step 2: Enable Syntax Highlighting Permanently

To enable syntax highlighting permanently, you need to modify your Vi/Vim configuration file. The configuration file is named .vimrc and is typically located in your home directory.

If you don't have a .vimrc file, create one using the following command

touch ~/.vimrc

Open the configuration file for editing

vim ~/.vimrc

Add the following line to enable syntax highlighting

syntax on

To add this line, navigate to the end of the file by pressing G, then press o to create a new line and enter insert mode. Type syntax on and press Esc to exit insert mode.

Save the changes by typing :wq and pressing Enter. This writes the changes to the file and exits the editor.

Additional Configuration Options

You can enhance your syntax highlighting experience by adding these optional configurations to your .vimrc file

syntax on
set number
set hlsearch
colorscheme default

These options enable line numbers, highlight search results, and set a color scheme for better visibility.

Step 3: Verify Syntax Highlighting

After configuring syntax highlighting, verify that it's working correctly by following these steps

  • Open a code file in Vi/Vim using vim filename.py or vim filename.c

  • Observe the colors of different code elements keywords should appear in one color, comments in another, and strings in a third color

  • If highlighting isn't working, try the :syntax on command in command mode

  • Check that syntax on is present in your ~/.vimrc file if the issue persists

Common File Types and Color Schemes

File Type Extension Typical Highlighting
Python .py Keywords (blue), strings (green), comments (gray)
C/C++ .c, .cpp Keywords (purple), preprocessor (red), strings (green)
HTML .html Tags (blue), attributes (red), content (black)
Shell Script .sh Commands (blue), variables (green), comments (gray)

Conclusion

Enabling syntax highlighting in Vi/Vim significantly improves code readability and development efficiency by visually distinguishing different code elements. The process involves either using the :syntax on command for temporary activation or adding syntax on to your .vimrc file for permanent configuration. Once enabled, syntax highlighting automatically detects file types and applies appropriate color schemes, making your coding experience more productive and enjoyable.

Updated on: 2026-03-17T09:01:39+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements