12

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
1
  • I really like the functionality this bit of code provides. It essentially lets you create a new vim mode, for instance you could call the code above a "window-mode" just like visual-mode or insert-mode. Pretty powerful stuff... Commented Nov 19, 2010 at 5:16

1 Answer 1

12

getchar()

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

Sign up to request clarification or add additional context in comments.

2 Comments

@stephen (and anyone else) -- I'm curious how you're presenting the prompt to the user for input that can be captured with getchar(). Maybe I'm dense but it's not jumping out at me.
I actually haven't implemented it yet (and why I did not mark it as correct yet) but from looking around it seems like getchar will work for me. I am hoping to get a change to implement it later tonight, so I will let you know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.