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

1

14

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.

Tribulation answered 4/11, 2011 at 20:15 Comment(16)
Perhaps better on superuser.com.Pulchi
Am I right in concluding that you always want to switch to EN keyboard mode for vim? In that case, I'd phrase the question as such. That is likely possibleFetishism
Similar problem here, except with three keyboard layouts. It would be nice to have Vim switch to qwerty for modes other than the insertion.Genitals
@nightcracker - Unless something changed in the last few days, Vim questions are ontopic on SO.Tribulation
@Fetishism - Not necessarily. What Don Reba is suggesting would be better.Tribulation
@DonReba - Excellent suggestion. I didn't think of it, but you're right - that would be the best way to go.Tribulation
@Idigas: I'd search the vim wiki and script libraries for the plugins that do dvorak and simplified chinese and stuff like that. I imagine they all share the same challenge, and I'm sure I've seen bothFetishism
@Fetishism - I've tried finding something alike before posting this. If you think you have a potential working solution, don't hesistate to post it as an answer.Tribulation
See my answer to a similar question "Using Vim with the Greek language". Cc: @FetishismAshburn
@ib.: nice work that. I +1-ed it!Fetishism
@ ldigas: would you post your resolution as a self-answer so it can be found by future SO searchers? Don't forget to credit (+1) the answers you used, and I'll be happy to +1 if it adds value for future searchersFetishism
@ldigas: If the solution I referenced is applicable for your question, I could adapt it and post as an answer for this question.Ashburn
@Ashburn - Yes, do that and I'll accept ti.Tribulation
I have written the answer presenting all of the key options (pun intended) concerning keymap configuration. Additionally, I will probably add a few more details about X Input Method later. Cc: @FetishismAshburn
@Ashburn you're being almost too nice :) Thanks for the nudge, will read laterFetishism
@ib - Sorry ib. mate, I completely "lost" track of this one. I don't know what I did at the time of this question, but recall that your answer helped somehow. In anycase I'll accept it for now, and try to find what I did at the time that solved the problem, when I find a free hour or two these days.Tribulation
A
19

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

Ashburn answered 16/11, 2011 at 13:4 Comment(1)
imcmdline does not work with 'lnoremap', which is used internally by 'keymap'. It does work with IM (Input Method) instead.Firstborn

© 2022 - 2024 — McMap. All rights reserved.