Skip to content

Instantly share code, notes, and snippets.

@thyeem
Created January 28, 2022 03:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thyeem/ea495ff1380b4d5fcd18b7038f1939ff to your computer and use it in GitHub Desktop.
Save thyeem/ea495ff1380b4d5fcd18b7038f1939ff to your computer and use it in GitHub Desktop.
Less annoying way to switch input method when using evil-mode
;; prerequisites: im-select (https://github.com/daipeihust/im-select)
;;
;; install it!
;; $ curl -Ls https://raw.githubusercontent.com/daipeihust/im-select/master/install_mac.sh | sh
;;
;; make sure that Emacs can execute 'im-select'. (Is it on the right $PATH?)
(setq english-im "com.apple.keylayout.ABC")
(setq previous-im "com.apple.keylayout.ABC")
(defun use-english-im ()
"use english input method"
(interactive)
(call-process-shell-command (concat "im-select " english-im)))
(defun set-previous-im ()
"set input method to the previously saved one"
(interactive)
(setq previous-im
(substring (shell-command-to-string "im-select") 0 -1)))
(defun use-previous-im ()
"use the previous input method"
(interactive)
(call-process-shell-command (concat "im-select " previous-im)))
(add-hook 'evil-normal-state-entry-hook 'use-english-im)
(add-hook 'evil-insert-state-entry-hook 'use-previous-im)
(add-hook 'evil-insert-state-exit-hook 'set-previous-im)
(add-hook 'evil-replace-state-entry-hook 'use-previous-im)
(add-hook 'evil-replace-state-exit-hook 'set-previous-im)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment