Vim plugin that provides convenient keyboard layout switching on Mac OS X

    Foreword


    More recently, just a couple of weeks ago, I decided to switch to Vim . I was attracted by the potential power of this editor: having configured everything correctly, you can get a full-fledged IDE that works just the way you want it. In addition, a bunch of all kinds of keyboard shortcuts allow you to create and edit texts with supersonic speed, it is enough to remember the necessary combinations only once. Plus, you can add your own.

    The only thing that bothered me was the need to constantly switch layouts in order to fully work in Vim. Yes, of course, you can make mappings for keys, but this does not always work.

    History of creation


    The first solution was suggested to me by kossnocorp . The essence of this solution was to use a small application written in Objective-C, which will activate the US layout at the right time (for example, when you exit input mode). There was even the source code for this program, which was shared with kossnocorp by the habrauzer labria . But this decision did not suit me: I use the typographical layout of Ilya Birman . Then I decided to write my decision, taking the same idea as a basis.

    I myself am not an Objective-C programmer, but, armed with good C knowledge and excellent documentation from Applestarted to dig. No sooner said than done. After killing half a day, I still achieved the desired result. If the original solution ironically switched the layout to the US layout, finding it by name, then my solution was flexible, it operated not by the layout name, but by its so-called index. We just take the installed layouts (in my case, this is the US English layout and the Russian and English Birman layouts) and select the desired one by number.

    Then, naturally, the plugin for Vim followed. Everything is quite simple here, using autocmd , you can call your functions for certain events (for example, the same InsertLeave - exit input mode).

    However, while studying the VimL documentationI realized how powerful this language is. It has almost everything: an event model, objects, lists, functions for working with lists, regular expressions, mathematical functions, and much more. And then the thought came to my mind, why not remember the keyboard layout when losing focus on Vim input?

    Result


    The result is a convenient plug-in that can switch the layout:
    • When starting Vim;
    • When you exit input mode;
    • At the time of entering command mode;
    • Double tap in normal and command modes.
    And the most delicious: the plugin remembers the layout if Vim has lost the input focus, and switches to it when the focus returns (for example, if you are distracted by the messenger or something else).

    Oh yes, with all this, the plugin stores the layout separately for each tab. Well isn't that great?

    Installation and setup


    The source code of the plugin itself lies on GitHub. Everything is as usual, download the source code in .zip , unzip it and put all the good in ~ / .vim .

    There, on GitHub, you can find the source code of the supporting application . It may be needed for those who still use Mac OS X Leopard, they will have to compile everything themselves. It is not difficult, in the source everything is described in detail.

    The plugin sets the default layout index to zero:
    let g: defaultInputSourceIndex = 0

    If you, like me, use the Birman layout or some other, then you have to change the index value. Finding it out is very simple: just switch to the desired layout and start the auxiliary application ( ~ / .vim / bin / KeyboardLayoutSwitcher ). This will open a terminal window in which the desired number will be:

    Screenshot of the Terminal window after starting the auxiliary application

    After that, open ~ / .vimrc and add the desired line:
    let g: defaultInputSourceIndex = n
    Where n is the received number (in the screenshot it is 1 ).

    Enjoy your coding in Vim.


    Update : at the request of the habrayuzer, cypok made the “most delicious” option. If you add the line to ~ / .vimrc :
    let g: kls_focusSwitching = 0
    then the function of storing and restoring layouts when losing and restoring input focus at Vim will be disabled.

    Also popular now: