How to avoid constant switching to and from English keyboard layout to type Vim commands when writing in non-Latin language (e.g., Greek)?
Asked Answered
F

1

42

I am giving Vim a serious try for coding my Python apps. However, Vim is proving to be so flexible, I was thinking to use it as my main editor at work for writing lawyer/legal documents. The problem is that my mother tongue is not English but Greek. So, I have mapped Alt+Shift to change between English and Greek keyboard layouts.

The issue I am experiencing is that I have to press Alt+Shift each time I want to enter a Vim command (to return back to English). So it is Alt+Shift when I type my document, then Alt+Shift again to enter Vim commands. This defeats the purpose of using Vim, at least in terms of speed of use.

So my question is simple: Is there any way to avoid those frequent Alt+Shift keyboard layout changes just for entering Vim commands when writing in a non-Latin language (e.g., Greek)?

Filmer answered 23/9, 2010 at 8:54 Comment(1)
Here's a simple wayGorlovka
F
54

This problem can be solved with the help of the keymap option. It allows to define an alternate keyboard mapping to use in modes requiring text input.

To switch between the default and alternate keymaps while in Insert, Replace, or Command-line mode (but not Normal mode), use Ctrl+^ (Ctrl+6). Changing the keymap affects text input only; keyboard behavior in Normal mode stays the same regardless of the current keymap setting. One can leave Insert mode writing in Greek and immediately use Normal-mode keybindings without switching to a different keyboard layout. If one then returns to Insert mode or, for example, starts a search by typing /, Vim switches the keymap back to Greek automatically.

The current keymap used in those text-entering modes is remembered between switchings to other modes. The only exception from this behaviour is made for Command-line mode which always starts with the default keymap, since most of the time it is required to type an Ex command (in ASCII). With the keymap option set, user is supposed to work in Vim keeping system keyboard layout set to English while switching Vim keymap with Ctrl+^ (instead of the system-wide layout switch).

To enable, say, the UTF-8 Greek keymap permanently, add the following line to your .vimrc file.

:set keymap=greek_utf-8

There are many predefined keymaps for a large set of languages, you can browse them all in Vim itself using :e $VIMRUNTIME/keymap. Note that usually there are several keymaps provided for one language which differ only by character encoding, so that anybody could choose one that suits their configuration.

I also recommend setting the options below to specify whether the keymap should be enabled by default in Insert mode and when entering a search pattern:

:set iminsert=0 imsearch=-1

See :help iminsert and :help imsearch for the detailed explanation.

There is also a special language mode that, if I am not mistaken, was introduced in Vim earlier than keymap. It allows to achieve the behaviour similar to the one provided by keymap through manually specifying letter pairs that correspond to the keys on keyboard in a long string to be saved in the langmap option. Personally—my native language is not English, too—I prefer (and recommend) using the keymap way instead.

In conclusion, I should emphasize that all of the above is equally applicable to any other language Vim has (or can be configured to have) a keymap for.

See also my answer to a similar question ‘Vim “annoyance” with keyboard layouts’ that has been asked since I originally gave this answer.

Fundus answered 23/9, 2010 at 10:48 Comment(7)
You are amazing!!! Your suggestion did not work, but you pointed me to the correct direction. All I had to do was to change set keymap=greek_utf-8 to set keymap = greek_iso-8859-7 . For a stange reason greek utf displays weird characters, the same happens with my firefox when it chooses utf, but iso also work there. Everything else worked like a charm I just press ctrl+^ in edit mode as you suggested and I can change between greek and englis while the command interface remains in english . You are a life savior , thanks.Filmer
Is it possible to use something else than ctrl+^. It's difficult to press Ctrl+Shift+6.Macbeth
@Sergey: Ctrl+6 works as well. If it's still uncomfortable for you to type, create an easy to use mapping. For example, to remap it to Ctrl+L use inoremap <c-l> <c-^>.Fundus
Ctrl + ^ just opens up a new file in the buffer for me; does not switch languages.Twinge
@dionyziz: That is because Ctrl+^ switches to the alternate buffer in Normal mode; while in Insert and Command-line modes it toggles language mappings. Compare :help ^^ with :help i_^^ and :help c_^^.Fundus
@Filmer perhaps your terminal font does not support UTF-8?Hedveh
@ib Thanks for inoremap <c-l> <c-^>! Was searching for how to remap this. Mapping to <F3> works best.Libenson

© 2022 - 2024 — McMap. All rights reserved.