8

The echon and echohl commands are very useful for outputting lines of text with multiple highlight groups within the same line. This can be used to for instance create nice status messages for plugins.

In a plugin I am working on, I am using this exact method of outputting colored status messages. However, sometimes I would want to add these messages to the message-history in a similar way to what you get with echomsg. Is this somehow possible? I don't believe there are any intrinsic functionality for this, but perhaps one could add the functionality with vim script?

  • 1
    I don't understand what the difference is? From reading the help pages, echon and echomsg work the same, except that echomsg adds messages to the history? You mentioned "I have been trying for some time now", you should tell us what exactly you've tried, what you expected to happen, and what did happen. That way we can try and fix the problem you're having (if any) or explain some misconception you might have. – Martin Tournoij Mar 11 '15 at 0:29
  • I am sorry for being unclear. I've update the question in order for it to be more clear, in particular in that I want the echon functionality combined with adding messages to the history. – Karl Yngve Lervåg Mar 12 '15 at 13:54
8

This is not currently possible in Vim.

Internally, :echomsg is implemented as :execute, except that when invoked as :echomsg the result of execution is displayed with the attribute of the last :echohl and saved to the message list (reference src/eval.c functions ex_echohl and ex_execute), which is how you get any color on a saved message.

The actual message history is a collection (linked list) of strings with attributes, and is stored and retrieved in src/message.c. Each string is a message (consisting of one or more lines), and the attribute determines, among other things, the highlight group to use for display. Because of this, each message must be highlighted as a whole (single highlight group), and no more than one message may be on the same line. (reference src/message.c structure msg_hist and function ex_messages)

I cannot find any way around this (I thought of using :echohl and :echon to display a message, but store a plain message in history. Unfortunately, the ability to add to the history without also displaying the message doesn't appear to be exposed to any ex-commands) without modifying the Vim source code.

  • 1
    I've updated the question so the intent is clearer. I think you could likewise update the answer slightly following my update (but that's up to you). I believe your answer is sufficient for being marked "accepted", although I am still hoping for some good idea for "hacking" the functionality with vim script. – Karl Yngve Lervåg Mar 12 '15 at 13:56

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.