Say you want to port nethack to vim, or, on a more serious note, make a nice looking gui for a script you may have -- say, an mp3 player, or a tree browser. Here are a few commands that allow you to put (full screen) arbitrarily colored text in pure vimscript -- they are very slow, but also convenient and portable. It's all in the manual, but, if you're like me, it may take you a while to put it all together:
set nomore-- This eliminates the "Press enter for more" prompt entirely, allowing you to produce animations by echoing
&lines*&columnschars at once.
echohl-- This allows you output text in arbitrary colors, either predefined (eg,
echohl visual) or customized (
hi myhighlight ctermfg=0 ctermbg=12)
echon-- Unlike plain
echo, this does not insert newlines or spaces, allowing you to, for example, produce a single line with multiple colors, eg:
echohl None echon "xxxx" echohl Visual echon "yyyy "will produce xxxxyyyy
The title of this proposed tip is a bit ambitious: graphics and drawing? I'm not sure that this page is really useful without something showing how it could be useful. Echo is ok for an occasional message, but not for a GUI. Netrw is an example of a Vim script that does complex presentation, without echo. Several tips use echohl/echon, but none discuss it as a topic, but there are lots of Vim commands, and I'm not sure that a tip on each is useful. JohnBeckett (talk) 09:52, August 7, 2013 (UTC)
I found it useful because vim comes so close to being a general purpose application scripting language but it is usally seen as lacks screen-drawing functions. There are patches that do expose the screen but they haven't been mainlined yet -- this method will obviously be obviated if they ever do. But it took me literally years to actually put all the parts together. For example, I have a nested list / dictionary editor that I've been sourcing for about 3 or 4 years which uses purely echo commands. This has the advantage of not needing to open a buffer and being a bit more straightfoward to code. The screen flicker is almost nonexistent even with the key held down, and certainly non-existent with single key presses. I went through a long time using "redraw! | echo" before I stumbled upon, randomly, the "se nomore" setting. And I went through a long time with it being monochromatic before I figured out that I can use "echon" to arbitrarily color texts -- so now the cursor selection is highlighted as "visual", I have different highlighting for types, and so on. It's really just a few lines of code away from being a nested file browser that doesn't need to load in a new tab.
Basically, you can imagine it as a quick and dirty way for someone used to having minimal screen drawing functions to write some prettier scripts -- and it does actually work pretty well. Q335r49