syntax for "map <expr> key test?'cmd1':'cmd2'|:cmd3|:cmd4"

13 views
Skip to first unread message

dado

unread,
Apr 26, 2022, 5:32:00 PM4/26/22
to vim_use
I have the following (pseudo code) mapping working:
nmap <expr> key some-expr ?
  \ 'cmd1<Bar>:cmd3<CR><Bar>:cmd4<CR>' :
  \ 'cmd2<Bar>:cmd3<CR><Bar>:cmd4<CR>'

As you can see the conditional selects between cmd1 and cmd2,  but both execute cmd3 and cmd4.
I'd like to move cmd3 and cmd4 outside of the conditional as in
nmap <expr> key some-expr?'cmd1':'cmd2'<Bar>:cmd3<CR><Bar>:cmd4<CR>
The problem is I get cmd1 or cmd2 to execute, but never cmd3 or cmd4.
I know I could move this to a function but I'd like to understand the syntax.

Thank you

Christian Brabandt

unread,
Apr 27, 2022, 4:02:19 AM4/27/22
to vim_use
Try something like the following:
nmap <expr> (key some-expr ? 'cmd1<Bar>' : 'cmd2<Bar>') .. ':cmd3<CR><Bar>:cmd4<CR>'

Note sure about all the `<Bar>`s and `<CR>`s :)


Best,
Christian
--
Der raffinierteste Druck ist manchmal der Händedruck.

Jürgen Krämer

unread,
Apr 27, 2022, 4:39:23 AM4/27/22
to vim...@googlegroups.com
Hi,
:nmap <expr> <F8> ( &list ? ':echomsg 1<Bar>' : ':echomsg 2<Bar>' ) .. 'echomsg 3<Bar>echomsg 4<CR>'

works. If 'list' is set, pressing <F8> will output

1
3
4

If 'list' is not set, pressing <F8> will output

2
3
4

Note that there is no need for a colon after <Bar> and no need for <CR> before <Bar>.

Regards,

--
~
~
~
:wq

dado

unread,
Apr 28, 2022, 8:24:19 PM4/28/22
to vim_use
Got it to work.
Thank you all!
Reply all
Reply to author
Forward
0 new messages