keaising / im-select.nvim

Switch Input Method automatically depends on Neovim's edit mode
MIT License
170 stars 25 forks source link

Astronvim里不起作用 #23

Closed lovelock closed 6 months ago

lovelock commented 6 months ago
  1. 运行环境有2个,都不行,macOS13 Apple silicon,macOS12 黑苹果。用的Astronvim
  2. homebrew安装的im-select,路径都配置好了,在命令行执行im-select com.apple.keylayout.ABC能正常切换
  3. 用了Astronvim-community的模板,在~/.config/nvim/lua/user/plugins/里创建了im-select.lua文件,内容如下
return {
    {
        "keaising/im-select.nvim",
        config = function()
            require('im_select').setup({
                -- IM will be set to `default_im_select` in `normal` mode
                -- For Windows/WSL, default: "1033", aka: English US Keyboard
                -- For macOS, default: "com.apple.keylayout.ABC", aka: US
                -- For Linux, default:
                --               "keyboard-us" for Fcitx5
                --               "1" for Fcitx
                --               "xkb:us::eng" for ibus
                -- You can use `im-select` or `fcitx5-remote -n` to get the IM's name
                default_im_select  = "com.apple.keylayout.ABC",

                -- Can be binary's name or binary's full path,
                -- e.g. 'im-select' or '/usr/local/bin/im-select'
                -- For Windows/WSL, default: "im-select.exe"
                -- For macOS, default: "im-select"
                -- For Linux, default: "fcitx5-remote" or "fcitx-remote" or "ibus"
                default_command = '/usr/local/bin/im-select', -- macOS13 Apple silicon上用的不是这个,而是/opt/homebrew那个

                -- Restore the default input method state when the following events are triggered
                -- set_default_events = { "VimEnter", "FocusGained", "InsertLeave", "CmdlineLeave" },

                -- Restore the previous used input method state when the following events
                -- are triggered, if you don't want to restore previous used im in Insert mode,
                -- e.g. deprecated `disable_auto_restore = 1`, just let it empty
                -- as `set_previous_events = {}`
                -- set_previous_events = { "InsertEnter" },

                -- Show notification about how to install executable binary when binary missed
                keep_quiet_on_no_binary = false,

                -- Async run `default_command` to switch IM or not
                async_switch_im = false
            })
        end,
    }
}

配置之后的效果就是切换到Normal模式下,再输入还是中文,比如按两下J,会进行中文输入,然后按下Shift(我的squirrel配置的中英文切换),会切换到英文模式,并且光标往前移动了2个。

反复实验多次,参数也改了几次,都不生效,就像没有配置这个一样。我平时用squirrel比较多,但即使用原生的输入法也不行,感觉是这里面配置的事件没有被触发,但又不知道怎么调试。多谢!

lovelock commented 6 months ago

我仔细分析了一下,发现启动之后这个插件并没有加载,再想想这个Astronvim的哲学,就是不需要的不加载,那么怎么让它不lazy load呢?就是在相关的配置里加上lazy = false,所以最终改成这样就好了

return {
  {
    "keaising/im-select.nvim",
    lazy = false, -- 最关键的在这里
    config = function()
      require("im_select").setup {
        -- IM will be set to `default_im_select` in `normal` mode
        -- For Windows/WSL, default: "1033", aka: English US Keyboard
        -- For macOS, default: "com.apple.keylayout.ABC", aka: US
        -- For Linux, default:
        --               "keyboard-us" for Fcitx5
        --               "1" for Fcitx
        --               "xkb:us::eng" for ibus
        -- You can use `im-select` or `fcitx5-remote -n` to get the IM's name
        default_im_select = "com.apple.keylayout.ABC",

        -- Can be binary's name or binary's full path,
        -- e.g. 'im-select' or '/usr/local/bin/im-select'
        -- For Windows/WSL, default: "im-select.exe"
        -- For macOS, default: "im-select"
        -- For Linux, default: "fcitx5-remote" or "fcitx-remote" or "ibus"
        default_command = "im-select",

        -- Restore the default input method state when the following events are triggered
        set_default_events = { "VimEnter", "FocusGained", "InsertLeave", "CmdlineLeave" },

        -- Restore the previous used input method state when the following events
        -- are triggered, if you don't want to restore previous used im in Insert mode,
        -- e.g. deprecated `disable_auto_restore = 1`, just let it empty
        -- as `set_previous_events = {}`
        set_previous_events = { "InsertEnter" },

        -- Show notification about how to install executable binary when binary missed
        keep_quiet_on_no_binary = false,

        -- Async run `default_command` to switch IM or not
        async_switch_im = true,
      }
    end,
  },
}
keaising commented 6 months ago

For anyone come here in the future:

If you use Astronvim/LazyVim or some else neovim distribution and im-select.nvim does not work, you can have a try with option:

  {
    "keaising/im-select.nvim",
    lazy = false,  -- This is the key
    config = function()
        require("im_select").setup({})
    end,
  }