Skip to content

Instantly share code, notes, and snippets.

@RRethy
Last active January 4, 2019 00:06
Show Gist options
  • Save RRethy/599cfc801b802c141ac9cb233d49c8a2 to your computer and use it in GitHub Desktop.
Save RRethy/599cfc801b802c141ac9cb233d49c8a2 to your computer and use it in GitHub Desktop.
React to user input as it is typed
fun! QueryInput() abort
" Set up the autocmd to react to the user typing
augroup textwatcher
autocmd!
autocmd CmdlineChanged * call s:on_text_changed()
augroup END
" Request input from the user
let search = input('Input: ')
" Tear down the autocmd which reacts to user typing
augroup textwatcher
autocmd!
augroup END
" Handle the user input
if search !=# ''
" User entered some text which is stored in the variable `search`
call setline(2, '" Got input from user: ' . search)
else
" User either did not enter any input or they cancelled
call setline(2, '" No input from user')
endif
endf
" This gets called when the text the user is typing has changed
fun! s:on_text_changed() abort
" get currently typed input with `getcmdline()`
" operate on that text
call setline(1, '" ' . getcmdline())
" Must redraw the screen since `input()` will only redraw the cmdline
" This is only needed if you make changes to the UI of Vim
redraw
endf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment