V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
yuancoder
V2EX  ›  Vim

分享一个 neovim 自动切换输入法的 lua 脚本

  •  2
     
  •   yuancoder · 2023-04-07 15:03:48 +08:00 · 1563 次点击
    这是一个创建于 386 天前的主题,其中的信息可能已经有所发展或是发生改变。

    使用了一段时间 rime 输入法, 这个输入法支持 vim 模式, 用起来还是挺方便的,但是打字效果太差了。
    最后还是回归了原生的输入法,为了解决切换输入法的问题写了个 lua 脚本。
    这里没有使用 im-select 是因为 im-select 在 mac 上切换会偶尔失效,所以我用了这个 macism

    -- 记录当前输入法
    Current_input_method = vim.fn.system("/usr/local/bin/macism")
    
    -- 切换到英文输入法
    function Switch_to_English_input_method()
        Current_input_method = vim.fn.system("/usr/local/bin/macism")
        if Current_input_method ~= "com.apple.keylayout.ABC\n" then
            vim.fn.system("/usr/local/bin/macism com.apple.keylayout.ABC")
        end
    end
    
    -- 设置输入法
    function Set_input_method()
        if Current_input_method ~= "com.apple.keylayout.ABC\n" then
            vim.fn.system("/usr/local/bin/macism " .. string.gsub(Current_input_method, "\n", ""))
        end
    end
    
    -- 进入 normal 模式时切换为英文输入法
    vim.cmd([[
    augroup input_method
      autocmd!
      autocmd InsertEnter * :lua Set_input_method()
      autocmd InsertLeave * :lua Switch_to_English_input_method()
    augroup END
    ]])
    
    
    
    4 条回复    2023-08-16 22:41:34 +08:00
    knightdf
        1
    knightdf  
       2023-04-07 16:58:32 +08:00
    mac 切换输入法不就按一下 cap 么,还需要搞这么复杂?
    yuancoder
        2
    yuancoder  
    OP
       2023-04-07 17:39:09 +08:00
    @knightdf #1 每次按 esc 之前需要多按一次 cap ,进入 insert 模式又要按一次,而且 hhkb 没有 cap 键
    saikete
        3
    saikete  
       345 天前
    刚看了有人分享的 im-select 版就看到了老哥分享的 macism 版,hhhh ,支持,我也是刚刚搞好
    houshuu
        4
    houshuu  
       255 天前
    im-select.nvim 的配置里把命令换成 macism 就非常好用了。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1486 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 16:57 · PVG 00:57 · LAX 09:57 · JFK 12:57
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.