How to Enable Syntax Highlighting in Vi/Vim Editor

One of the easiest ways to boost the readability and context of the text in a configuration file or your source code for various programing languages, is by using a text editor that supports “syntax highlighting”.

Syntax highlighting is a simple but useful component in most if not all text editors that are used for programming, scripting, or markup languages, which enables for displaying colored text, notably source code, in different colors (and possibly fonts) corresponding to the category of terms.

Read Also: 10 Reasons Why You Should Use Vi/Vim Text Editor in Linux

In this article, we will show how to turn on syntax highlighting temporarily or permanently in Vi/Vim text editor.

VIM is an alternative and advanced version of VI editor that enables Syntax highlighting feature in VI. Syntax highlighting means it can show some parts of text in another fonts and colors. VIM doesn’t show whole file but have some limitations in highlighting particular keywords or text matching a pattern in a file. By default VIM works on all Linux terminals, but some terminals have minimal highlighting capabilities to run.

VIM has another great feature that enable us to Turn Off or Turn On syntax highlighting using option syntax on and syntax off.

How to Install VIM

Most of the Linux system already included VIM package, if not then install it using YUM tool.

# yum -y install vim-enhanced

How to Enable Syntax Highlighting in VI and VIM

To enable Syntax Highlighting feature in VI editor, open the file called /etc/profile.

# vi /etc/profile

Add the alias function to VI by pointing to VIM in /etc/profile file. This file is used to set alias functions globally.

alias vi=vim

If you would like to set user specific aliases and functions, then you need to open the file .bashrc under user directory.

# vi /home/tecmint/.bashrc

Add the alias function. For example we set alias for tecmint user.

alias vi=vim

After making changes to file you need to reset the changes by executing following command.

# source /etc/profile
OR
# source /home/tecmint/.bashrc

Test Syntax Highlighting in Vi Editor

Open any example code of file with vi editor. By default Syntax Highlighting is automatically turned on in /etc/vimrc file.

Example of Syntax Highlighting in VI

Syntax Highlighting in Vi
Syntax Highlighting in Vi

Turn On or Turn Off Syntax Highlighting in VI

You can Turn On or Turn Off syntax highlighting by pressing ESC  button and use command as :syntax on and :syntax off in Vi editor. Refer example screenshots.

Turn On Syntax Highlighting in VI

Enable Syntax Highlighting in Vi Editor
Enable Syntax Highlighting in Vi Editor

Turn Off Syntax Highlighting in VI

Disable Syntax Highlighting in Vi Editor
Disable Syntax Highlighting in Vi Editor

If you are new to vi/vim, you will find the following guides useful:

  1. Learn Vi/Vim as a Full Text Editor in Linux
  2. Learn Useful Vi/Vim Editor Tricks and Tips in Linux
  3. 8 Interesting Vi/Vim Editor Tricks for Every Linux User
  4. How to Password Protect a Vim File in Linux

You can share with us any other useful vi/vim tips or tricks you have come across, via the comment form below.

If this article helped, with someone on your team.

TecMint Weekly Newsletter
Get the Learn Linux 7 Days Crash Course free when you join 34,000+ Linux professionals reading every Thursday.
Check your email for a magic link to get started.
Something went wrong. Please try again.
TecMint has been free for 14 years. Help keep it that way.
Google AI Overviews and tools like ChatGPT have cut into search traffic for independent tech sites like TecMint. Running this site costs over $2,000 every month for hosting, infrastructure, and paying authors to keep the content accurate and tested.

If this article helped you solve a problem, consider buying a coffee. It helps keep TecMint free, supports the authors, and keeps the project going.
☕ Buy Me a Coffee
Ravi Saive
I'm Ravi Saive, an award-winning entrepreneur and founder of several successful 5-figure online businesses, including TecMint.com, GeeksMint.com, UbuntuMint.com, and the premium learning hub Pro.Tecmint.com.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

6 Comments

Leave a Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.

Free Course
Get a free Linux course before you go.
Subscribe to TecMint Weekly and get the Learn Linux 7 Days Crash Course free. Read by 34,000+ Linux professionals every Thursday.
Something went wrong. Please try again.
Check your email for a magic link to get started.