简体   繁体   English

在vim键映射中使用变量

[英]Using variable in vim key mappings

How can I use a variable when mapping keys in vim? 在vim中映射键时如何使用变量? The specific problem that I am trying to solve is the following. 我想解决的具体问题如下。 I need these key mappings: 我需要这些键映射:

nnoremap <C-1> 1gt
nnoremap <C-2> 2gt
nnoremap <C-3> 3gt

... and so on.

Can I specify one mapping; 我可以指定一个映射; something like 就像是

nnoremap <C-x> xgt

where x takes the value of the key pressed (which can be from 1..9) 其中x取压键的值(可以是1..9)

Thank you. 谢谢。

Edit 1: Towards the solution (not yet complete) thanks to Peter Rincker 编辑1:由于Peter Rincker,解决方案(尚未完成)

I can use the function 我可以使用这个功能

function gotoTab(num)
   execute "normal" a:num."gt"
endfunction

If I :call goToTab(3) , it goes to tab 3. 如果我:call goToTab(3) ,它将转到选项卡3。

How do I map Command-x (Dx) to goToTab(x) where x is between 1..9. 如何将Command-x(Dx)映射到goToTab(x),其中x介于1..9之间。 How do I read the number from a Command-x press? 如何从Command-x印刷机读取数字?

I got bad news. 我得到了坏消息。 You can not map <c-1> , etc. You can only bind <c-6> which I wouldn't do as it is very handy. 你不能映射<c-1>等。你只能绑定我不会做的<c-6> ,因为它非常方便。

It also seems like you are doing a heavily tab centric workflow. 您似乎正在进行以标签为中心的工作流程。 I know it might sound weird but maybe use less tab panes and more buffers. 我知道这可能听起来很奇怪,但可能会使用较少的选项卡窗格和更多缓冲区。 Here are some nice posts about it: 这里有一些关于它的好帖子:

... Ok, but I really want to do this variable mapping thing. ...好吧,但我真的想做这个变量映射的事情。 You have options: 你有选择:

  • Use a for loop and use :execute to create mappings 使用for循环并使用:execute来创建映射
  • The more Vim Way is to use a count so 7gt . 更多Vim Way是使用计数7gt The 7 is the count. 7是计数。

Example of using :for and :execute : 使用示例:for:execute

for i in range(1, 9)
  execute "nnoremap \<d-" . i . "> " . i . "gt"
endfor

Note: this uses <d-...> syntax for Command which is only available on MacVim and no terminal support (See :h <D- ). 注意:这使用Command的<d-...>语法,该语法仅在MacVim上可用且没有终端支持(参见:h <D- )。 You can use <a-...> for Alt. 您可以使用<a-...> Alt。 However I must warn you using Alt on the terminal can be tricky. 但是我必须警告你在终端上使用Alt可能会很棘手。

For more help see: 有关更多帮助请参阅:

:h keycodes
:h map-which-keys
:h :for
:h :exe
:h count
:h v:count
:h range(

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

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