question about expression mappings

62 views
Skip to first unread message

Christian Brabandt

unread,
Jan 8, 2016, 4:54:04 PM1/8/16
to vim...@vim.org
Hi,
I think I found an inconsistency.

#v+
vim -u NONE -N
:let @a='foobar'
:fu AB()
return "<c-r>a"
:endfu
:fu CD()
return "\<c-r>a"
:endfu
:imap <expr> ab '<c-r>a'
:imap <expr> cd AB()
:imap <expr> ef CD()
#v-
Now enter insert mode:

1) Inserting ab results in:
foobar
2) Inserting cd results in:
<c-r>a
3) Inserting ef results in:
foobar

That is unexpected. I think the first case, it should also insert
'<c-r>a' Since according to the documentation (:h expr-quote) keys need
to be returned like "\<key>" in a double quoted string.

How would one return the literal string "<c-r>a" in the first case?
Is this a bug or did I miss anything?

Best,
Christian
--
Alles Gute kommt nach oben.

Nikolay Aleksandrovich Pavlov

unread,
Jan 8, 2016, 5:28:03 PM1/8/16
to vim_dev, vim-dev Mailingliste
​You miss how maps are parsed. When filling mappings table (i.e. at the time :*map commands are parsed themselves) rhs always has special keys translated, regardless whether it is <expr> or not.​ And only then, when you type something, this is treated as expression. This way allows `:inoremap <expr> foo <SID>Func()` to work: `<SID>` must be translated before mapping is run because mapping is run in the currently effective script context and not in the context of the script it was defined in (unlike :command does).

To have literal string you need to escape `<` just as you will do in case of other mappings: `'<LT>c-r>a'`. Or you may abstract this away in a function or global variable.

 

Best,
Christian
--
Alles Gute kommt nach oben.

--
--
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Christian Brabandt

unread,
Jan 9, 2016, 11:11:51 AM1/9/16
to vim_dev, vim-dev Mailingliste
Hi Nikolay!
Ah thanks. I think this should be documented.

Best,
Christian
--
Was eine Nation groß macht, sind nicht in erster Linie die großen
Männer. Es ist das Format der Mittelmäßigen.
-- José Ortega y Gasset
Reply all
Reply to author
Forward
0 new messages