When developing a vim plugin I'll publish soon, I want to highlight a text contained in a variable g:last_completion_text.

Here below the code to

  • define an highlight group named PrompterCompletion
  • assign the highlight to a text in a global vim variable g:last_completion_text
:highlight PrompterCompletion  ctermbg=3  ctermfg=0
:execute 'match PrompterCompletion /' . g:last_completion_text . '/'

If g:last_completion_text is a single line the code works correctly.

echo g:last_completion_text
Hello! How can I assist you today?

variable is a single line

But if g:last_completion_text is a multiline text (contains newline), the match command fails.

echo g:last_completion_text
21. Regione dell'enogastronomia
22. Regione della neve
23. Regione delle malghe
24. Regione delle tradizioni contadine
25. Regione delle fiabe e delle leggende
26. Regione della natura incontaminata
27. Regione delle attività all'aria aperta
28. Regione dell'architettura alpina
29. Regione delle terme e del benessere
30. Regione della storia e della cultura tirolesi.

variable is a multiline


echo g:last_completion_text


echom g:last_completion_text



Is there a way to let the match command accept newlines in the text contained in the referenced variable?

Is there a way to correct the command:

:execute 'match PrompterCompletion /' . g:last_completion_text . '/'
  • For the record: if this is for a plugin, see the end of :help :match. That command is mostly for users now; plugins should use matchadd() or text properties.
    – D. Ben Knoble
    Dec 29, 2023 at 17:37

1 Answer 1


I would try:

:execute 'match PrompterCompletion /' . substitute(g:last_completion_text, "[\n\<C-m>]", '\\n', 'g') . '/'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.