Vimscript: Function with user input and a variable

input() gets user input. Its parameter is the prompt.

We can put it in a variable: let some_input.

:exec makes a function by concatenating strings. A space adds two strings together with a space in-between. An . adds it without the space.

function MyFunc()
  let some_input  = input("City? ")
  normal AI've always loved
  exec ":normal A" some_input . ". "
  normal AIt is a wonderful city.
endfunction

Then map it using map cq :call MyFunc()^M. (You get ^M with ctrl-v <enter>)

Typing cq in normal mode can then result in : I've always loved Mordor. It is a wonderful city.

vimscript

Edit on github
comments powered by Disqus
Click me