15

In this answer I faced the CTRL-U used in function call after : and before the actual function name.

:nnoremap <buffer> <cr> :<C-U>call append('.', repeat([''],v:count1))<cr>

Here is what help says:

CTRL-U      Scroll window Upwards in the buffer.  The number of
            lines comes from the scroll option (default: half a
            screen).  If [count] given, first set the 'scroll'
            option to [count].

I tried it myself and it worked as I supposed. But I didn't quite understand the part about the scroll option. What do they mean 'scroll' option?

And besides it does some strange thing in insert mode. As far as I understood it deletes everything from the cursor to the beginning of the line and then joins the resulting line and the line above.

23

Help, by default, returns any mappings that apply to normal mode. In normal mode, <C-u> does scroll upwards, but that's not what we're interested in. To see what <C-u> does in command line mode, or cmode, (the mode where you type commands starting with a colon) search :h c_CTRL-u

                            *c_CTRL-U*
CTRL-U      Remove all characters between the cursor position and
        the beginning of the line.  Previous versions of vim
        deleted all characters on the line.  If that is the
        preferred behavior, add the following to your .vimrc: >
            :cnoremap <C-U> <C-E><C-U>

To understand why this is useful for a normal mode mapping, try removing it, try typing something like 5:, and see what comes up. You should see something like:

:.,.+4

This is a range

Now try typing 5:<C-u> and see what comes up. You should just see

:

Similarly in visual mode, pressing : inserts the range

:'<,'>

That's why you'll very frequently see mappings like:

nnoremap foo :<C-u>bar

or

xnoremap foo :<C-u>bar
  • Maybe worth noting the reason it's common in visual mode mappings is that in visual mode it inserts :'<,'>. – Random832 Sep 30 '16 at 14:13
  • @Random832 Ah, good point. Thanks, I've edited that in now. – DJMcMayhem Sep 30 '16 at 15:51

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.