vim:映射一个键输入映射命令

vim: map a key to entering a mapping command

在vim中,我想使用热键输入映射命令,映射命令又将热键映射到一串命令。这是我正在做的事情:

nnoremap <leader>r :map `t :w<cr>:silent !make<cr>:redraw!<cr>

如你所见,当我按下<leader>r时,vim会将映射命令放在命令行上,我可以修改实际命令(在本例中为make)然后按回车键创建映射。

现在,这实际上不起作用,因为 <cr> 将应用于 nnoremap 命令。我如何转义它们并让它们显示在命令行上以便它们应用于 :map 命令?

要按字面意义将 <...> 添加到映射中,您需要使用 <lt> 对文件 < 进行转义。所以 <cr> 会变成 <lt>cr>

所以你的映射看起来像

nnoremap <leader>r :map `t :w<lt>cr>:silent !make<lt>cr>:redraw!<lt>cr>

这在帮助中作为 :h command-bang

的小节介绍
Replacement text

The replacement text for a user defined command is scanned for special escape
sequences, using <...> notation.  Escape sequences are replaced with values
from the entered command line, and all other text is copied unchanged.  The
resulting string is executed as an Ex command.  To avoid the replacement use
<lt> in place of the initial <.  Thus to include "<bang>" literally use
"<lt>bang>".