Search code examples
vimvim-pluginneovimctrlp

Get user input after opening window


How do I get user input (so that I can filter some items) after opening a new window (which has the items)? I wanted something similar to CtrlP or CtrlSpace. I tried to look at their code, but failed to identify where they do it.

The way I am calling it is by creating a command that calls a function foo that opens the window and waits for an user input.

I have already tried some variations (code inside foo):

botright split NewWindow
let input = input('>> ')

and

botright split NewWindow
redraw!
let input = input('>> ')

The first one always get me the input before opening the new window. In both of them the >> part doesn't show up (neither does the input itself). How do they do this? With a bunch of getchar calls?

(using Neovim v0.2.0)


Solution

  • Ok. It seems it was not that difficult (for some reason I was lost in the source code and couldn't see it). They basically get one char at a time and keep echoing the results.

    From what I could see, CtrlP uses the following approach:

    • redraw
    • ask for character with getchar()
    • concatenate the character with an accumulator string
    • echo the string
    • loop while you want

    My vimscript now looks like this:

    let str = ''
    while s:some_stop_condition()
        redraw
        let c = getchar()
        let str = str . nr2char(c)
        echo str
    endwhile