Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vim script "input()" function that doesn't require user to hit enter

Tags:

vim

I would like to have the user call my function and then have the function request user input but I do not want the user to have to type 'enter' after typing a letter as is required by the "input()" function. For instance, the user should be able to type single letter commands like 'h','j','k','l' and each letter typed would loop around my function until the user typed 'x' for exit. If I use "input()" then the user would have to type 'h<enter>','j<enter>'...

Any suggestions on how I might be able to do this?

If more clarification is needed please let me know.

UPDATE

Got it working:

function! s:getchar()
  let c = getchar()
  if c =~ '^\d\+$'
    let c = nr2char(c)
  endif
  return c
endfunction

" Interactively change the window size
function! InteractiveWindow()
  let char = "s"
  while char =~ '^\w$'
    echo "(InteractiveWindow) TYPE: h,j,k,l to resize or a for auto resize"
    let char = s:getchar()
    if char == "h" | call SetWindowSize( "incr" ,-5 ,0 ) | endif
    if char == "j" | call SetWindowSize( "incr"  ,0 ,5 ) | endif
    if char == "k" | call SetWindowSize( "incr"  ,0 ,-5) | endif
    if char == "l" | call SetWindowSize( "incr"  ,5 ,0 ) | endif
    if char == "a" | call SetWindowSize( "abs"  ,0  ,0 ) | endif
    redraw
  endwhile
endfunction
like image 893
stephenmm Avatar asked Nov 15 '10 21:11

stephenmm


1 Answers

getchar()

http://vimdoc.sourceforge.net/htmldoc/eval.html#getchar()

like image 100
Kamal Avatar answered Nov 14 '22 03:11

Kamal