Skip to main content
Module

x/denops_std/helper/mod.ts>input

📚 Standard module for denops.vim
Go to Latest
function input
import { input } from "https://deno.land/x/denops_std@v6.3.0/helper/mod.ts";

Open a prompt to get user input.

It is a wrapper function of Vim/Neovim's native input() function. This version has the following advantages

  • Developers can use TypeScript custom completion function
  • It returns null instead when user cancelled by or
  • It automatically guard input when inputsave option is specified
import type { Denops } from "https://deno.land/x/denops_std@v6.3.0/mod.ts";
import { input } from "https://deno.land/x/denops_std@v6.3.0/helper/input.ts";

export async function main(denops: Denops): Promise<void> {
  console.log(
    await input(denops, {
      prompt: "> ",
      text: "This is default value",
    }),
  );
}

Not like native input() function, it returns null instead of an empty string when user press <Esc> or <C-c> so that developers can distinguish if user cancelled or input an empty string.

It accepts a TypeScript callback as a completion function addition to built-in completions and Vim script custom completion like:

import type { Denops } from "https://deno.land/x/denops_std@v6.3.0/mod.ts";
import { input } from "https://deno.land/x/denops_std@v6.3.0/helper/input.ts";

export async function main(denops: Denops): Promise<void> {
  // Built-in completions
  console.log(
    await input(denops, {
      prompt: "> ",
      text: "This is default value",
      completion: "command",
    }),
  );

  // Vim script custom completion (assume 'MyVimScriptCompletion' is defined in Vim script)
  console.log(
    await input(denops, {
      prompt: "> ",
      text: "This is default value",
      completion: "custom,MyVimScriptCompletion",
    }),
  );

  // TypeScript custom completion
  console.log(
    await input(denops, {
      prompt: "> ",
      text: "This is default value",
      completion: (
        arglead: string,
        cmdline: string,
        cursorpos: number,
      ): Promise<string[]> => {
        return Promise.resolve(["Hello", "World"]);
      },
    }),
  );
}

If you'd like to guard input by inputsave() and inputrestore(), use inputsave option like:

import type { Denops } from "https://deno.land/x/denops_std@v6.3.0/mod.ts";
import { input } from "https://deno.land/x/denops_std@v6.3.0/helper/input.ts";

export async function main(denops: Denops): Promise<void> {
  denops.dispatcher = {
    say: async () => {
      return await input(denops, {
        prompt: "> ",
        inputsave: true,
      });
    },
  };
}

Parameters

denops: Denops
optional
options: InputOptions = [UNSUPPORTED]

Returns

Promise<string | null>