lyokha / vim-xkbswitch

vim plugin for automatic keyboard layout switching in insert mode
MIT License
481 stars 23 forks source link

Switch layout when in normal mode #54

Closed coordinatio closed 2 years ago

coordinatio commented 2 years ago

First, thank you for the great plugin! It is the real life-changer when you need to edit multilingual text.

I wonder, is there a way to switch layout when in normal mode? Occasionally, I realize that I need to switch all key maps to another language when in normal mode. Now I enter insert mode, switch the layout and then switch back to the normal mode. That sequence is time-consuming. Is it possible to replace that sequence above by a key combination somehow?

lyokha commented 2 years ago

Hi.

I wonder, is there a way to switch layout when in normal mode?

Do you mean that Vim would alter some state after triggering system keyboard layout switch? If so, then no: system keyboard layout change cannot be caught inside Vim as it is only caught by the desktop environment.

Occasionally, I realize that I need to switch all key maps to another language when in normal mode.

What kind of key maps do you mean? The plugin does not affect Vim's Normal mode maps in any way, it only may load the Insert mode maps (i.e. imaps) which have no effects in Normal mode.

coordinatio commented 2 years ago

For example, let's suppose that you need to edit multilingual text like that:

A word in English, а теперь слово на русском.

You have left the insert mode when you were writing the Russian text, you are at the end of the line and need to jump to the beginning of the word English by pressing FE. You cannot do that because of the mappings active. So, you must enter insert mode, switch the layout, exit the insert mode and then if you press FE that will do what you want.

The idea is to avoid that "enter insert mode - exit insert mode" sequence. Would you please recommend how to do that?

lyokha commented 2 years ago

Ok, this looks like an effect of the Keymap assistance in Normal mode. Removing

let g:XkbSwitchAssistNKeymap = 1
let g:XkbSwitchDynamicKeymap = 1

from the Vim's config file should fix this. Or, if you still want to use the keymap assistance, you may reset iminsert by typing

:set iminsert=0

(you can also define a map for this in Normal mode, notice also that the language in the search line can be switched easily by Ctrl-^).

Btw while digging through this issue I found a small bug: when g:XkbSwitchDynamicKeymap is on then both g:XkbSwitchAssistNKeymap and g:XkbSwitchAssistNKeymap is automatically on regardless of their values in the Vim config. I am going to fix this.

coordinatio commented 2 years ago

Thanks for the advice! I've made the following change in my .vimrc and it seems like the perfect solution I have been searching for!

function! ImInserToggle()
    if &iminsert
        set iminsert=0
    else
        set iminsert=1
    endif
endfunction
nnoremap <a-k> :call ImInserToggle()<cr>

Thank you so much!

lyokha commented 2 years ago

@coordinatio, ImInsertToggle() looks good except you'd better use setlocal instead set in both clauses to not affect iminsert globally rather than in the current buffer. I also fixed the bug with the dynamic keymap assistance that I'd mentioned.