如何在 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
在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