yuex
yuex

Reputation: 23

Is it possible to do something like `:normal :` to enter command line during the execution of a vimscript function?

I am writing a vim script function. During its execution, I want to enter the command line to provide some arguments that cannot be decided in advance, for some specific commands. I want something like

:startinsert

But should goes like

:startcmd

Is it possible? Or some other ways around?

Upvotes: 1

Views: 347

Answers (2)

yuex
yuex

Reputation: 23

There is a way around, we can use following code to mimic the command line.

exec input(prompt, text, completion)

text and completion are optional, :h input() for more

But one thing to note:

the built-in completions of vim (:h command-completion for more) indicated by the completion argument complete with entire preceding line before the cursor when you hit <tab> in input(). This may be not what you want, e.g., I just want to complete the last word instead of the entire preceding line.

To solve this problem, you have to write your own completion function, please refer to :h command-completion-custom and :h command-completion-customlist

Upvotes: 1

romainl
romainl

Reputation: 196476

input() is what you want.

Here is an example:

let myFile = input("Choose a file: ", "", "file")
execute 'edit ' . myFile

and another one:

buffer `=input("Choose a buffer: ", "", "buffer")`

See :help input().


You can also allow your user to choose from a predefined set of options with inputlist().

Upvotes: 1

Related Questions