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

I see <cr>s a lot in vim mappings, but what does it do?


Solution 1:

: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.

Solution 2:

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 (<A-a> 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 (e.g. :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.