Wikia

Vim Tips Wiki

Changes: Graphics and Drawing in vimscript

Edit

Back to page

(Comments)
(Insert TipProposed template + manual clean)
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
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:
+
{{TipProposed
  +
|id=0
  +
|previous=0
  +
|next=0
  +
|created=July 31, 2013
  +
|complexity=basic
  +
|author=Q335r49
  +
|version=7.0
  +
|subpage=/2013
  +
|category1=
  +
|category2=
  +
}}
  +
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==
 
==Commands==
Line 5: Line 5:
 
*<code>echohl</code> -- This allows you output text in arbitrary colors, either predefined (eg, <code>echohl visual</code>) or customized (<code>hi myhighlight ctermfg=0 ctermbg=12</code>)
 
*<code>echohl</code> -- This allows you output text in arbitrary colors, either predefined (eg, <code>echohl visual</code>) or customized (<code>hi myhighlight ctermfg=0 ctermbg=12</code>)
 
*<code>echon</code> -- Unlike plain <code>echo</code>, this does not insert newlines or spaces, allowing you to, for example, produce a single line with multiple colors, eg:
 
*<code>echon</code> -- Unlike plain <code>echo</code>, this does not insert newlines or spaces, allowing you to, for example, produce a single line with multiple colors, eg:
 
 
<pre>
 
<pre>
 
echohl None
 
echohl None
 
echon "xxxx"
 
echon "xxxx"
 
echohl Visual
 
echohl Visual
echon "yyyy
+
echon "yyyy"
 
"will produce xxxxyyyy
 
"will produce xxxxyyyy
  +
</pre>
  +
*<code>redr!</code> -- 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 <code>redr!</code>. 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:
  +
<pre>
  +
let &ch=&lines-1
  +
while drawing
  +
redraw! "clear screen
  +
"echo some lines
  +
endwhile
  +
let &ch=1
 
</pre>
 
</pre>
   
Line 17: Line 25:
 
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. [[User:JohnBeckett|JohnBeckett]] ([[User talk:JohnBeckett|talk]]) 09:52, August 7, 2013 (UTC)
 
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. [[User:JohnBeckett|JohnBeckett]] ([[User talk: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.
+
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. [[User:Q335r49|Q335r49]]
 
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. [[User:Q335r49|Q335r49]]

Latest revision as of 10:40, January 31, 2014

Proposed tip Please edit this page to improve it, or add your comments below (do not use the discussion page).

Please use new tips to discuss whether this page should be a permanent tip, or whether it should be merged to an existing tip.
created July 31, 2013 · complexity basic · author Q335r49 · version 7.0

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:

CommandsEdit

  • 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
  redraw! "clear screen
  "echo some lines
endwhile
let &ch=1

CommentsEdit

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