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

查看:28
本文介绍了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).它可以发送 8 位字符并在使用 Alt 时设置高位,也可以使用转义序列,将 Alt-a 作为 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.

在早期的终端使用中,输入 Escj 是在没有 Meta 键的情况下在键盘上发送 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

Alt-letter 现在可以在终端中被 vi 和 gvim 识别.timeout 设置用于解决转义序列的歧义.50ms内发送的Escj会映射到,大于50ms的会算作单独的key.这应该足够区分 Meta 编码和敲击两个键了.

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天全站免登陆