vimtricks.wiki Concise Vim tricks, one at a time.

How do I insert all Vim messages into the current buffer so I can read, search, or save them?

Answer

:put =execute('messages')

Explanation

Vim's :messages command shows recent output — error messages, echo'd values, and diagnostic information — but the display is ephemeral and hard to search. Using :put =execute('messages'), you can dump the entire message history directly into the current buffer as editable text. This makes it easy to search with /, copy sections, or save them to a log file.

How it works

  • execute('messages') — calls Vim's built-in execute() function (introduced in Vim 8), which runs the :messages command and returns its output as a string
  • :put = — evaluates the expression on the right and inserts the result as new lines below the current line

Example

After some errors have accumulated:

:put =execute('messages')

This inserts the full message history into the buffer:

E488: Trailing characters: foo
W10: Warning: changing a readonly file
Press ENTER or type command to continue

Tips

  • Combine with :new to insert into a scratch buffer: :new | put =execute('messages')
  • Use execute() for any command output: :put =execute('ls') inserts the buffer list
  • Clear the message history with :messages clear (Vim 8+)
  • execute() is cleaner than the older :redir approach for one-liner capture

Next

How do I open the directory containing the current file in netrw from within Vim?