Vim syntax highlighting doesn’t just make code pretty; it actively helps you spot errors before you even compile.
Here’s Vim highlighting my_script.py and my_config.yaml as it might look in a typical terminal:
# my_script.py
import os # Function definition, often bright blue
def greet(name): # Keyword, usually red or bold
message = f"Hello, {name}!" # String, typically green
print(message) # Variable, often the default text color
if __name__ == "__main__":
greet("World") # Call, might be the default or a specific color
# my_config.yaml
database:
host: localhost # Key, often bold or a distinct color
port: 5432 # Value (numeric), might be a different color
user: admin # Value (string), typically green
logging:
level: INFO # Enum/keyword, often a specific color like yellow
file: /var/log/app.log
To get this working, you first need to tell Vim to enable syntax highlighting. This is usually done by adding a single line to your Vim configuration file, which is typically ~/.vimrc.
syntax enable
This command tells Vim to load and apply the syntax highlighting rules for the current file type. Vim automatically detects the file type based on the file’s extension (e.g., .py for Python, .yaml for YAML). If Vim doesn’t guess correctly, or if you’re working with a file that has no extension, you can manually set the file type with :set filetype=python or :set ft=python within Vim.
Customization is where the real power lies. Vim uses "syntax groups" to categorize different elements of a language (like keywords, strings, comments, functions, etc.). You can then map these groups to specific colors and styles. These rules are defined in syntax files, usually located in ~/.vim/syntax/ or /usr/share/vim/vimXX/syntax/.
Let’s say you want to make all Python function names stand out more. You’d first need to identify the correct syntax group. For Python functions, it’s typically pythonFunction. You can see what group a particular piece of text belongs to by typing :syn list and then moving your cursor over the text.
Once you know the group, you can add a mapping to your ~/.vimrc to change its appearance. For instance, to make Python function names a bright yellow with a bold style:
highlight pythonFunction guifg=yellow gui=bold ctermfg=yellow cterm=bold
Let’s break this down:
highlight: The command to modify syntax highlighting.pythonFunction: The name of the syntax group you want to change.guifg=yellow: Sets the foreground color for GUI Vim (like gVim).gui=bold: Applies a bold style for GUI Vim.ctermfg=yellow: Sets the foreground color for terminal Vim.cterm=bold: Applies a bold style for terminal Vim.
You can use a wide range of color names (like red, blue, green, magenta, cyan, white, black, none) or hexadecimal color codes (e.g., guifg=#FF0000 for red). For styles, you can use bold, underline, italic, reverse, standout, none.
If you want to change the color of all strings in Vim, regardless of file type, you’d use the String group:
highlight String guifg=SeaGreen ctermfg=22 guibg=NONE ctermbg=NONE
Here, SeaGreen is a named color, and ctermfg=22 uses a number corresponding to a color in the 256-color terminal palette. guibg=NONE and ctermbg=NONE ensure the background color doesn’t change.
Many plugins provide enhanced syntax highlighting for specific languages or frameworks. For example, vim-polyglot is a popular plugin that bundles syntax files for hundreds of languages, often with more detailed highlighting than Vim’s built-in options. If you use a plugin manager like vim-plug, you’d add:
Plug 'sheerun/vim-polyglot'
to your ~/.vimrc and then run :PlugInstall.
The most surprising thing about Vim’s syntax highlighting is that the highlight command can also be used to define new syntax groups that your custom syntax files can then use. This is how complex highlighting rules are built: a base syntax file might define groups like myLangKeyword and myLangString, and then you can customize them using highlight myLangKeyword ... and highlight myLangString ... in your ~vimrc. It’s a layered system where Vim’s core highlighting is augmented by language-specific files, which are then further tweaked by your personal configuration.
After you’ve made changes to your ~/.vimrc, you’ll need to either restart Vim or source the file within Vim by typing :source ~/.vimrc for the changes to take effect.
The next thing you’ll likely want to tackle is configuring Vim’s indentation to match the syntax you’re highlighting.