Vim will automatically soft-wrap echoed lines longer than &columns
. Due to this perk, the following does work, although the formatting would break on screen resizes.
let msg = "Hello"
echoerr msg . repeat(" ", &columns - len(msg)) . msg
The snippet should output
Hello
Hello
You probably could create a helper function to calculate that automatically
function! EchoErrMsg(msg)
if type(a:msg) == type("")
let msg = split(a:msg, "\n")
elseif type(a:msg) == type([])
let msg = a:msg
else
throw "Expecting list or string as input"
endif
if len(msg) == 1
let str_msg = msg[0]
else
for i in range(0, len(msg) - 1)
let msg_len = len(msg[i])
if msg_len < &columns
let msg[i] = msg[i] . repeat(" ", &columns - (msg_len + 1))
endif
endfor
let str_msg = join(msg, " ")
endif
echoerr str_msg
endfun
Note that this is a hack. Ideally echom
would be modified in the Vim source or a new message-related could be created to allow for the same syntax echon
.
Alternatively, if you simply want to display multiple lines as if it were an error, use echohl Error
. Albeit simpler, unlike echomsg
or echoerr
, this will not save the output to the message history.
echohl Error
echon "Line 1\nLine 2"
echohl NONE