Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do some Vim mappings include <C-U> after a colon?

Tags:

syntax

vim

I'm trying to figure out the syntax of the mapping commands, like onoremap, in vim.

Specially, I am confused over this line in the manual, regarding the use of <C-U>:

The CTRL-U (<C-U>) is used to remove the range that Vim may insert.

Can someone explain this?

like image 799
Kevin Avatar asked Dec 12 '12 00:12

Kevin


People also ask

What is CU in Vim?

The <C-U> in the mapping tells vim that after the : is entered the Control + U combination should be used to clear the command line, eliminating the automatically inserted range leaving the command line looking like: : Then the remainder of the mapping is used.

What does colon do in Vim?

The colon is simply used to change modes. Show activity on this post. In Normal mode you don't require to type : , this mode can be reached by pressing Esc.

What is mapping in vim?

Introduction. Key mapping refers to creating a shortcut for repeating a sequence of keys or commands. You can map keys to execute frequently used key sequences or to invoke an Ex command or to invoke a Vim function or to invoke external commands. Using key maps you can define your own Vim commands.

What is operator pending mode in Vim?

After typing an operator command, Vim will wait for a motion. For example, typing yw when in Normal mode will yank the word under the cursor: y is the operator command and w is the motion. The operator is said to be pending in the time between typing the operator command and the motion.


1 Answers

That isn't part of the syntax for the onoremap command, that is explaining what a particular mapping does. That mapping is:

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

So, when the F key is used while an operator is pending vim will replace that with the bits in the next argument to the onoremap command. That starts with a : to begin an ex mode command. If there is a visual selection when the mapping is used, vim will automatically insert the range '<,'> so that the following ex command will apply to the visual selection, leaving the command line looking like:

:'<,'> 

The <C-U> in the mapping tells vim that after the : is entered the Control+U combination should be used to clear the command line, eliminating the automatically inserted range leaving the command line looking like:

: 

Then the remainder of the mapping is used.

You can see this for yourself by using V to begin a line-wise visual selection, then : to start entering a command. The range will show up, you can then use Control+U to clear it just as the example mapping does.

The portion of vim help that contains that mapping explains the remainder of it.

like image 87
qqx Avatar answered Sep 25 '22 07:09

qqx