Pass variable to sleep in vimscript

I've been using vim for too many years to count, but I never really knew very well about vimscript. I'm trying now.

Anyway, I would like to pass a variable amount of time to the sleep function. I also want to manipulate this value before passing it in. Here's a simple example.

function! wait(mil)
    let timetowait = mil . "m"
    sleep timetowait
endfunction

      

Even if I try the timetowait prefix with l: it says "Invalid argument: l: timetowait".

What's the correct way to pass the value of a variable to sleep?

+3


source to share


2 answers


There are several problems:

  • Your method must start with a name with a capital letter
  • You need to access your argument with a:

  • You must have space between the waiting time and m

  • You have to sleep indirectly using execute



Here's an example of how you can do this:

function! Wait(mil)
    let timetowait = a:mil . " m"
    exe 'sleep '.timetowait
endfunction 

      

+5


source


Daan's answer is correct; here's some more reference data:

Vimscript evaluates exactly the same as Ex commands entered on the command line :

. There ex

were no variables in the variables, so do not specify them. When entering the command interactively, you probably use <C-R>=

to insert the contents of a variable:

:sleep <C-R>=timetowait<CR>m<CR>

      



... but in a script, :execute

you must use. All literal parts of the Ex command must be quoted (single or double quotes) and then concatenated with variables:

execute 'sleep' timetowait . 'm'

      

+3


source







All Articles