When I'm typing a Chinese article in Vim, I use Chinese input method in InsertMode and English input method in NormalMode. I need to switch to English input method automatically when leaving InsertMode and switch to the original input method when entering InsertMode again — this is a common requirement.
I know GVim in Windows is doing this well. Also, in Linux there are some plugins solving this problem well with "fcitx" input method. But I prefer to use Vim in terminal (I've chosen git-bash) in Windows!
I tried the following way:
- Use AutoHotkey to create scripts for getting the current input method (
getIM.sh
) and switching to a specified input method (setIM1.exe
for Chinese &setIM2.exe
for English). - Then write a function in Vimscript to switch input method, then use
autocmd
to call this function automatically when leaving or entering InsertMode in.vimrc
, as follows:
let g:otherModeIM = 2
function SwitchIM()
let l:curModeIM = system("~/getIM.sh")
let l:tmp = system("~/setIM" . g:otherModeIM)
let g:otherModeIM = l:curModeIM
endfunction
set ttimeoutlen=100
autocmd InsertEnter * call SwitchIM()
autocmd InsertLeave * call SwitchIM()
The problem is: it becomes so slow to switch mode in Vim now! When I type i
or <esc>
, there's a one-second delay. I have no idea it's because my scripts (getIM.sh
& setIM1/2.exe
) run too slow, or because the autocmd
is too slow, or something else. Can someone help me?
P.S. The following is getIM.ahk
& getIM.sh
, and setIM1/2.ahk
(which setIM1/2.exe
is compiled from).
// getIM.ahk:
IMCodeToIndex := {0x8040804: 1, 0x4090409: 2}
winID := WinActive("A")
threadID := DllCall("GetWindowThreadProcessId", "Int", winID)
IM := DllCall("GetKeyboardLayout", "Int64", threadID)
FileOpen("curIM.txt", "w").write(IMCodeToIndex[IM])
// getIM.sh:
#!/bin/bash
AutoHotkey ~/getIM.ahk
cat ~/curIM.txt
// setIM1.ahk:
PostMessage, 0x50, 0, 0x8040804 /* which is 0x4090409 for setIM2.sh */, , A