Why does "print" only show one line when called from a keymap?

I’m a little confused by the behavior of the Lua print function. In particular, it seems that calling print with two messages will show both messages when called directly from Lua. However, when called from a keymap, it looks like only the last message is retained (the first one is overwritten).

What is the reason for this? Is there a way to keep old messages around without having to open :messages?


function test()

vim.keymap.set('n', '<Space>', test, { noremap = true })

call :lua test() and you’ll see both hello and world, but press space and you’ll see only world.

It’s there; just run :messages and you will see the appropriate output.