sjl / learnvimscriptthehardway

Home Page:http://learnvimscriptthehardway.stevelosh.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Chapter 15: unnecessary <c-u>

ultcyber opened this issue · comments

In Chapter 15 Operator-Pending Mappings there is an example mapping given for deleting a text inside next parenthesis:

:onoremap in( :normal! f(vi(`

and in the next paragraph it is explained that

First, the <c-u> is something special that you can ignore for now -- just trust me that it needs to be there to make the mappings work in all cases

Actually, in this case the <c-u> does not serve any purpose. The key combination is used to remove everything from the command line, but in this case there will be nothing anyway.

I think the author took suggestion from the example of VIM documentation:

onoremap <silent> F :<C-U>normal! 0f(hviw<CR>

But in this case, the <c-u> is indeed necessary, as by F gets VIM into the visual mode and ":" will get it into the visual mode command selection (:'<.>'), so the key combination is used to clear that out.

So it does not need to be there as the author suggests.

@ultcyber I've also stumbled upon reading this example from the book, but even the case from the Vim docs didn't seem to justify the need for <c-u> in the oremap mapping. This led to the StackExchange question "Is really necessary in operator-pending mode mappings?".

Would you mind clarifying how were you see the need for <c-u> in the Vim docs example? You said

by F gets VIM into the visual mode

but F is simply the key being mapped. The earliest instance of visual mode used here seems to occur at v, which is after command-line mode is entered with :.

@mxxk I've revisited the question and I think you're right - even in the vim help example it doesn't seem like the <c-u> is necessary because, as you said:

but F is simply the key being mapped. The earliest instance of visual mode used here seems to occur at v, which is after command-line mode is entered with :.

In this case, I don't know for sure the real answer to why it seems like <c-u> is always needed in operator-pending mappings. What I suppose could be the reason is to block the commands from being operated on a range. Taking the example from vim help, where you onoremap F to removing the function name - if you select a visual range and try to do :'<.>'norm!dF it will not work. If you don't use the <c-u> when mapping, it will delete everything from found word to next one, so doesn't work in a proper way. Maybe the <c-u> is to disable that option entirely.

It's just my wild guess...