如何解决 Vim 中为不同语言切换键盘布局的烦恼? [英] How to resolve the annoyance with switching keyboard layouts for different languages in Vim?

查看:41
本文介绍了如何解决 Vim 中为不同语言切换键盘布局的烦恼?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

事情就是这样.我有两种键盘布局,HR"(克罗地亚语,我的母语)和EN"(英语).嗯,实际上我还有一些,但目前它们并不重要.

Here’s the thing. I have two keyboard layouts, ‘HR’ (Croatian, my native language) and ‘EN’ (English). Well, actually I have some more but they’re not important at the moment.

在使用 Vim 时,我经常不得不切换到EN"——因为在HR"上我既没有 [,] 也没有 {,},也没有很多其他字符——然后切换回HR"以获取我自己的语言字符.这很痛苦.

When working with Vim, I often have to switch to ‘EN’—since on ‘HR’ I don't have neither [,], nor {,}, nor a lot of other characters—and then switch back to ‘HR’ for my own language characters. This is a pain.

当然,因为我在没有任务栏的情况下工作,这通常会导致浪费Shift 按键.

Of course, since I’m working without a taskbar, this often results in wasted Shift key presses.

Vim 中是否有一种方法可以检测"键盘布局集、对其进行评估并将其放入状态行?

我尝试重新映射一些键(例如 tilda 到反引号),但这只会带来很多新问题.

I’ve tried remapping some keys (like tilda to backtick) but that just introduced a whole new lot of problems.

对此的所有建议(此处未考虑)将不胜感激.

All advice on this (not thought of here) will be appreciated.

推荐答案

Vim 有系统的语言相关键重映射方式驻留在一次配置整个范围的映射设置 keymap 选项.它允许定义之间的映射英文键盘布局和对应的非英​​文字符该布局的字符(请参阅 :help mbyte-keymap).译文仅在解释用户输入的情况下发生作为文本输入,而不是作为任何类型的 Vim 命令的调用.这个适用于插入、替换和命令行模式,以及输入搜索模式或单字符参数ftr 命令等.

There is a systematic way of language-related key remapping in Vim that resides in configuring a whole range of mappings at once by setting the keymap option. It allows to define a mapping between characters of English keyboard layout and corresponding non-English characters for that layout (see :help mbyte-keymap). The translation takes place only in situations where the user typing is interpreted as text input, not as invocation of a Vim command of any kind. This applies to Insert, Replace, and Command-line modes, as well as to entering a search pattern or a single-character argument for the f, t, r commands and alike.

在所有上述上下文中,除了一个挂起的字符参数,切换使用 keymap 中指定的键映射选项由 Ctrl+^ 执行(Ctrl+6) 按键.(见 :help i_^^:help c_^^.) 如上所述,启用的键盘映射仅影响文字输入;正常模式下的键盘行为保持不变无论当前的语言映射如何.这样,当一个人离开非英文keymap中的插入模式书写,他们可以立即使用无需切换键盘的正常模式命令和键绑定布局.返回插入模式将输入切换到键盘映射上次离开时​​使用(键盘映射处于活动状态的事实存储在 iminsert 选项中).

In all of the aforementioned contexts, except for a pending character argument, toggling the use of a key mapping specified in the keymap option is performed by the Ctrl+^ (Ctrl+6) keystroke. (See :help i_^^ and :help c_^^.) As it is mentioned above, enabled keymap affects only text input; keyboard behavior in Normal mode remains the same regardless of the current language mapping. This way, when one leaves Insert mode writing in non-English keymap, they can immediately use Normal mode commands and keybindings without switching keyboard layout. Returning back to Insert mode switches input to the keymap used the last time it has been left (the fact that keymap was active is stored in the iminsert option).

然而,键映射的状态不会被记住默认为命令行模式,因为它被认为方便每次都开始输入英文,因为 Ex 命令的名称是全部为 ASCII.要否定该行为,请使用命令

However, the state of the key mapping is not remembered for Command-line mode by default, since it is considered convenient to start typing in English every time, as the names of Ex commands are all in ASCII. To negate that behavior, use the command

:set imcmdline

输入搜索模式时语言映射是否处于活动状态(对于 /? 命令)是分开记住的,通过imsearch 选项.同步切换语言的使用用于输入搜索模式和在缓冲区中插入文本的映射,相应地设置选项:

Whether the language mappings are active when typing a search pattern (for the / and ? commands) is remembered separately, via the imsearch option. To synchronize toggling the use of the language mappings for entering a search pattern and inserting text in a buffer, set the option accordingly:

:set imsearch=-1

大约有十几个半的预定义键盘映射并不多语言.通常,一种语言由多个映射支持它们的设计用途因编码或键盘布局而异和.你可以在 Vim 中用 :e $VIMRUNTIME/keymap 浏览它们.为了启用特定的键盘映射,请设置 keymap该映射名称的选项.例如,以下命令将 keymap 更改为内置的 UTF-8 克罗地亚语映射.

There are not a few predefined keymaps for about a dozen and a half languages. Usually, a language is supported by several mappings that differ by encoding or keyboard layout they are designed to be used with. One can browse them all in Vim itself with :e $VIMRUNTIME/keymap. In order to enable a particular keyboard mapping, set the keymap option to the name of that mapping. For instance, the following command changes keymap to the built-in UTF-8 Croatian mapping.

:set keymap=croatian_utf-8

如果您想自定义现有的键盘映射或创建一个全新的一,在:help keymap-file-format查看keymap文件格式.

If you want to customize an existing keymap or to create a brand new one, look into the keymap file format at :help keymap-file-format.

将上述命令放入 .vimrc 文件中,您可以激活所有缓冲区中的指定键映射.尽管如此,keymap 选项是更强大,因为它是本地缓冲区,这意味着各种键盘映射可以在不同的缓冲区中同时使用取决于设置(可能会受到文件类型的影响)自动命令的使用).

Putting the above command into the .vimrc file lets you activate the specified keymap in all buffers. Nevertheless, the keymap option is even more powerful since it is local to buffer, meaning that various keyboard mappings could be used simultaneously in different buffers depending on settings (which can be affected by file types through the use of autocommands).

这篇关于如何解决 Vim 中为不同语言切换键盘布局的烦恼?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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