将映射中的寄存器名称传递给 ex 命令 [英] Passing register name in mapping to an ex command

查看:17
本文介绍了将映射中的寄存器名称传递给 ex 命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我想在 vim 中创建一个映射,它将调用 ex 命令作为其工作的一部分.并假设 ex 命令将寄存器名称作为输入.

Suppose I want to create a map in vim that will call an ex command as part of its work. And suppose that the ex command takes a register name as input.

这是一个玩具示例:

nmap <leader>p :put x

问题在于x"寄存器将始终被使用.但是打字的时候,我希望能够写:

The problem there is that the 'x' register will always be used. But when typing, I want to be able to write:

"a<leader>p

要使用a"寄存器,或:

To use the 'a' register, or:

"b<leader>p

使用'b'寄存器.

有没有办法将当前正常模式寄存器"传递给ex"命令?

Is there a way that I can pass the 'current normal-mode register' along to the 'ex' command?

推荐答案

啊,我想通了.

诀窍是在映射中使用 .

The trick is to use <expr> in the mapping.

所以,对于我的例子,答案是:

So, for my example, the answer is:

nnoremap <expr> <leader>p ':put ' . v:register . '<CR>'

检查 :h map-expression:h v:register 以获取更多信息.

Check :h map-expression and :h v:register for more info.

这篇关于将映射中的寄存器名称传递给 ex 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆