Dmitry Ermolov
Dmitry Ermolov

Reputation: 2237

how can I get last echoed message in vimscript?

Is there any way to retrieve last echoed message into a variable?
For example: if i call function, that does:

echo 'foo'

Can I somehow retrieve this 'foo' into a variable?
Thanks!

Upvotes: 7

Views: 1390

Answers (1)

ZyX
ZyX

Reputation: 53644

You can't retrieve last echoed message. But there are other options:

  1. If you can place a :redir command before this function call and another one after, you can catch everything it echoes. But be aware that redirections do not nest, so if function uses :redir itself, you may get nothing:

    redir => s:messages
    echo "foo"
    redir END
    let s:lastmsg=get(split(s:messages, "\n"), -1, "")
    
  2. If function uses :echomsg instead of :echo, then you can use :messages command and :redir:

    echom "foo"
    redir => s:messages
    messages
    redir END
    let s:lastmsg=get(split(s:messages, "\n"), -1, "")
    

Upvotes: 12

Related Questions