6

I found from the vim's manual that we can echo colored text by the following command:

:echohl WarningMsg | echo "Warnging" | echohl None | echo ":Some warning"

which will highlight the string Warning and keep :Some warning in default color. My problem is that this will display the results in two lines, is there any way to display them in one line?

1

1 Answer 1

11

This should work:

function! EchoWarning(msg)
  echohl WarningMsg
  echo "Warning"
  echohl None
  echon ': ' a:msg
endfunction

call EchoWarning('test')

This defined a simple function that you can call with the warning message as the argument.

As you see, the key is the echon command, which is similar to an echo except it does not add a newline at the end.

2
  • how can i specify other colors? such as green.
    – alexzander
    Jun 8, 2021 at 9:05
  • You can define a new highlight group (see :help :highlight) and use it instead of WarningMsg. Jun 8, 2021 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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