1

If you execute this

cnoremap <expr> <silent> <C-S> execute(':cnoremap <c-s> ctrl-s')

and then hit /Ctrl-sCtrl-sCtrl-sCtrl-s, you'll see the search command be populated by /ctrl-sctrl-sctrl-s.

Good.

But what's special in <space> not to work like that?

After executing this

cnoremap <expr> <silent> <C-S> execute(':cnoremap <space> space')

as soon as I hit /Ctrl-s, I get E385: Search hit BOTTOM without match for: ^S.

To get the thing to work, I have to alter the command above to look like this

cnoremap <expr> <silent> <C-S> execute(':cnoremap <lt>space> space')

Why is that?

Enlico
  • 2,194
  • 15
  • 31

1 Answers1

1

A map command substitutes all "angle brackets" inside. Really all of them.

Therefore, the second map gets either ctrl-s as byte#19 (which is ok) or space as byte#32 (which is a literal space to eat). And then some cryptic errors appear for the second case only.

There could be different solutions but the most straightforward one is always to use <lt> instead of < inside nested maps. This way all nested expansions will be performed in two steps.

Matt
  • 20,685
  • 1
  • 11
  • 24
  • At which point does ':cnoremap <space> space' become ':cnoremap space'? In the context of cnoremap? Or in the context of execute? Or what? – Enlico Oct 21 '22 at 18:54
  • Well, from the first paragraph of your answer, I understand it happens as soon as the mapping is created. – Enlico Oct 21 '22 at 18:55
  • Mmm. There's still something I don't understand. Paradoxically, it is maybe why does ':cnoremap <c-s> ctrl-s' work? I mean, I don't understand what ctrl-s as byte#19 (which is ok) means. – Enlico Oct 21 '22 at 18:57
  • @Enlico Exactly. Every map expands everything it can. Just imagine how would you program it in the most straightforward way: never check anything except "less-than" and "greater than". – Matt Oct 21 '22 at 18:59
  • @Enlico As I mentioned above, c-s is byte#19. And it is okay. That is, you can put "control codes" right inside vim source and they will work (well, sometimes in some specific places, of course). – Matt Oct 21 '22 at 19:01
  • FYI, I asked this in the process of giving this answer. – Enlico Oct 21 '22 at 19:41