Vim Tips Wiki
Register
(More sophisticated script example)
No edit summary
Line 67: Line 67:
 
</pre>
 
</pre>
   
Here, <code>KeyPrinter()</code> will echo what key your pressed based on the dictionary <code>KPDict</code>. Some explanation:
+
<code>KeyPrinter()</code> will echo what key your pressed based on the dictionary <code>KPDict</code>. Some explanation:
  +
*The user sets <code>g:GC_ProcChar</code> and <code>g:GC_ProcEsc</code> and then calls <code>GetChar()</code>.
 
*<code>feedkeys(...)[17]</code> is just a cute way to produce an empty string, since <code>0[17]</code> is an empty string.
 
*<code>feedkeys(...)[17]</code> is just a cute way to produce an empty string, since <code>0[17]</code> is an empty string.
 
*In <code>KeyPrinterHandler</code>, there is a loop that gets the full keypress. For example, the F1 key is actually ^[OP, but ^[ is eaten up by our mapping, so we would need to check whether any keypresses have been buffered.
 
*In <code>KeyPrinterHandler</code>, there is a loop that gets the full keypress. For example, the F1 key is actually ^[OP, but ^[ is eaten up by our mapping, so we would need to check whether any keypresses have been buffered.

Revision as of 14:42, 31 January 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 January 30, 2014 · complexity basic · author Q335r49 · version 7.0

A Vim script can wait for user input with getchar(). But the annoying thing about getchar() is that it shifts the cursor to the command line while the user waits. This can be disorienting when trying to write a script that captures and processes all keys in input mode, or when one simply wants the cursor to remain in the same spot in normal mode. This tip provides a workaround.

Basic script

The most basic form is something like this:

imap <F8><Esc> <Nop>
imap <F8> <c-r>=PrintChar()<cr>

function! PrintChar()
  let chr=""
  if getchar(1)
    let chr = getchar()
  endif
  call feedkeys("\<F8>")
  echo chr
  return ''
endfunction

When the user presses F8, the above script simply echos the keypresses until the user presses Escape. Every time PrintChar() is called, it uses feedkeys() to reactivate itself, setting up an input loop.

The first line, imap <F8><Esc> <Nop> serves two functions. First, it allows a way to break the loop. The second is more subtle: it makes the F8 mapping ambiguous, which will cause Vim to pause momentarily to wait for the next keystroke, since it is unsure which mapping the user wants to activate. This is a clever way to slow down the loop while still leaving Vim responsive for user input.

More complex forms

As always, these scripts can get rather complex if you're trying to account for all of Vim's quirks and nuances. For example, you might notice that F8 can really be any key. Here is what I came up with as a more general purpose key handler.

nmap <silent> <plug>GC<esc> :exe g:GC_ProcEsc<cr>
nmap <silent> <plug>GC :call GetChar()<cr>
fun! GetChar()
	exe getchar(1)? g:GC_ProcChar : feedkeys("\<plug>GC")[17]
endfun

fun! KeyPrinter()
	let g:GC_ProcChar="call KeyPrinterHandler(getchar())"
	let g:GC_ProcEsc="call KeyPrinterHandler(27)"
	call GetChar()
endfun
fun! KeyPrinterHandler(c)
	let k=type(a:c)==0? nr2char(a:c) : a:c
	let c=getchar(0)
	while c isnot 0
		let k.=type(c)==0? nr2char(c) : c
		let c=getchar(0)
	endwhile
	exe get(g:KPDict,k,g:KPDict.default)
endfun

let KPDict={}
let KPDict.a='ec "You pressed a"'
let KPDict.b='ec "You pressed b"'
let KPDict.default='ec "The key you pressed isn''t handled"'
let KPDict["\eOP"]='ec "You pressed F1"'
let KPDict["\ek"]='ec "You alt-k"'

KeyPrinter() will echo what key your pressed based on the dictionary KPDict. Some explanation:

  • The user sets g:GC_ProcChar and g:GC_ProcEsc and then calls GetChar().
  • feedkeys(...)[17] is just a cute way to produce an empty string, since 0[17] is an empty string.
  • In KeyPrinterHandler, there is a loop that gets the full keypress. For example, the F1 key is actually ^[OP, but ^[ is eaten up by our mapping, so we would need to check whether any keypresses have been buffered.

See also

  • vim_use mailing list original tip from Andy Wokula; has a 'typewriter mode' that intercepts all user input

Comments