1
nnoremap <leader>td $F)i, args2<ESC>

# base string
test ----> (args1)

# expected string after <leader>td
test ----> (args1, args2)

# real result string 
test ----> (args1)

The above are the key-map and what I expect with the key-map. However, the result is that the cursor is stopped the end of the string not entering insert mode. When I simulate the key-map, typing keys in the <leader>td by hand, it works.

In addition, The more wired thing is the below. After appending a character at the end of the string, It works as expected.

# base string, I append ':' character at the end of the string
test ----> (args1):

# real result string, it works !!!?
test ----> (args1, args2):

What have I missed?

1

From :h map-error:

                                                        *map-error*
Note that when an error is encountered (that causes an error message or beep)
the rest of the mapping is not executed.  This is Vi-compatible.

In your mapping, if the F command fails to find a closing parenthesis, Vim will stop processing the rest of the {rhs}.

If you want Vim to ignore a possible error, you could use the :silent command, followed by the ! argument. From :h :sil:

When [!] is added, error messages will also be
skipped, and commands and mappings will not be aborted
when an error is detected.

Here's how you could rewrite your mapping, using the :normal command to type the keys from the command line, prefixed by :silent to ignore any error:

nno  <silent>  <leader>td  :<c-u>sil! norm! $F)i, args2<cr>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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