I already have one way to do this:

function input-switch-darwin() {
    cliclick kd:ctrl kp:space ku:ctrl

But this is very slow:

❯ time cliclick kd:ctrl kp:space ku:ctrl
cliclick kd:ctrl kp:space ku:ctrl  0.02s user 0.03s system 13% cpu 0.434 total; max RSS 7088
  • 2
    Have a look at this answer Change OSX keyboard layout(“input source”) programmatically via terminal or AppleScript?. I just tested it, the code under "Additionally", and it works faster than: cliclick kd:ctrl kp:space ku:ctrl. That said, while I'm in Terminal, pressing ctrl+space changes between the two layouts I have, so why can't you just physically press the keys? Oct 7, 2020 at 15:11
  • @user3439894 : As you said, it changes between two layouts. If you have more than 2, it is awkward to switch to a certain different one, because you have to press ctrl-space repeatedly until you get to the desired one. I would love to assign a specific key combination for each layout, for instance Ctrl-Alt-1 for the first layout, Ctrl-Alt-2 for the second one and so on. From what I know, this is not possible. Nov 20 at 12:14

2 Answers 2


Thanks to the tip by @user3439894, I found an answer (you need ripgrep and https://github.com/myshov/xkbswitch-macosx installed):

ec() print -r -- "$@"
ecerr() { ec "$@" 1>&2 }

function input-lang-set-darwin() {
    # https://github.com/myshov/xkbswitch-macosx
    # `hyperfine --warmup 5 'xkbswitch -s 3' 'xkbswitch -s 0' 'xkbswitch -se Persian-ISIRI2901' 'xkbswitch -se US'`
    # they all ran about the same 77ms
    local wanted="${1:l}" to='US'
    case "$wanted" in
        en*) to=US ;;
        fa*|per*) to='Persian-ISIRI2901' ;;
            case "$(input-lang-get-darwin)" in
                U.S.) to='Persian-ISIRI2901' ;;
                Persian*) to='US' ;;
        *) ecerr "Not supported" ; return 1 ;;
    xkbswitch -se "$to"
function input-lang-get-darwin() {
    defaults read ~/Library/Preferences/com.apple.HIToolbox.plist AppleSelectedInputSources | command rg -e '"KeyboardLayout Name" = "([^"]*)"' --replace '$1' --only-matching --color never

Another option is https://github.com/Lutzifer/keyboardSwitcher, but I couldn't build it without xcode.

  • Could you explain why you would not just use Control Space ? Oct 8, 2020 at 0:05
  • @TomGewecke I wanted to add an input toggle button to the touchbar. Another use was to automatically switch to English if I type non-English on the terminal, but I'm rethinking that. Also, it's not as easy for me to press ctrl-space, as my hands are somewhat disabled.
    – HappyFace
    Oct 8, 2020 at 0:08
  • A single thing to tap on the touch bar instead of two keys sounds good to me. Especially if I use three or more keyboards and want to pick any with a single tap from three or more Touch Bar icons.
    – WGroleau
    Mar 6, 2021 at 18:48

@Tom Gewecke in my case, I use three input language source (so I use Finnish, English and Russian keyboards). And I'm using something like punto switcher, but it can not detect differences between Finnish and English.

Also, if I just use Comand ⌘ + space, then I don't see which one of three is in use now as I'm a partially sighted. I'd use the "run shell script" in the iOS/mac shortcuts script, and the iOS shortcut's script I'd run by adding a hotkey to it.

  • Hey! This answer may be beneficial to the thread if it included the shortcut's script. Try to explain how your answer works for this specific problem and how it answers the question appropriately.
    – Thinkr
    Nov 18 at 11:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .