nested expr in a mapping

22 views
Skip to first unread message

M Kelly

unread,
Feb 13, 2020, 2:04:51 PM2/13/20
to vim_use
Hi,

Is it posible to nest conditionals in a mapping, something like, just for demonstration:

vnoremap <expr> vv (strlen(@y) == 1) ? ':let ...' : '("vcl" =~ getregtype("*")) ? 'some foo' : 'some bar' ' <bar> some more ...

Can we do this ?  Do I use ''' (3 single quotes) to embed quotes ?  (and then 5 for a 2nd level embed ...) ... ?

thx as always,
-m

M Kelly

unread,
Feb 13, 2020, 9:33:22 PM2/13/20
to vim_use
Hi,

I suppose I can call a function to do this :-)

-m

Gary Johnson

unread,
Feb 13, 2020, 11:22:42 PM2/13/20
to vim_use
On 2020-02-13, M Kelly wrote:
> Hi,
>
> Is it posible to nest conditionals in a mapping, something like, just for
> demonstration:
>
> vnoremap <expr> vv (strlen(@y) == 1) ? ':let ...' : '("vcl" =~ getregtype("*"))
> ? 'some foo' : 'some bar' ' <bar> some more ...
>
> Can we do this ?

Yes. See

:help 41.3
:help expr1
:help :map-<expr>
:help matchup.txt " lines 896-900 for some examples

> Do I use ''' (3 single quotes) to embed quotes ?  (and then 5
> for a 2nd level embed ...) ... ?

No. Expressions are not nested using quotes. You may use
parentheses if you wish, or just use the precedence rules.

It would be easier to help you if we knew what you had tried, the
actual results, and what you expected.

Regards,
Gary

M Kelly

unread,
Feb 14, 2020, 10:04:55 PM2/14/20
to vim_use
Hi,

ok, thank you.
I always thought there had to be quotes around each block, as in () ? 'foo' : 'bar'
So I was trying \' and \\' and \" and \\" and extra quotes for each additional nested block to no success.
I will remove all quotes and see if I can get it to work.

take care,
-m

Gary Johnson

unread,
Feb 15, 2020, 2:18:44 AM2/15/20
to vim_use
The quotes are necessary if the expression evaluates to a string and
each of the alternatives is a literal string.

Here are some simple examples.

:let x = 1
:echo x == 1 ? 73 : 88
73

:echo x == 0 ? "hello" : "goodbye"
goodbye

In the first example, the expression evaluates to a number, while in
the second example the expression evaluates to a string.

Here's an example of nesting.

:let x = 2
:echo x == 1 ? "one" : x == 2 ? "two" : "something else"
two

I deliberately didn't use any parentheses to show that they're often
not necessary, although they can make expressions more clear.

If you can't get it to work, post what you've tried here and we'll
figure it out.

Regards,
Gary

Reply all
Reply to author
Forward
0 new messages