Vim and switching layouts: about sore

Published on February 15, 2017

Vim and switching layouts: about sore

    Hello. I have been using the Vim editor for quite a while to edit very different texts: editing configs, writing code, as an external editor in the browser and email client ...

    The editor is almost perfect, but there was one small “but”. In particular, infuriated switching layouts. Recently, in a private conversation, we talked about a rake lying on the way of using Vim, and I decided to sketch out some sort of “marginal notes”.


    It would seem that there’s such a thing: I pressed the combination, enter the Latin alphabet, pressed again - the Cyrillic alphabet. However, in normal mode, a stay in which takes at least 80% of the time, its fingerprints are superimposed. Okay, I think everyone knows about langmap.

    We write the following in ~ / .vimrc:

    set langmap=ФИСВУАПРШОЛДЬТЩЗЙКЫЕГМЦЧНЯ;ABCDEFGHIJKLMNOPQRSTUVWXYZ,фисвуапршолдьтщзйкыегмцчня;abcdefghijklmnopqrstuvwxyz

    And we can execute commands in normal mode with a Cyrillic layout. However, to enter commands via <:> you still need to switch the layout, which infuriated.

    Then I remembered that the editor has its own switch.

    set keymap=russian-jcukenwin

    In more detail, the technique was described, for example, here .

    It seems that the problem was solved: the layout is switched only in insert mode, and the commands are entered in Latin. But again, the problem: the built-in switch, well, very much conflicts with the X. And the default combination <C - ^> is not particularly ergonomic.

    Disable X's and rely entirely on Vim? In principle, one could go for it, but really did not want to. To call the editor for a set of Cyrillic names in the terminal seemed to me some sort of bust. For some time he used, got confused and cursed.

    So it won’t work, again on Google. I found an interesting article by Dmitry Khrabrov DeXPeriX about the vim-xkbswitch plugin written by him and Alexei Radkov .

    In short, the plugin does the following: when switching to normal mode, switches the system-wide layout (via xkb-switch, which must be installed in the system) to the Latin alphabet, remembering the current layout, and when switching to insert mode, switches to the memorized one. To say that I was glad - to say nothing. Many thanks to the authors for what they have been lacking for all these years.

    However, in the process of operation, I encountered some difficulties. Somehow I went to my computer via ssh:

    $vim file.txt
    Vim: Caught deadly signal SEGV
    Vim: Finished.
    zsh: segmentation fault  vim file.txt

    I didn’t think of the plug-in right away, there were thoughts of a crumbled hard drive, a crashing file system and the like “pleasant” things ... In general, xkb-switch with the DISPLAY variable not set falls into segfault, and after it the plug-in, taking away and vim .

    As a solution, you can check whether the variable is set and, only if successful, activate the plugin. It looks something like this:

    if $DISPLAY == "" 
    	let g:XkbSwitchEnabled = 0
    else
    	let g:XkbSwitchEnabled = 1
    	let g:XkbSwitchLib = '~/.local/lib/libxkbswitch.so'
    	let g:XkbSwitchIMappings = ['ru']
    endif 

    After that, the flights stopped. If you want to have plugin functionality when editing via ssh, then you need to connect with X-forwarding support, with the -X or -Y switches.

    Hope this article helps save some nerve cells.

    UPD: Made a pull request to the developer.