繁体   English   中英

如何在Vim中使用循环来定义多个映射

[英]How to use a loop in Vim to define multiple mappings

在Vim中有一种方法可以用循环定义多个映射吗?

例如,这是在没有循环的情况下定义它的方式。

nnoremap<leader>1 1gt
nnoremap<leader>2 2gt
nnoremap<leader>3 3gt

但我想用循环来做这样的事情。

for i in [1, 2, 3]
  nnoremap<leader>${i} ${i}gt
endfor

有没有办法做某种插值?

是的, :execute命令:

:exe[cute] {expr1} ..   Executes the string that results from the evaluation
                        of {expr1} as an Ex command.

所以你的榜样就是

for i in [1, 2, 3]
  execute 'nnoremap <leader>'.i.' '.i.'gt'
endfor

暂无
暂无

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

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