Passing register name in mapping to an ex command

瘦欲@ 提交于 2020-01-11 09:21:12

问题


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.

Here is a toy example:

nmap <leader>p :put 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

To use the 'a' register, or:

"b<leader>p

to use the 'b' register.

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


回答1:


Ah, I figured it out.

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

So, for my example, the answer is:

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

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



来源:https://stackoverflow.com/questions/14632109/passing-register-name-in-mapping-to-an-ex-command

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!