I wrote a moderately complex routine for Vim and I need to programmatically set the height of windows.

The way to do this is with :resize [N] as the help says.

But my invocation didn't work until I employed exec:

exe `resize `.i[1]

The following doesn't work, it always resizes to a height of 1 instead of the required i[1]:

resize i[1]

So I understand that resize takes a Number type and I am apparently feeding it a string, but I can't figure out how to avoid exec.

Maybe there's a trick I can use to use the + operator with 0? In fact the Vim doc explicitly tells us to do this.

I tried it, but it doesn't work! It works with echo but not res.

Then I tried setting the value i[1] + 0 to a variable and then using that. Indeed it seems that when I use res, it does not attempt to expand the value of variables. Really perplexing.

Is there another canonical fast way to convert an integer string into a Number? Perhaps some other thing is causing this unexpected behavior?


You can't use a variable directly with :res. Compare the help text for :res, :s, :echo and :exe:

:res[ize] -N                                    :res :resize CTRL-W_-
CTRL-W -        Decrease current window height by N (default 1).

:[range]s[ubstitute]/{pattern}/{string}/[flags] [count]
                        For each line in [range] replace a match of {pattern}
                        with {string}.
                        For the {pattern} see pattern.
                        {string} can be a literal string, or something
                        special; see sub-replace-special.

                                                        :ec :echo
:ec[ho] {expr1} ..      Echoes each {expr1}, with a space in between.  The
                        first {expr1} starts on a new line.
                                                        :exe :execute
:exe[cute] {expr1} ..   Executes the string that results from the evaluation
                        of {expr1} as an Ex command.

The text uses expr1 for echo and exe, but nothing of the sort for the other commands. For :s, it's specially stated that the replacement can be something special:

                                        sub-replace-special :s\=
When the {string} starts with "\=" it is evaluated as an expression, see
sub-replace-expression.  You can use that for complex replacement or special

In short, unless the command is expected to be followed by an expression, don't expect variables to evaluated.

:exe is the way to go, in this case.

By the way, you don't need to use . here, the following will work the same:

:exe 'resize' i[1]

exe implicitly joins arguments with spaces. It's when you don't want the sapce that you should use . with exe arguments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.