0

How do I combine mapped commands into another mapping?

For example, I have two mapped commands, each of which enters a bracketed string at the end of the current line, and then leaves the cursor on the line following:

nmap <leader>wtf A<Tab>(wtf!?)<CR><ESC>l
nmap <leader>eh A<Tab>(eh?)<CR><ESC>l

Each of them work as expected individually.

When I reuse them both in another mapping, the new command executes the first but not the second:

nmap <leader>c1 \wtf\eh

I tried using <leader> at the head of each sub-command. It again, executes the first and not the second.

nmap <leader>c2 <leader>wtf<leader>eh

I tried also separating the sub-commands with a <bar> operator in the new mapping. That does the same thing: executes the first but not the second.

nmap <leader>c3 \wtf<bar>\eh

A regular bar operator generates an error upon its definition (E10: \ should be followed by /, ? or &):

nmap <leader>c4 \wtf | \eh

How can I combine these simple commands by invocation in another mapping?

1 Answer 1

1

Both:

nmap <leader>c1 \wtf\eh

and:

nmap <leader>c2 <leader>wtf<leader>eh

are correct. Each comes with its own set of issues (mostly related to the <leader> mechanism) but they are fine as far as the problem at hand goes.

The problem is not in the way you combine those mapping together, but in your original mappings themselves:

nmap <leader>wtf A<Tab>(wtf!?)<CR><ESC>l
nmap <leader>eh A<Tab>(eh?)<CR><ESC>l

which could just as well be simplified to:

nmap xx Ax<Esc>l
nmap yy Ay<Esc>l

Both end with l, for "move the cursor one character to the right", but the cursor is already on the last character of the line so the cursor can't be moved. l is thus an error, which prevents the rest of the mapping to be executed. See :help map-error.

Those ls serve no purpose so you can remove them to fix your problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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