H ow do I turn on or off color syntax highlighting in vi or vim text editor on a Linux or Unix-like operating system?
Vim or vi is a text editor. It can be used to edit all kinds of plain text files, editing programs, and UNIX/Linux configuration files. Vim supports additional options and it includes a lot of enhancements above old good vi text editor. Syntax highlighting is nothing but a feature of vi/vim text editors that displays text, especially source code, in different colors and fonts according to the category of terms. The following instructions show you how to enable or disable syntax colors for VI/VIM text editor running on a Linux or Unix-like system.
Tutorial details | |
---|---|
Difficulty level | |
Root privileges | No |
Requirements | Linux or Unix terminal |
Category | Text Editor |
Prerequisites | vim |
OS compatibility | *BSD ⢠⢠⢠⢠WSL |
Est. reading time | 4 minutes |
How to enable vim syntax colors option
The permanent procedure is as follows for vim/vi:
- Edit the ~/.vimrc file by typing the command: vim ~/.vimrc
- Append the following option:
syntax on - Save and close the file
- Test it by running vim command: vim foo.sh
Let us see all steps in details for turning on or off color syntax highlighting under Linux, macOS and Unix-like system when using vim text editor.
Please note that on most Linux distro vi is a symbolic link to vim text editor.
Turn on color syntax highlighting in vim
Open a file, for example open existing file called file.c, enter:
$ vi file.c
Now press ESC key, type â: syntax on â i.e. type as follows:
:syntax on
If above syntax failed to work, try (press Esc and then type):
:set syntax=on
Here is C source code editing while syntax highlighting is on in vi or vim:
Fig.01: Turn on or off color syntax highlighting in vi or vim
Turn off color syntax highlighting in vim
To turn it back off, press ESC key, type : syntax off
:syntax off
How do I make the syntax highlighting permanent under vim?
You can edit ~/.vimrc file and add append vim command syntax on to it. This ensures that vim will start with color syntax highlighting option. First, use the cmd command to change to the current directory:
$ cd ~ $ vi .vimrc
Append the following line:
syntax on
Save and close the file. You should now be able to use vim to edit files with syntax on options.
How to set and use a vim color scheme
You can find installed color schemes in the following path:
/usr/share/vim/vim*/colors/
Use the ls command to list it:
$ ls -l /usr/share/vim/vim*/colors/
Sample outputs:
total 76
-rw-r--r-- 1 root root 2476 Jul 27 01:43 blue.vim
-rw-r--r-- 1 root root 2990 Jul 27 01:43 darkblue.vim
-rw-r--r-- 1 root root 548 Jul 27 01:43 default.vim
-rw-r--r-- 1 root root 2522 Jul 27 01:43 delek.vim
-rw-r--r-- 1 root root 2812 Jul 27 01:43 desert.vim
-rw-r--r-- 1 root root 1666 Jul 27 01:43 elflord.vim
-rw-r--r-- 1 root root 2452 Jul 27 01:43 evening.vim
-rw-r--r-- 1 root root 1958 Jul 27 01:43 industry.vim
-rw-r--r-- 1 root root 3555 Jul 27 01:43 koehler.vim
-rw-r--r-- 1 root root 2460 Jul 27 01:43 morning.vim
-rw-r--r-- 1 root root 2006 Jul 27 01:43 murphy.vim
-rw-r--r-- 1 root root 1037 Jul 27 01:43 pablo.vim
-rw-r--r-- 1 root root 2673 Jul 27 01:43 peachpuff.vim
-rw-r--r-- 1 root root 2904 Jul 27 01:43 README.txt
-rw-r--r-- 1 root root 1393 Jul 27 01:43 ron.vim
-rw-r--r-- 1 root root 2720 Jul 27 01:43 shine.vim
-rw-r--r-- 1 root root 2445 Jul 27 01:43 slate.vim
-rw-r--r-- 1 root root 1629 Jul 27 01:43 torte.vim
-rw-r--r-- 1 root root 1840 Jul 27 01:43 zellner.vim
To use torte color scheme type the following in vim text editor:
:colorscheme torte
Sample outputs:
Using a vim color scheme
Summary
Add command in your ~/.vimrc file:
vim Command | Description/Purpose |
---|---|
syntax on | Enabling vi/vim syntax colors |
syntax off | Disabling vi/vim syntax colors |
colorscheme industry | Specifying a colorscheme called foo. Use â/usr/share/vim/vim*/colors/â to find installed color scheme |
Vim Syntax highlighting options
You can also use the following command that switches on syntax highlighting:
:syntax enable
Which basically execute the command:
:source $VIMRUNTIME/syntax/syntax.vim
More about turning on and of vim syntax highliting
Syntax highlighting enables Vim to show parts of the text in another font or color. Those parts can be specific keywords or text matching a pattern. Vim doesnât parse the whole file (to keep it fast), so the highlighting has its limitations. Lexical highlighting might be a better name, but since everybody calls it syntax highlighting weâll stick with that. Vim supports syntax highlighting on all terminals. But since most ordinary terminals have very limited highlighting possibilities, it works best in the GUI version, gvim. See following tutorials:
- Turn off color in Linux terminal/bash session
- How To Show or Hide Line Numbers In vi / vim Text Editor
When running vim, type the following option to read help about vim syntax highlighting (or see online help):
:help syntax
đĽş
Was this helpful?
Please add a comment to show your appreciation or feedback.