如何在iTerm2和Vim中使用Cmd-left/right(无需更改.vimrc)? [英] How to get Cmd-left/right working with iTerm2 and Vim (without requiring .vimrc changes)?

查看:110
本文介绍了如何在iTerm2和Vim中使用Cmd-left/right(无需更改.vimrc)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够像在shell提示符下一样,在Vim中使用Option-left和Option-right跳过单词(而Cmd-left/right转到行的开头和结尾).我的Iterm2首选项具有执行此操作的映射(例如,Option-leftEsc-H,而option-rightEsc-F的一个可以跳过单词),这在本地Shell中或在SSH到远程服务器时都有效

当我在本地或远程使用Vim时,option-left有效,但option-right无效.我怀疑这是因为Vim自然会监听Esc-H,而不是Esc-F.我可以通过将.vimrc文件修改为Esc-bb并将Esc-f修改为f来解决此问题,但是我不想对要连接的每个服务器执行此操作.

类似地,我对Cmd-left/right具有相同的期望设置,以转到行的开头和结尾.我可以通过Iterm2映射(例如Cmd-leftEsc-[h)在外壳中使用它,但是Vim除非对此再次映射键(例如Esc-[h^),否则根本不会对此做出响应.

更新:我只是想出了如何使左/右选项正常工作.我将iTerm2中的映射分别更改为escape-[1;5Descape-[1;5C.我仍然想解决Cmd-left/right问题(我更改了问题标题以反映这一点).有什么想法吗?

解决方案

为模仿OS X将Cmd-left/right发送到行首/结尾的行为,我在iTerm2中添加了以下映射:

  • Cmd-left转义序列[1~
  • Cmd-right转义序列[4~

为模仿OS X将Option-left/right发送到上一个/下一个单词的行为,我在iTerm2中添加了以下映射:

  • Option-left转义序列[1;5D
  • Option-right转义序列[1;5C

特别感谢此博客文章的跟踪记录我在cmd-left/right映射中缺少的东西

I want to be able to use Option-left and Option-right to skip words (and Cmd-left/right to go to beginning and end of lines) within Vim as it does at my shell prompt. My Iterm2 preferences have mappings to do this (e.g. Option-left to Esc-H and a one for option-right to Esc-F to skip over words), and this works in the shell locally or when ssh'd to a remote server.

When I use Vim locally or remotely, option-left works, but option-right does not. I suspect this is because Vim naturally listens for Esc-H, but not Esc-F. I am able to get around this by modifying .vimrc file to Esc-b to b and Esc-f to f, but I don't want to do this to every server I'm connecting to.

Similarly, I have the same desired setup for Cmd-left/right for going to beginning and end of a line. I can get this working in the shell via Iterm2 mappings (e.g. Cmd-left to Esc-[h), but Vim doesn't respond at all to this unless I map keys again (e.g. Esc-[h to ^).

Update: I just figured out how to get option-left/right working. I changed mapping in iTerm2 for these to be escape-[1;5D and escape-[1;5C respectively. I still want to solve the Cmd-left/right problem though (I changed my question's title to reflect this). Any ideas?

解决方案

To mimic OS X's behavior of sending Cmd-left/right to the beginning/end of a line, I add the following mappings in iTerm2:

  • Cmd-left to escape-sequence [1~
  • Cmd-right to escape-sequence [4~

To mimic OS X's behavior of sending Option-left/right to the previous/next word, I add the following mappings in iTerm2:

  • Option-left to escape-sequence [1;5D
  • Option-right to escape-sequence [1;5C

Special thanks to this blog post for tracking down what I was missing with the cmd-left/right mappings

这篇关于如何在iTerm2和Vim中使用Cmd-left/right(无需更改.vimrc)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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