3

I recently found the awesome plugin named CamelCaseMotion which is awesome. I used their suggested mappings in my .vimrc for the plugin like this:

map <silent> w <Plug>CamelCaseMotion_w
sunmap w
omap <silent> iw <Plug>CamelCaseMotion_iw
xmap <silent> iw <Plug>CamelCaseMotion_iw

to map w but also b, e, ge to their respective functions. The problem is now that I want to map W, B, E to work like w, b, e worked before and <leader>w, <leader>b, <leader>e to work like W, B, E. Also I want to remap gE and g<leader>e in normal mode using the same rules above. This is where my vimscript-fu and google-fu failed me. I can't use map <silent> W w because I will override w, and I can't find the function to which w is mapped originally/natively by vim. Can someone help please? Also you please explain how can I find mappings for native vim keys in the future?

Edit: :map does not help me. It does not show the native mappings for w or W motion.

:verbose map W
No mapping found
9
  • Hi. Please take a look at the answers on the page linked in the previous comment. Sounds like you are having problems with mapping to mapped commands. That page will help.
    – B Layer
    Commented Aug 13, 2019 at 13:17
  • :map does not have the native vim mappings for w and W. I have searched, believe me. Commented Aug 13, 2019 at 13:18
  • @RevanDarth To map the native vim keys, e.g w, you can use :normal! w<CR>. Also consider using nnoremap familly instead of nmap which can go recursive.
    – perelo
    Commented Aug 13, 2019 at 13:40
  • I understand what you're trying to do. That's why I linked that page. (And take note of the second thing perelo said.)
    – B Layer
    Commented Aug 13, 2019 at 13:50
  • 1
    @b-layer Now I understand what you meant. noremap W w worked! It was not overriden. Thanks a million! Commented Aug 13, 2019 at 14:02

1 Answer 1

2

You should simply use non-recursive mappings, which allow you to access the original command under the keys, regardless of them having been mapped to something else.

For example, for w, W and <leader>w:

map <silent> w <Plug>CamelCaseMotion_w
noremap W w
noremap <leader>w W

(And possibly use the sunmaps that you had as well.)

The first one can't use a non-recursive mapping, because <Plug> is a "virtual" keystroke and you're actually invoking a mapping, so you need it to be recursive. But that's fine. It's an exception and one of the few cases where you want a non-recursive mapping.

For most other cases, you will almost always want a non-recursive mapping. Use non-recursive mappings by default, unless you really know you need a recursive one.

For more details and a great discussion on them, see "Nonrecursive Mappings" on "Learn Vimscript the Hard Way", which demonstrates why they're important and why you always want to use them.

Also see the excellent answer to "How to debug a mapping?", which has many pointers on how you can learn to debug this by yourself. (In fact, the answer to your particular question, using non-recursive mappings, is listed there under best practices.)

5
  • 1
    I marked the question as a duplicate some time ago. That's why I put the answer (same as you have here) in comments. Generally I don't think it's a good practice to write an answer while duplicate disposition is still up in the air. (I would have answered early on if it were otherwise.) Sloooow down, man.
    – B Layer
    Commented Aug 13, 2019 at 17:00
  • @BLayer Ah sorry, didn't notice that, didn't really look at the comments before I started typing the answer... Will keep an eye on it going forward.
    – filbranden
    Commented Aug 13, 2019 at 17:03
  • 1
    hi filbranden, thank you for explaining things in detail and for the awesome references. Given that my question really was a duplicate I upvoted your answer. Commented Aug 13, 2019 at 17:54
  • @RevanDarth Basically everything in this answer is in the page I linked including that book chapter.
    – B Layer
    Commented Aug 13, 2019 at 18:09
  • 1
    @D.BenKnoble Good call! Added a reference to that answer. I agree that, even though the information is there, for the OP to find it there would have been quite daunting. (If they knew what they were after, they would probably have found it already.) I also don't see much harm in having an answer and marking the question as a duplicate, since that usually also counts as a "see also" in cases where a potential asker's problem is similar but not exactly the same.
    – filbranden
    Commented Aug 13, 2019 at 21:29

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