How do I display a colored highlighted message using a highlight group in Vim?
Answer
:echohl WarningMsg | echo "message" | echohl None
Explanation
:echohl sets the highlight group applied to subsequent :echo and :echom output. By wrapping your message between :echohl {Group} and :echohl None, you display colored text in the command line — useful when writing Vim functions, plugins, or configuration that needs to draw attention to warnings or errors.
How it works
:echohl {Group}— sets the active highlight group for:echo/:echomoutput:echo "text"or:echom "text"— outputs the message with the current highlight applied:echohl None— resets to the default highlight (always required to prevent coloring subsequent output)
Chaining multiple :echohl and :echo on one line is idiomatic:
:echohl WarningMsg | echo "Warning: no match found" | echohl None
Example
" Warn in yellow
:echohl WarningMsg | echom "Warning: no config found" | echohl None
" Error in red
:echohl ErrorMsg | echom "Error: file not found" | echohl None
" Success in green
:echohl MoreMsg | echo "Done." | echohl None
Tips
- Always reset with
echohl None— forgetting it colors all subsequent Vim output until the session resets - Use
echom(notecho) so the message is stored in:messageshistory - Common built-in highlight groups for messages:
WarningMsg— yellow, for warningsErrorMsg— red, for errorsMoreMsg— green, for success/infoQuestion— styled for prompts
- Use
:hi {Group}to inspect a group's colors - In functions, consider wrapping in
try/finallyto ensureechohl Nonealways runs