2

I find that when running :echo message with a message variable that contains newlines, it displays the newline character as ^@. Often I'd prefer it to actually be split on a new line though. Do I have to split up the string manually and then make multiple calls to echo or is there a simpler way to do that?

edit: Here's an example that reproduces what I'm talking about: :echom "foo\nbar". I'm running gvim on windows

  • Can you be more specific about those newlines. Where is the text coming from. Are you on *nix or Windows? (Can't be regular, multi-line Vim text because that should work just fine.) – B Layer Dec 8 '18 at 3:19
  • Ok I updated the question – Steve Vermeulen Dec 8 '18 at 3:25
4

You originally mention :echo but based on your example it's :echom that is causing you issues so assuming that's right...

Some choices depending on your specific needs (e.g. do you care about the message being saved to the message history)...

:echo "foo\nbar"

:echon "foo\nbar"

:echom "foo" | echom "bar"

All of these will produce

foo
bar

Likely due to it's primary purpose being to print messages to be read by the user interactively, echom parses things a bit differently from the others. "Unprintable characters are displayed not interpreted". (Similar to the strtrans() function which also prints newlines as ^@).

| improve this answer | |
  • Ohh I didn't realize this behaviour was specific to echom. Thanks. – Steve Vermeulen Dec 8 '18 at 6:58
  • @SteveVermeulen No problem. Yeah I think the primary use case for echom, in contrast to the others, is real-time display of short messages for users. Thus you get qualities like avoiding non-printing chars and discouraging multi-line output. – B Layer Dec 8 '18 at 8:38
0

Although the question is about echo or echomsg, from some reason echoerr acts differently, so in addition to the 1st answer given, it may worth noting echomsg's caveats:

If you want to display an error with new lines, this won't work:

echoerr 'line 1 here\n' .
 \ 'line 2 there'

Removing the . won't help. Doing this:

echoerr 'line 1 here'
echoerr 'line 2 there'

Is a bit better - but the output looks like this:

Error detected while processing function 212:
line   18:
line 1 here
Press ENTER or type command to continue
Error detected while processing function 212:
line   19:
line 2 there
Press ENTER or type command to continue

And this is probably not what one would desire - having to press these ENTER keys.

Hence, the probably best solution is:

let v:errmsg = "line 1 here\n" .
  \ "line 2 there"
echo v:errmsg
| improve this answer | |

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.