Search code examples
vimuser-input

vimscript read input but already pass something


i want to read a input from the user. I do this that way:

let wordUnderCursor = expand("<cword>")
call inputsave()
let filePattern = input('Searchterm: ')
call inputrestore()

Now my goal is to already put something into the searchterm so that the user (me) doesnt has to write the whole searchterm. So is it possible to prepopulate the input-function with a string?

thx in advance


Solution

  • Check out :help input(); it tells you that there are optional additional arguments; the first one is exactly the default text you've been looking for:

    input({prompt} [, {text} [, {completion}]])
                [...]
      If the optional {text} argument is present and not empty, this
      is used for the default reply, as if the user typed this.
    

    For your example:

    let filePattern = input('Searchterm: ', wordUnderCursor)
    

    If you don't want the preset, you can remove it via <BS> character-by-character, or in one fell swoop with <C-u> (as in any command-line).