Vim “annoyance” with keyboard layouts

筅森魡賤 提交于 2019-12-28 12:32:10

问题


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.

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 back to HR for my own language's characters. This is a pain.

Of course, since I'm working without a taskbar this often results in wasted Shift pressing.

Is there a way within Vim to "detect" a keyboard layout set, and to evaluate it and put it in status line?

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

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


回答1:


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 will take place only when the user is supposed to input text, not to enter a Vim command of some kind. This applies to typing in Insert, Replace, and Command-line modes as well as to entering a search pattern or a single-character argument for commands like f, t, r.

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).

However, 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 Ex commands' names are all in ASCII. The command

:set imcmdline

negates that behavior.

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

:set imsearch=-1

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

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.

Putting that setting line into the .vimrc file lets you activate the specified keymap in any buffer. 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.



来源:https://stackoverflow.com/questions/8015231/vim-annoyance-with-keyboard-layouts

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!