📜 ⬆️ ⬇️

A plugin for Vim that provides easy keyboard layout switching on Mac OS X

Foreword


Just recently, just a couple of weeks ago, I decided to switch to Vim . I was attracted by the potential power of this editor: by setting everything in the right way, you can get a full-fledged IDE that works exactly as you need. In addition, a bunch of various keyboard shortcuts allow you to create and edit texts with supersonic speed, it’s enough just to remember the necessary combinations 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 do key mappings, but this does not always work.

History of creation


The first decision was suggested to me by kossnocorp . The essence of this decision was to use a small application written in Objective-C, which will activate the US layout at the right time (for example, when exiting the input mode). Even there was the source code of this program, which labria shared with kossnocorp . But this decision did not suit me: I use the typographical layout of Ilya Birman . Then I decided to write my decision, based on the same idea.
')
I myself am not an Objective-C programmer, but, armed with good knowledge of C and excellent documentation from Apple , I began to dig. No sooner said than done. After killing half a day, I still achieved the desired result. If the original solution was ironically switching the layout precisely to the layout of the USA, finding it by name, then my solution was flexible, it operated not with the name of the layout, but with its so-called index. We simply take the installed layouts (in my case, this is the English layout of the United States and the Russian and English layouts of Birman) and select the desired one by number.

Then, naturally, followed the turn of the plugin for Vim. It's all quite simple, using autocmd , you can call your functions for certain events (for example, the same InsertLeave - exit from input mode).

However, in the process of studying the VimL documentation, I realized how powerful this language is. It has almost everything: event model, objects, lists, functions for working with lists, regular expressions, mathematical functions and much more. And then the thought occurred to me, why not memorize the keyboard layout when I lose the Vim input focus?

Result


The result was a convenient plugin that can switch layouts:And the most delicious: the plugin remembers the layout, if Vim lost the input focus, and switches to it when the focus returns (for example, if you are distracted by a messenger or something else).

Oh yeah, with all this, the plugin stores the layout separately for each tab. Isn't it great?

Installation and Setup


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

In the same place, on GitHub, it is possible to find the source code of the auxiliary application . It may be necessary for those who still use Mac OS X Leopard, they will have to compile everything themselves. It's easy, in the source, everything is described in detail.

In the plugin, the default layout index is set to zero:
let g : defaultInputSourceIndex = 0

If you, like me, use the layout of Birman or any other, then you will have to change the value of the index. It is very easy to figure it out: just switch to the desired layout and launch the auxiliary application ( ~ / .vim / bin / KeyboardLayoutSwitcher ). A terminal window will open, in which the required number will be:

Screenshot of the Terminal window after the launch of the auxiliary application

After that, open ~ / .vimrc and add the necessary 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 habraiser cypok rendered the option of "the most delicious." If in ~ / .vimrc add the line:
let g : kls_focusSwitching = 0
then the function of memorizing and restoring the layout at the loss and restoration of the input focus of Vim will be disabled.

Source: https://habr.com/ru/post/98756/


All Articles