-- Take from here: https://github.com/LazyVim/LazyVim/discussions/1346 -- Auto change input method between Normal and Insert mode vim.api.nvim_create_autocmd({ "InsertLeave" }, { pattern = "*", callback = function() -- record the input method in Insert mode local f_input = vim.fn.stdpath("state") .. "/last_input.tmp" os.execute("im-select > " .. f_input) -- switch back to `ABC` input method in Normal mode local config = package.config if config:sub(1, 1) == "\\" then -- windows operation print("Windows Operation to change input method") elseif config:sub(1, 1) == "/" then -- unix / linux / macos operation vim.cmd("silent ! im-select com.apple.keylayout.ABC") else -- other operation print("Unrecognized operating system to change input method") end end, }) vim.api.nvim_create_autocmd({ "InsertEnter" }, { pattern = "*", callback = function() -- change back to the last input method used in Insert mode if it is recorded local f_input = io.open(vim.fn.stdpath("state") .. "/last_input.tmp") if f_input then local input_method = f_input:read("*a") io.close(f_input) vim.cmd("silent ! im-select " .. input_method) end end, })