在 VSCode 的 Vim keybinding 下自動切換 fcitx 模式

DanSnow
2 min readSep 30, 2018

--

因為最近在寫 markdown 總是要切換中英文的很麻煩,在 insert mode 要切成中文 normal mode 要切回英文,原本都想自己想辦法寫個 extension 來解決這問題了,上網爬了下文 發現 VSCodeVim 有支援切換輸入法了。
可是它示範使用的是 im-select 而我想要只用 fcitx-remote 來解決這問題,
經過一番研究後終於找到了個設定可以解決

{
"vim.autoSwitchInputMethod.enable": true,
"vim.autoSwitchInputMethod.defaultIM": "1",
"vim.autoSwitchInputMethod.obtainIMCmd": "/usr/bin/fcitx-remote",
"vim.autoSwitchInputMethod.switchIMCmd": "/usr/bin/fcitx-remote -t {im}"
}

請記得一定要填入 fcitx-remote 的完整路徑,你可以用 which 找到,
fcitx-remote 不帶參數時會印出目前狀態 1 是未啟動 2 是啟動,
而 fcitx-remote -t 是切換狀態,後面的 {im} 是 VSCodeVim 要求要有的參數,如果不加會被認為是錯誤的設定。
不過幸好 fcitx-remote 會自己忽略掉多餘的參數,不然這個 hack 就沒辦法成功了

VSCodeVim 會在 insert mode 切到 normal mode 時去抓目前輸入法的狀態
而如果是 2 也就是啟動的,跟 defaultIM 的值不同,於是它就會執行 switchIMCmd 並在下次切回 insert mode 時也執行,這正好達成了切換輸入法的要求

--

--