在 vim 中映射 shift 键 [英] mapping the shift key in vim

查看:38
本文介绍了在 vim 中映射 shift 键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 VIM 中映射 Shift 键时遇到问题.我希望 Ctrl+L 的行为不同于 Ctrl+Shift+L

I'm having an issue when mapping the Shift key in VIM. I want Ctrl+L to behave differently than Ctrl+Shift+L

我有这个

" for insert mode remap <c-l> to:
" Insert a hash rocket  for ruby
" Insert a -> for php
" for coffee the shift key decides

function! SmartHash(...)
  let shift = a:0 > 0
  let ruby = &ft == 'ruby'
  let php = &ft == 'php'
  let coffee = &ft == 'coffee'

  if php
    return "->"
  end

  if coffee
    return shift ? " =>
" : " ->
"
  end

  if ruby
    return " => "
  end

  return ""

endfunction

imap <c-l> <c-r>=SmartHash()<cr>
imap <C-S-L> <c-r>=SmartHash(1)<cr>

...但无论我是否按下了 Shift 键,它都会触发第二个映射.

...but it's just triggering the second mapping whether I have the Shift key pressed or not.

" for insert mode remap <c-l> to:
" Insert a hash rocket  for ruby
" Insert a -> for php and coffeescript
" double tapping does alternate symbol for php and coffescript
function! SmartHash(...)
  let alt = a:0 > 0
  let ruby = &ft == 'ruby'
  let php = &ft == 'php'
  let coffee = &ft == 'coffee'

  if php || coffee
    return alt ? " =>
" : " ->
"
  end

  if ruby
    return " => "
  end

  return ""

endfunction

imap <c-l> <c-r>=SmartHash()<cr>
imap <c-l><c-l> <c-r>=SmartHash(1)<cr>

推荐答案

由于键盘输入在内部处理的方式,不幸的是,即使在 GVIM 中,今天也无法做到这一点.这是一个已知的痛点,也是 vim_dev 和 #vim IRC 频道上各种讨论的主题.

Due to the way that the keyboard input is handled internally, this unfortunately isn't possible today, even in GVIM. This is a known pain point, and the subject of various discussions on vim_dev and the #vim IRC channel.

有些人(最重要的是 Paul LeoNerd Evans)想要解决这个问题(即使是支持此功能的终端中的控制台 Vim),并提出了各种建议,cp.http://groups.google.com/group/vim_dev/browse_thread/thread/626e83fa4588b32a/bfbcb22f37a8a1f8

Some people (foremost Paul LeoNerd Evans) want to fix that (even for console Vim in terminals that support this), and have floated various proposals, cp. http://groups.google.com/group/vim_dev/browse_thread/thread/626e83fa4588b32a/bfbcb22f37a8a1f8

但截至今天,还没有补丁或志愿者出现,尽管许多人表示希望在未来的 Vim 8 主要版本中提供此功能.

But as of today, no patches or volunteers have yet come forward, though many have expressed a desire to have this in a future Vim 8 major release.

我通常通过使用 g 开始我的备用映射来解决这个问题,例如<C-l>g<C-l>,但这在插入模式下不起作用.在您的情况下,通过一些额外的逻辑,您可以在同一位置创建第二个 <C-l> 将先前插入的 -> 更改为 =>,这样您就可以通过 <C-l> 获得第一个替代方案,通过 .

I usually work around this by starting my alternate mapping with g, e.g. <C-l> and g<C-l>, but that won't work in insert mode. In your case, with a little bit of additional logic, you could make a second <C-l> at the same position change the previously inserted -> to =>, so that you get the first alternative via <C-l>, and the second via <C-l><C-l>.

这篇关于在 vim 中映射 shift 键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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