vimtricks.wiki Concise Vim tricks, one at a time.

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/:echom output
  • :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 (not echo) so the message is stored in :messages history
  • Common built-in highlight groups for messages:
    • WarningMsg — yellow, for warnings
    • ErrorMsg — red, for errors
    • MoreMsg — green, for success/info
    • Question — styled for prompts
  • Use :hi {Group} to inspect a group's colors
  • In functions, consider wrapping in try/finally to ensure echohl None always runs

Next

How do I configure Vim's :grep command to use a faster external search tool like ripgrep or ag?