Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

vim

keymapping

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

like image 644
Kilon Avatar asked Sep 23 '10 08:09

Kilon


1 Answers

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.

like image 157
ib. Avatar answered Sep 26 '22 07:09

ib.