Vim on Windows and switching keyboard layouts

    UPD: This is the "historical" version of the topic. See a new solution to the problem here .

    The problem of the Russian layout in Vim has been raised many times. One of the solutions can be seen here , however it makes you get used to the new hot key for switching layouts. There are also many solutions with calling the system utility to change the layout, but I did not find such a utility under Windows, so I had to implement it myself.

    In essence, we got a console interface for WinAPI functions. To install a new layout for a window, the program receives the class name of the window and the two-letter language code. If there is such a window and the corresponding language code is found, then the program sends a WM_INPUTLANGCHANGEREQUEST message to this window.

    To create a connection between the program and Vim, I relied on an entry from the Tech-Details blog . For switching to work on Windows, add the following lines to _vimrc:

    fun! <SID>xkb_switch(mode)
        let cur_layout = system('dxlsw.exe -get VIM')
        if a:mode == 0if cur_layout != 'en'callsystem('dxlsw.exe -set VIM en')
            endif
            let b:xkb_layout = cur_layout
        elseif a:mode == 1ifexists('b:xkb_layout') && b:xkb_layout != cur_layout
                callsystem('dxlsw.exe -set VIM '.b:xkb_layout)
            endif
        endif
    endfun
    if executable('dxlsw.exe')
        autocmd InsertEnter * call <SID>xkb_switch(1)
        autocmd InsertLeave * call <SID>xkb_switch(0)
    endif
    


    Also do not forget to put dxlsw.exe (3.5 KB) somewhere in % PATH , for example, in C: \ Windows \ System32 . If anyone needs it, then there is a 64-bit version (5 KB). Source code (6.8 KB) is available under the LGPL2 license.

    Advantages: it works, switches the layout only in the Vim window.
    Disadvantages: when the system function is called from GVim for a short period of time, the cmd.exe window opens and the GVim window loses focus for a short time.

    UPD:
    On the advice of the habrayuzer, ivnik put together a DLL version of the language switch. The cmd window does not appear, there are no brakes.

    _vimrc has changed to:
    fun! <SID>lib_kb_switch(mode)
        let cur_layout = libcallnr('libdxlsw', 'dxGetLayout', 0)
        if a:mode == 0if cur_layout != 1033
                call libcallnr('libdxlsw', 'dxSetLayout', 1033)
            endif
            let b:lib_kb_layout = cur_layout
        elseif a:mode == 1if exists('b:lib_kb_layout') && b:lib_kb_layout != cur_layout
                call libcallnr('libdxlsw', 'dxSetLayout', b:lib_kb_layout)
            endif
        endif
    endfun
    autocmd InsertEnter * call <SID>lib_kb_switch(1)
    autocmd InsertLeave * call <SID>lib_kb_switch(0)
    


    Put the .dll file in the directory with the .exe file of Gvim. If the Vim build is 64-bit, then use the appropriate library.

    Also popular now: