How can one use a variable when executing something via
normal! in vimscript?
function! MyFunction(someArg) normal! (a:someArg)l endfunction
This should move the cursor
someArg steps to the right.
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.Sign up to join this community
execute function takes a string as argument, it expands the string and execute it a a regular ex command. Thus you can do:
function! MyFunction(someArg) execute "normal! ". a:someArg. "l" endfunction
. is a standard vimscript operator to concatenate strings.
execute can be shortened to
EDIT I'll add a point about the comment of @Christian Brabandt about the concatenation with a
The doc says:
Multiple arguments are concatenated, with a space in between. To avoid the extra space use the "." operator to concatenate strings into one argument.
So the command could be:
execute "normal!" a:someArg . "l"
The executed command will be:
For completeness's sake, I find the string concatenation using
. is not pleasant to read. We may use
printf to concatenate strings and variables for better readability.
execute printf("normal! %sl", a:someArg)