Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve the annoyance with switching keyboard layouts for different languages in Vim?

Tags:

vim

windows-xp

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

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

Is there a way within Vim to “detect” a keyboard layout set, 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 on this (not thought of here) will be appreciated.

like image 895
Rook Avatar asked Nov 04 '11 20:11

Rook


1 Answers

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.

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, 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

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

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

like image 186
ib. Avatar answered Oct 22 '22 08:10

ib.