enable syntax highlighting in vi vim editor

How to Enable Syntax Highlighting in Vim

Syntax highlighting is a useful feature on every text editor. It helps you easily understand your source code of different programming languages, or settings in a configuration file. In syntax highlighting, the file editor analyzes the file content and highlights specific parts of it different colors, fonts and styles so that readers can easily understand what they see. Vi editor is a popular text editor in Linux but syntax highlighting is disabled by default in it. But it is enabled in vim editor by default. In this article, we will learn how to enable syntax highlighting in Vim. These steps work for both vim as well as its predecessor vi editor.


How to Enable Syntax Highlighting in Vim

In most systems, both vi and vim are already present by default. So there is no need to install them separately.

First, we open /etc/profile file in text editor.

$ vi /etc/profile

Next, add the alias for vim editor. This will point vi editor to vim editor and both commands will give same result. When you set alias in /etc/profile, it is applicable globally for all users.

alias vi=vim

If you want to set this alias for only specific user, add the above line in ./bashrc file.

$ vi ~/.bashrc

Save and close the file after you make changes. Run the following command to apply changes.

# source /etc/profile
OR
# source ~/.bashrc

Now when you open vi it will open vim editor, which has syntax highlighting enabled by default. If syntax highlighting is not enabled by default in your case, open vimrc file at /etc/vimrc or /etc/vim/vimrc, depending on your system.

$ vi /etc/vimrc
OR
$ vi /etc/vim/vimrc

Once the file is open, look for the following lines.

if has("syntax")
  syntax on
endif

Make sure that the above lines are uncommented, that is, if you find hash ‘#’ at their beginning, remove them. This will enable syntax highlighting in vi/vim editor.

Save and close the file. Now syntax highlighting should be enabled on your system. If it is still not enabled, just start a new session to apply changes. Alternatively, you can run the following command to apply changes.

# source /etc/profile
OR
# source ~/.bashrc

The above steps ensure that syntax highlighting is enabled all the time, whenever you use vi/vim editor.

On the contrary, if you want to permanently disable syntax highlighting, just comment the following in vimrc file, by adding hash ‘#’ at their beginning.

#if has("syntax")
#  syntax on
#endif

If that is not your requirement, you can use ‘:syntax on’ command in vi/vim editor to turn on syntax highlighting, and ‘:syntax off’ to turn off syntax highlighting.

# Turn on syntax highlighting
:syntax on

# Turn off syntax highlighting
:syntax off

But in this case, these changes last only as long as your vi/vim editor is open. When you close the editor, the changes will be reset to your default settings.

In this article, we have learnt how to enable syntax highlighting in vi/vim editor. If you want to permanently enable/disable syntax highlighting, you need to edit the vimrc file mentioned above. If you want to turn on/off syntax highlighting temporarily, use :syntax on/off command in your editor.

Also read:

How to Run Same Command Multiple Times in Linux
How to Reload Bashrc Settings Without Logging Out
How to Split File in Linux
Random Password Generator in Python
How to Generate Random Password in Linux

Leave a Reply

Your email address will not be published. Required fields are marked *