简体   繁体   English

Vim 中的 remap、noremap、nnoremap 和 vnoremap 映射命令有什么区别?

[英]What is the difference between the remap, noremap, nnoremap and vnoremap mapping commands in Vim?

Vim 中的remapnoremapnnoremapvnoremap映射命令有什么区别?

remap is an option that makes mappings work recursively. remap是一种使映射递归工作的选项 By default it is on and I'd recommend you leave it that way.默认情况下它是打开的,我建议你保持这种状态。 The rest are mapping commands , described below:其余的是映射命令,描述如下:

:map and :noremap are recursive and non-recursive versions of the various mapping commands. :map:noremap是各种映射命令的递归非递归版本。 For example, if we run:例如,如果我们运行:

:map j gg           (moves cursor to first line)
:map Q j            (moves cursor to first line)
:noremap W j        (moves cursor down one line)

Then:然后:

  • j will be mapped to gg . j将被映射到gg
  • Q will also be mapped to gg , because j will be expanded for the recursive mapping. Q将映射到gg ,因为j将针对递归映射进行扩展。
  • W will be mapped to j (and not to gg ) because j will not be expanded for the non-recursive mapping. W将被映射到j (而不是gg ),因为j不会为非递归映射扩展。

Now remember that Vim is a modal editor .现在请记住,Vim 是一个模态编辑器 It has a normal mode, visual mode and other modes.它有普通模式、视觉模式和其他模式。

For each of these sets of mappings, there is a mapping that works in normal, visual, select and operator modes ( :map and :noremap ), one that works in normal mode ( :nmap and :nnoremap ), one in visual mode ( :vmap and :vnoremap ) and so on.对于每个这些映射存在的,有一种映射,在正常的,视觉作品,选择和操作员模式( :map:noremap ),一种在正常模式下工作( :nmap:nnoremap ),一个在可视模式( :vmap:vnoremap ) 等等。

For more guidance on this, see:有关这方面的更多指导,请参阅:

:help :map
:help :noremap
:help recursive_mapping
:help :map-modes

I think the Vim documentation should've explained the meaning behind the naming of these commands.我认为 Vim 文档应该已经解释了这些命令命名背后的含义。 Just telling you what they do doesn't help you remember the names.只是告诉你他们做什么并不能帮助你记住名字。

map is the "root" of all recursive mapping commands. map是所有递归映射命令的“根”。 The root form applies to "normal", "visual+select", and "operator-pending" modes.根形式适用于“正常”、“视觉+选择”和“操作员待定”模式。 (I'm using the term "root" as in linguistics .) (我在语言学中使用术语“根”。)

noremap is the "root" of all non-recursive mapping commands. noremap是所有非递归映射命令的“根”。 The root form applies to the same modes as map .根形式适用于与map相同的模式。 (Think of the nore prefix to mean "non-recursive".) (将nore前缀nore “非递归”。)

(Note that there are also the ! modes like map! that apply to insert & command-line.) (请注意,还有像map!这样的!模式适用于插入和命令行。)

See below for what "recursive" means in this context.请参阅下文,了解在这种情况下“递归”的含义。

Prepending a mode letter like n modify the modes the mapping works in. It can choose a subset of the list of applicable modes (eg only "visual"), or choose other modes that map wouldn't apply to (eg "insert").前面的模式字母n修改映射工作的模式。它可以选择适用模式列表的子集(例如仅“视觉”),或选择map不适用的其他模式(例如“插入”) .

Use help map-modes will show you a few tables that explain how to control which modes the mapping applies to. Use help map-modes将向您展示一些表格,解释如何控制映射适用于哪些模式。

Mode letters:模式字母:

  • n : normal only n : 仅正常
  • v : visual and select v : 视觉和选择
  • o : operator-pending o : 运营商待定
  • x : visual only x : 仅视觉
  • s : select only s : 只选择
  • i : insert i :插入
  • c : command-line c : 命令行
  • l : insert, command-line, regexp-search (and others. Collectively called "Lang-Arg" pseudo-mode) l :插入、命令行、正则表达式搜索(以及其他。统称为“Lang-Arg”伪模式)

" Recursive " means that the mapping is expanded to a result, then the result is expanded to another result, and so on. 递归”表示将映射扩展为一个结果,然后将结果扩展为另一个结果,依此类推。

The expansion stops when one of these is true:当其中之一为真时,扩展停止:

  1. the result is no longer mapped to anything else.结果不再映射到其他任何东西。
  2. a non-recursive mapping has been applied (ie the "noremap" [or one of its ilk] is the final expansion).应用了非递归映射(即“noremap”[或其同类之一] 是最终扩展)。

At that point, Vim's default "meaning" of the final result is applied/executed.在这一点上,Vim 对最终结果的默认“含义”被应用/执行。

" Non-recursive " means the mapping is only expanded once, and that result is applied/executed. 非递归”意味着映射仅扩展一次,并且该结果被应用/执行。

Example:示例:

 nmap K H
 nnoremap H G
 nnoremap G gg

The above causes K to expand to H , then H to expand to G and stop.以上导致K扩展到H ,然后H扩展到G并停止。 It stops because of the nnoremap , which expands and stops immediately.它因为nnoremap而停止,它会立即扩展并停止。 The meaning of G will be executed (ie "jump to last line"). G的意思会被执行(即“跳到最后一行”)。 At most one non-recursive mapping will ever be applied in an expansion chain (it would be the last expansion to happen).在扩展链中最多应用一个非递归映射(这将是最后一次发生的扩展)。

The mapping of G to gg only applies if you press G , but not if you press K . Ggg的映射仅在您按下G时才适用,而在您按下K则不适用。 This mapping doesn't affect pressing K regardless of whether G was mapped recursively or not, since it's line 2 that causes the expansion of K to stop, so line 3 wouldn't be used.无论G是否递归映射,此映射都不会影响按K ,因为第 2 行导致K的扩展停止,因此不会使用第 3 行。

Caution, vnoremap and vmap work in Visual AND Select mode. 注意, vnoremapvmap在Visual AND Select模式下工作。 To have a mapping only in Visual mode, use xmap and xnoremap . 要仅在可视模式下进行映射,请使用xmapxnoremap

One difference is that:一个区别是:

  • :map does nvo == normal + (visual + select) + operator pending :mapnvo == normal + (visual + select) + operator pending
  • :map! does ic == insert + command-line mode ic == 插入 + 命令行模式

as stated on help map-modes tables.help map-modes表所述。

So: map does not map to all modes .所以: map不会映射到所有模式

To map to all modes you need both :map and :map!要映射到所有模式,您需要:map:map! . .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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