Alt键快捷方式在Vim的gnome终端上不起作用 [英] Alt key shortcuts not working on gnome terminal with Vim

查看:142
本文介绍了Alt键快捷方式在Vim的gnome终端上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在gnome终端上运行Vim.但是alt键映射不起作用.
例如(这只是一个例子):

I'm running Vim on a gnome terminal. But the alt key mappings are not working.
For example (this is just an example):

:imap <A-i> <Esc>

它在GVim中工作正常.但是,当我在gnome终端中用Vim运行相同的命令时,它根本不起作用.
所以我认为问题出在终端上,对吗?
我该如何解决?

It works fine in GVim. But when I run the same command with Vim in the gnome terminal it just doesn't work.
So I think the problem is with the terminal, right?
How can I fix it?

谢谢

编辑:我在同一台计算机上安装了Windows 7,并且在Windows终端上也可以正常工作.

I have Windows 7 installed on the same machine, and with Windows terminal it works just fine too.

推荐答案

问题

终端仿真器有两种发送 Alt 密钥的方法(通常称为Meta密钥,因为实际的终端没有Alt).当使用Alt时,它可以发送8位字符并设置高位,也可以使用转义序列,将 Alt-a 发送为<Esc>a. Vim希望看到8位编码,而不是转义序列.

The problem

There are two ways for a terminal emulator to send an Alt key (usually called a Meta key as actual terminals didn't have Alt). It can either send 8 bit characters and set the high bit when Alt is used, or it can use escape sequences, sending Alt-a as <Esc>a. Vim expects to see the 8 bit encoding rather than the escape sequence.

某些终端仿真器(例如 xterm )可以设置为使用两种模式,但是 Gnome终端不提供任何此类设置.老实说,在当今的Unicode编辑中,无论如何,8位编码并不是一个好主意.但是转义序列也不是没有问题.他们没有办法区分<Esc>j Alt-j 还是按 Esc 后跟 j .

Some terminal emulators such as xterm can be set to use either mode, but Gnome terminal doesn't offer any such setting. To be honest in these days of Unicode editing, the 8-bit encoding is not such a good idea anyway. But escape sequences are not problem free either; they offer no way of distinguishing between <Esc>j meaning Alt-j vs pressing Esc followed by j.

在较早的终端使用中,键入 Esc j 是在没有 Meta 键的情况下在键盘上发送元数据的另一种方法,但这并没有与vi使用Esc离开插入模式非常不匹配.

In earlier terminal use, typing Escj was another way to send a Meta on a keyboard without a Meta key, but this doesn't fit well with vi's use of Esc to leave insert mode.

可以通过配置vim将转义序列映射到其Alt组合来解决此问题.

It is possible to work around this by configuring vim to map the escape sequences to their Alt combinations.

将此添加到您的.vimrc:

Add this to your .vimrc:

let c='a'
while c <= 'z'
  exec "set <A-".c.">=\e".c
  exec "imap \e".c." <A-".c.">"
  let c = nr2char(1+char2nr(c))
endw

set timeout ttimeoutlen=50

现在,终端中的vi以及gvim都将识别Alt-letter. timeout设置用于解决转义序列的歧义.在50毫秒内发送的 Esc j 将被映射到<A-j>,大于50毫秒将被视为单独的密钥.那应该是足够的时间来区分元编码和击中两个键.

Alt-letter will now be recognised by vi in a terminal as well as by gvim. The timeout settings are used to work around the ambiguity with escape sequences. Esc and j sent within 50ms will be mapped to <A-j>, greater than 50ms will count as separate keys. That should be enough time to distinguish between Meta encoding and hitting two keys.

如果您不喜欢设置timout设置,该设置对于其他映射的键序列会超时(默认情况下为一秒钟),那么您可以使用ttimeout代替. ttimeout仅适用于键控代码,不适用于其他映射.

If you don't like having timout set, which times out for other mapped key sequences (after a second by default), then you can use ttimeout instead. ttimeout applies only to key codes and not other mappings.

set ttimeout ttimeoutlen=50

这篇关于Alt键快捷方式在Vim的gnome终端上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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