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.

ib.
  • 27,830
  • 11
  • 80
  • 100
Rook
  • 60,248
  • 49
  • 165
  • 242
  • Perhaps better on http://superuser.com/. – orlp Nov 04 '11 at 20:22
  • 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 possible – sehe Nov 04 '11 at 20:23
  • Similar problem here, except with three keyboard layouts. It would be nice to have Vim switch to qwerty for modes other than the insertion. – Don Reba Nov 04 '11 at 20:44
  • 6
    @nightcracker - Unless something changed in the last few days, Vim questions are ontopic on SO. – Rook Nov 04 '11 at 20:46
  • @sehe - Not necessarily. What Don Reba is suggesting would be better. – Rook Nov 04 '11 at 20:47
  • @DonReba - Excellent suggestion. I didn't think of it, but you're right - that would be the best way to go. – Rook Nov 04 '11 at 20:47
  • @Idigas: I'd search the [vim wiki](http://vim.wikia.com/wiki/Vim_Tips_Wiki) and [script libraries](http://www.vim.org/scripts/index.php) 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 both – sehe Nov 04 '11 at 20:52
  • @sehe - 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. – Rook Nov 04 '11 at 21:19
  • See [my answer](http://stackoverflow.com/questions/3776728/using-vim-with-the-greek-language/3777557#3777557) to a similar question "[Using Vim with the Greek language](http://stackoverflow.com/questions/3776728/using-vim-with-the-greek-language)". Cc: @sehe – ib. Nov 05 '11 at 02:50
  • @ib.: nice work that. I +1-ed it! – sehe Nov 05 '11 at 11:01
  • @ 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 searchers – sehe Nov 05 '11 at 11:03
  • @ldigas: If the solution I referenced is applicable for your question, I could adapt it and post as an answer for this question. – ib. Nov 06 '11 at 00:59
  • @ib. - Yes, do that and I'll accept ti. – Rook Nov 06 '11 at 01:06
  • 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: @sehe – ib. Nov 16 '11 at 13:11
  • @ib. you're being almost too nice :) Thanks for the nudge, will read later – sehe Nov 16 '11 at 13:33
  • @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. – Rook Feb 16 '12 at 17:20

1 Answers1

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

ib.
  • 27,830
  • 11
  • 80
  • 100
  • 1
    `imcmdline` does not work with 'lnoremap', which is used internally by `'keymap'`. It does work with `IM` (Input Method) instead. – xged Feb 23 '15 at 09:21