Files
vim-im-switch/README-im-select.md
2026-07-06 14:20:29 +08:00

277 lines
7.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# vim-im-switch-select.vim
> 使用 `im-select` 而不是 `fcitx-remote` 的 Vim 输入法自动切换插件
## 概述
`vim-im-switch-select.vim``vim-im-switch.vim` 的变体版本,使用 `im-select` 工具而不是 `fcitx-remote` 来控制输入法切换。
## 主要区别
| 特性 | vim-im-switch.vim | vim-im-switch-select.vim |
|------|------------------|-------------------------|
| 输入法工具 | fcitx-remote | im-select |
| 适用平台 | macOS, Linux | 主要是 macOS |
| 获取当前输入法 | `fcitx-remote -n` | `im-select` |
| 切换输入法 | `fcitx-remote -s <IM>` | `im-select <IM>` |
| 全局变量前缀 | `g:fcitx_*` | `g:imselect_*` |
| 函数前缀 | `Fcitx*` | `IMSelect*` |
## 功能特性
**自动切换输入法**:在 Normal 模式和 Insert 模式之间切换时自动切换输入法
**提前保存状态**:使用 `InsertLeavePre` 在离开 Insert 前保存输入法状态
**异步静默处理**:使用 `job_start()` 异步执行,并屏蔽命令输出,避免阻塞和乱码
**macOS 稳定恢复**:进入 Insert 后延迟恢复中文输入源,并进行一次兜底重试
## 安装要求
### 1. 安装 im-select
本项目已经包含了 `im-select` 可执行文件(适用于 macOS ARM64
如果你需要其他版本,可以从这里获取:
- [im-select GitHub](https://github.com/daipeihust/im-select)
或者使用项目中的 `im-select` 文件:
```bash
# 确保 im-select 有执行权限
chmod +x /path/to/vim-im-switch/im-select
# 将 im-select 添加到 PATH可选
sudo cp /path/to/vim-im-switch/im-select /usr/local/bin/
```
### 2. 验证 im-select 安装
```bash
# 如果在项目目录中
./im-select
# 如果在 PATH 中
im-select
```
应该输出当前的输入法名称,例如:
```
com.apple.keylayout.ABC
```
## 安装插件
### 方法 1复制到 Vim 插件目录
```bash
mkdir -p ~/.vim/plugin
cp vim-im-switch-select.vim ~/.vim/plugin/
```
### 方法 2在 .vimrc 中 source
```vim
" 在 .vimrc 中添加
source /Users/gavin/vim-im-switch/vim-im-switch-select.vim
```
### 方法 3使用插件管理器
**vim-plug**:
```vim
Plug 'yourusername/vim-im-switch', { 'rtp': 'vim-im-switch-select.vim' }
```
## 配置
`.vimrc` 中添加配置(可选):
```vim
" 英文输入法 ID默认值
let g:imselect_english_im = 'com.apple.keylayout.ABC'
" 中文输入法 ID默认值系统自带简体拼音
" macOS 更新后建议显式配置,不依赖自动检测
let g:imselect_chinese_im = 'com.apple.inputmethod.SCIM.ITABC'
```
### 如何获取输入法 ID
```bash
# 切换到你想要的输入法,然后运行:
./im-select
# 或
im-select
```
常见的输入法 ID
- 美国英文:`com.apple.keylayout.ABC`
- 简体拼音:`com.apple.inputmethod.SCIM.ITABC`
- 搜狗输入法:`com.sogou.inputmethod.sogou`
- 百度输入法:`com.baidu.inputmethod.BaiduIM.pinyin`
- 微信输入法:`com.tencent.inputmethod.wetype.pinyin`
## 使用方法
插件安装后会自动工作,无需手动操作:
1. **进入 Insert 模式** (`i`, `a`, `o` 等)
- 延迟切换到中文输入法
- 约 200ms 后再次确认中文输入法,降低 macOS 状态未刷新导致的偶发失败
2. **退出 Insert 模式** (`ESC`)
-`InsertLeavePre` 提前保存当前输入法状态
-`InsertLeave` 自动切换到英文输入法
3. **macOS 行为说明**
- `im-select` 控制的是 macOS 输入源 ID
- 新版 macOS 中文输入法可能存在“图标为中文但内部仍输入英文”的状态
- 当前稳定版不模拟 `Ctrl-Space` / `Caps Lock`,避免快捷键被 Vim 接收导致退出 Insert 模式
## 测试
### 运行自动化测试
```bash
# 测试 im-select 功能
./test-im-select.sh
# 在 Vim 中测试插件
./run-vim-test.sh
```
### 手动测试步骤
1. 启动 Vim 并打开测试文件:
```bash
vim test-im-switch.txt
```
2. 按 `i` 进入 Insert 模式
3. 切换到中文输入法,输入一些中文
4. 按 `ESC` 退出到 Normal 模式
- 观察输入法是否切换到英文
5. 再次按 `i` 进入 Insert 模式
- 观察输入法是否自动恢复到中文
6. 在 Insert 模式下,手动切换到英文输入法,输入一些英文
7. 按 `ESC` 退出到 Normal 模式
8. 再次按 `i` 进入 Insert 模式
- 当前稳定版会恢复到配置的中文输入法
- 如果需要保持英文,请临时禁用插件或调整 `g:imselect_chinese_im` 策略
## 调试
### 查看插件是否加载
```vim
:echo exists('g:imselect_loaded')
" 应该输出 1
```
### 查看保存的输入法状态
```vim
:echo g:imselect_last_insert_im_name
" 输出上次 Insert 模式的输入法名称
```
### 手动测试函数
```vim
" 切换到英文
:call IMSelect2en()
" 恢复输入法
:call IMSelect2zh()
```
### 查看 Vim 消息
```vim
:messages
```
## 故障排除
### 插件没有效果
1. **检查 im-select 是否可执行**
```bash
which im-select
# 或
ls -l ./im-select
```
2. **检查 Vim 版本是否支持 job_start()**
```vim
:echo has('job')
" 应该输出 1
```
3. **检查是否有错误消息**
```vim
:messages
```
### 输入法切换延迟
- 这是正常的,因为进入 Insert 后使用了 `timer_start()` 延迟恢复
- 当前稳定版会在进入 Insert 后约 80ms 切中文,并在约 200ms 后兜底确认
- 如果机器较慢或 macOS 状态刷新较慢,可以在插件中适当调大延迟值
### 输入法图标是中文但实际输入英文
这是新版 macOS 中文输入法可能出现的内部状态问题:`im-select` 已经把输入源切到中文,但中文输入法内部仍处于英文输入状态。
当前稳定版采取的处理方式:
1. 不模拟 `Ctrl-Space`、`Caps Lock`、`Shift` 等按键,避免这些按键被 Vim 接收后影响模式。
2. 进入 Insert 后延迟切换中文输入源。
3. 再进行一次中文输入源兜底确认。
如果仍偶发出现该问题,优先调整延迟时间,不建议重新加入系统快捷键模拟。
### 输入法 ID 不正确
```bash
# 切换到你的中文输入法,然后运行:
./im-select
# 将输出的 ID 配置到 .vimrc
let g:imselect_chinese_im = '你的输入法ID'
```
## 与原版的兼容性
`vim-im-switch-select.vim` 和 `vim-im-switch.vim` 可以共存,但不建议同时使用。它们使用不同的:
- 全局变量名(`g:imselect_*` vs `g:fcitx_*`
- 函数名(`IMSelect*` vs `Fcitx*`
- 自动命令组名(`IMSelect` vs `Fcitx`
## 文件清单
- `vim-im-switch-select.vim` - 插件主文件
- `im-select` - im-select 可执行文件macOS ARM64
- `test-im-select.sh` - 自动化测试脚本
- `run-vim-test.sh` - Vim 交互测试脚本
- `test-im-switch.txt` - 测试文件
- `README-im-select.md` - 本说明文件
## 参考链接
- [im-select GitHub](https://github.com/daipeihust/im-select)
- [原版 vim-im-switch 项目](https://github.com/yourusername/vim-im-switch)
## 许可证
MIT License
---
**如有问题或建议,欢迎提交 Issue**