简体   繁体   中英

What is the meaning of a <CR> at the end of some vim mappings?

我看<cr> sa在VIM映射很多,但它有什么作用?

:help key-notation

says:

notation    meaning            equivalent  decimal    value(s)
-----------------------------------------------------------------------
<CR>        carriage return        CTRL-M    13       *carriage-return*
<Return>    same as <CR>                              *<Return>*
<Enter>     same as <CR>                              *<Enter>*

Mappings often involve Ex commands and you must press <CR> to execute them so it's included in the mapping.

Why <special keys> ?

While you can use literal keys in mapping definitions (the Enter key would appear as ^M , or even just as an additional new line, depending on the settings), Vim provides a special key notation for key (combinations), so that it is easier to define (you don't have to use i_CTRL-V to literally insert the special character) and understand ( <Aa> better expresses the intention than the equivalent á ) the mappings.

See :help key-notation for a list and explanation.

Why <CR> ?

As many mappings invoke Ex commands (eg :w ) and therefore have to switch from normal to command-line mode, they have to conclude the command with <Enter> (or <CR> ), just as you would when manually typing the command.

vim映射中的<CR>回车 ,通常是键盘上的Enter键。

<CR> in a mapping corresponds to the Enter key just like a in a mapping corresponds to the A key. Ley's say you have this mapping

:map <f8> :wq<cr>

This will map F8 to the key sequence : W Q Enter (which would save the current buffer and quit).

It's basically a means to say "this is the end", see map :

When you have a mapping that contains an Ex command, you need to put a line terminator after it to have it executed. The use of is recommended for this. Example:

:map _ls :!ls -l %<CR>:echo "the end"<CR>

also,

<CR>    [count] lines downward, on the first non-blank character |linewise|.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM