Wikia

Vim Tips Wiki

Graphics and Drawing in vimscript

Talk0
1,612pages on
this wiki
Revision as of 23:23, January 13, 2014 by 24.30.243.140 (Talk)

Say you want to make tetris in pure vimscript or a nice looking gui for a script you may have: here are a few commands that allow you to put arbitrarily colored text on the screen, in full screen if desired. This is very slow but also convenient and portable, and is often preferable to opening another tab / split / buffer to display an interface. It's all in the manual, but, as is typical of vimscript, it often takes awhile to put it all together:

Commands

  • set nomore -- This eliminates the "Press enter for more" prompt entirely, allowing you to produce animations by echoing &lines lines or &lines*&columns chars 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
  • redr! -- For moderate screen sizes (say, more than 25 lines), directly echoing lines may produce a flicker. That would mean you would want to clear the screen first with redr!. However, this redraws the buffer you're currently editing as well. So to speed things up, you would want to set the commandheight (ch) to the largest possible value so redrawing mostly redraws the blank command line and not the current buffer:
let &ch=&lines-1
while drawing
   redr! "clear screen
   "echo some lines
endwhile
let &ch=1

Comments

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

Around Wikia's network

Random Wiki