Master Vim Syntax Highlighting: Enable, Customize, and Persist Color Schemes
This guide walks you through installing Vim, checking its version, creating sample scripts, toggling syntax highlighting, permanently configuring .vimrc, switching and customizing color schemes, applying language‑specific syntax, and defining custom highlight groups for a richer editing experience.
Vim’s built‑in syntax highlighting makes code easier to read by coloring keywords, variables, comments, and other elements.
Installation and version check
On Ubuntu, install Vim with: $ sudo apt-get install vim Verify the installation using:
$ vim --versionCreate a sample script
Save the following Bash script as login.sh:
#!/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"
fiOpen it with vim login.sh to see default highlighting.
Enable/disable syntax highlighting
While editing, press ESC and type :syntax on to enable, or :syntax off to disable.
Persist settings in .vimrc
Edit ~/.vimrc (e.g., sudo vim ~/.vimrc) and add:
syntax on
set background=darkSave with :wq. Use syntax off to permanently disable.
Change color schemes
Vim stores schemes in /usr/share/vim/vim*/colors/. List them with: $ ls -l /usr/share/vim/vim*/colors/ Apply a scheme temporarily with :colorscheme morning. To make it permanent, add the same command to .vimrc.
Language‑specific syntax
Vim automatically selects a scheme based on file type, but you can override it. For a Python file, type: :set syntax=perl Press Enter to view the Perl‑style highlighting.
Custom highlight groups
Vim defines groups such as Identifier, Statement, Comment, Type, PreProc, Constant, Special, Underlined, and Error. Change a group’s color with the :hi command, e.g.: :hi Statement ctermfg=red This turns keywords like if and else red. Similar commands work for comments, constants, and other groups, allowing you to craft a personalized color scheme.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
