Fundamentals 9 min read

Master Vim Syntax Highlighting: Enable, Customize, and Persist Color Schemes

This guide walks you through installing Vim, checking its version, creating a sample script, enabling and disabling syntax highlighting, permanently configuring color schemes via .vimrc, exploring built‑in color schemes, and customizing highlight groups for any language.

ITPUB
ITPUB
ITPUB
Master Vim Syntax Highlighting: Enable, Customize, and Persist Color Schemes

Installing and Verifying Vim

On Ubuntu, Vim is usually pre‑installed; if not, run sudo apt-get install vim. Verify the installation with vim --version (or vim -version).

Creating a Sample Bash Script

Create a file named login.sh with the following content:

#!/bin/bash

echo "Type your username"
read username

echo "Type your password"
read password

if [[ $username == "admin" && $password == "secret" ]]; then
  echo "Authorized user"
else
  echo "Unauthorized user"
fi

Open it in Vim with vim login.sh.

Enabling and Disabling Syntax Highlighting

While editing, press ESC and type :syntax on to turn highlighting on, or :syntax off to turn it off.

Making Highlighting Permanent

Edit your .vimrc (e.g., sudo vim ~/.vimrc) and add the line syntax on to enable highlighting for every session, or syntax off to disable it.

Changing the Color Scheme

Vim ships with many schemes located in /usr/share/vim/vim*/colors/. List them with ls -l /usr/share/vim/vim*/colors/. To apply a scheme temporarily, open a file and run :colorscheme morning. To make it permanent, add the following to .vimrc:

color evening
set background=dark

Choosing a Scheme by Language

Vim automatically selects a language‑specific scheme. You can override it, for example, to view a Python file with Perl syntax highlighting by typing :set syntax=perl and pressing Enter.

Understanding Highlight Groups

Vim defines nine main highlight groups that control what gets colored:

Identifier : variables

Statement : keywords such as if, else, while Comment : comments

Type : data types like int, double, string PreProc : pre‑processor directives (e.g., #include)

Constant : literals, numbers, strings, true/false

Special : special symbols such as \t, \n Underlined : underlined text

Error : error highlighting

Customizing Highlight Colors

Use the :hi command to change a group's color. For example, to make all statements red: :hi Statement ctermfg=red After running the command, keywords like if and else appear in red. The same approach works for comments, constants, types, etc., allowing you to craft a personal color scheme.

Putting It All Together

By combining temporary commands ( :syntax on, :colorscheme) with permanent settings in .vimrc, you can fully control Vim’s appearance for any language, making code reading more comfortable and visually appealing.

Vim installation screenshot
Vim installation screenshot
Vim version output
Vim version output
Enabling syntax highlighting
Enabling syntax highlighting
Permanent syntax on in .vimrc
Permanent syntax on in .vimrc
Evening color scheme
Evening color scheme
Resulting color scheme
Resulting color scheme
Perl syntax view
Perl syntax view
Custom highlight for statements
Custom highlight for statements
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

customizationterminalsyntax highlightingcolor scheme.vimrc
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.