update at 2025-11-09 22:04:12

This commit is contained in:
douboer
2025-11-09 22:04:12 +08:00
parent 36c97b0616
commit 172f68ac37
14 changed files with 469 additions and 1431 deletions

View File

@@ -11,6 +11,24 @@
---
## [2.0.3] - 2025-11-09
### 修复
#### Obsidian 插件
- **修复 Normal 模式下按 ESC 触发输入法切换的问题**
- 问题:在 Normal 模式下按 ESC 会意外触发输入法切换(从中文切到英文,再按又从英文切到英文)
- 原因:`handleKeyDown` 函数处理 ESC 键时没有检查当前 Vim 模式
- 解决:增加模式检查,只在 Insert 或 Replace 模式下按 ESC 时才保存输入法状态并切换到英文
- 效果:与原生 Vim 插件行为保持一致Normal 模式下按 ESC 不会改变输入法
### 改进
- 提高了模式检测的准确性
- 优化了 ESC 键的处理逻辑
---
## [2.0.0] - 2025-11-04
### 新增功能

View File

@@ -11,6 +11,24 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
---
## [2.0.3] - 2025-11-09
### Fixed
#### Obsidian Plugin
- **Fixed input method switching triggered by ESC in Normal mode**:
- Issue: Pressing ESC in Normal mode would unexpectedly trigger input method switching (from Chinese to English, then English to English on second press)
- Cause: `handleKeyDown` function didn't check current Vim mode when handling ESC key
- Solution: Added mode check to only save IM state and switch to English when ESC is pressed in Insert or Replace mode
- Result: Behavior now consistent with native Vim plugin - ESC in Normal mode doesn't change input method
### Improved
- Enhanced accuracy of mode detection
- Optimized ESC key handling logic
---
## [2.0.0] - 2025-11-04
### Added

256
README.md
View File

@@ -1,16 +1,26 @@
# Vim 输入法自动切换插件
[![Version](https://img.shields.io/badge/version-2.0.3-blue.svg)](./CHANGELOG.md)
[![Platform](https://img.shields.io/badge/platform-macOS%20%7C%20Linux%20%7C%20Windows-lightgrey.svg)](#安装要求)
[![License](https://img.shields.io/badge/license-MIT-green.svg)](./LICENSE.txt)
[English](./README_en.md) | 中文
这是为 原生Vim 和 Obsidian 编辑器的 Vim 模式设计的输入法自动切换插件,包含:
- **Obsidian 插件**:适用于 Obsidian 编辑器的 Vim 模式
- **Vim 插件**:适用于原生 Vim/NeoVim 编辑器
> 🚀 **智能输入法管理工具** - 为 Vim 用户打造的无感知输入法切换体验
## 功能简介
这是为 **原生 Vim****Obsidian 编辑器** 的 Vim 模式设计的智能输入法自动切换插件,包含:
- 自动切换输入法:在 Vim 的 Normal 模式和 Insert 模式之间切换时,自动切换输入法
- 智能记忆:记住上次 Insert 模式退出时的输入法状态,下次进入时自动恢复
- 无缝体验:在 Insert 模式下正常输入文本时不会触发输入法切换
- 🎯 **Obsidian 插件**:适用于 Obsidian 编辑器的 Vim 模式
- **Vim 插件**:适用于原生 Vim/NeoVim 编辑器
- 🌍 **跨平台支持**macOS、Linux、Windows 全平台兼容
## ✨ 功能亮点
- 🔄 **自动切换输入法**:在 Vim 的 Normal 模式和 Insert 模式之间切换时,自动切换输入法
- 🧠 **智能状态记忆**:记住上次 Insert 模式退出时的输入法状态,下次进入时自动恢复
- 🎭 **无感知体验**:完全静默切换,无 UI 闪烁或延迟
- 🌐 **中英混合友好**:完美支持中英文混合输入场景
-**多重保障机制**:三层检测确保切换的可靠性
## 核心特性
### 1. 模式切换自动化
@@ -23,10 +33,19 @@
- 支持中英文混合输入场景
### 3. 智能检测机制
插件使用多重检测机制确保可靠性:
- **键盘事件监听**(主要):使用事件捕获模式监听 ESC 和 Insert 按键
- **CodeMirror 事件**(辅助):监听 vim-mode-change 事件
- **定时轮询**兜底100ms 轮询检测模式变化
插件采用**三重检测机制**确保可靠性:
| 检测方式 | 优先级 | 说明 |
|---------|--------|------|
| 🎯 **键盘事件监听** | 最高 | 使用事件捕获模式监听 ESC 和 Insert 按键,响应最快 |
| 🔧 **CodeMirror 事件** | 中等 | 监听 vim-mode-change 事件,处理非按键触发的模式切换 |
| 🔄 **定时轮询** | 兜底 | 100ms 轮询检测模式变化,作为最后保障机制 |
### 4. 技术特性
-**异步处理**:使用 `job_start()` 异步执行,避免 UI 阻塞
- 🛡️ **防抖机制**100ms 防抖避免重复处理
- 🎯 **事件捕获优化**:使用 capture 模式确保最快响应
- 🔧 **自动检测**:智能检测和配置中文输入法
## 安装要求
@@ -56,22 +75,21 @@ sudo pacman -S fcitx
```
#### Windows
下载并安装 AutoHotKey 版本的 fcitx-remote
- [下载 fcitx-remote.exe](https://github.com/yuanotes/obsidian-vim-im-switch-plugin/releases/download/1.0.3/fcitx-remote.exe)
- 将 exe 文件放到系统 PATH 路径中
使用项目内置的 AutoHotkey 脚本
1. 安装 [AutoHotkey](https://www.autohotkey.com/)
2. 使用项目中的 `fcitx-remote.ahk` 脚本
3. 或下载编译好的版本:[fcitx-remote.exe](https://github.com/yuanotes/obsidian-vim-im-switch-plugin/releases/download/1.0.3/fcitx-remote.exe)
4. 将 exe 文件放到系统 PATH 路径中
## 安装插件
### Obsidian 插件安装
1. 下载插件文件到 Obsidian 插件目录:
- 方法一:
```bash
cd /path/to/your/vault/.obsidian/plugins/
git clone https://biboer.cn/gitea/gavin/vim-im-switch
git clone https://github.com/yourusername/vim-im-switch.git
```
- 方法二(简单):
把 vim-im-switch 目录复制到/path/to/your/vault/.obsidian/plugins/目录下。
2. 在 Obsidian 中启用插件:
- 打开设置 → 社区插件 → 浏览
@@ -88,7 +106,7 @@ sudo pacman -S fcitx
1. 复制插件文件到 Vim 配置目录:
```bash
mkdir -p ~/.vim/plugin
cp fcitx-osx.vim ~/.vim/plugin/
cp vim-im-switch.vim ~/.vim/plugin/
```
2. 重启 Vim插件会自动加载
@@ -103,14 +121,32 @@ sudo pacman -S fcitx
" let g:fcitx_chinese_im = 'com.tencent.inputmethod.wetype.pinyin'
```
### 一键部署(推荐)
#### 使用插件管理器安装
使用部署脚本同时安装两个插件:
**vim-plug**:
```vim
Plug 'yourusername/vim-im-switch'
```
**Vundle**:
```vim
Plugin 'yourusername/vim-im-switch'
```
### 🚀 一键部署(推荐)
使用部署脚本同时安装 Obsidian 插件和 Vim 插件:
```bash
# 克隆项目
git clone https://github.com/yourusername/vim-im-switch.git
cd vim-im-switch
# 构建并部署
npm install
./deploy.sh
```
Note: v2.0.2 fixes a terminal-specific title/flash issue when using terminal Vim (see CHANGELOG for details).
> 💡 **提示**: v2.0.2 修复了终端 Vim 中的标题闪烁问题,详见 [更新日志](./CHANGELOG.md)
## 使用方法
@@ -162,51 +198,157 @@ graph TD
插件维护一个状态机,跟踪 Vim 模式和输入法状态,在模式转换时自动切换输入法,同时保留用户偏好设置。
## 故障排除
## 🔧 故障排除
### 问题:插件没有效果
1. 检查 fcitx-remote 是否正确安装
2. 检查插件日志(开发者控制台)
3. 验证输入法名称是否与设置匹配
### 问题:输入法切换不正确
1. 确认设置中的输入法名称正确
2. 手动切换输入法后,在开发者控制台查看日志
3. 检查是否有其他插件冲突
## 开发
### 构建
### 快速诊断
使用内置诊断脚本:
```bash
./diagnose-im.sh
```
### 常见问题
#### 🚫 插件没有效果
1. **检查依赖**:确认 fcitx-remote 是否正确安装
```bash
# macOS
fcitx-remote -n
# Linux
which fcitx-remote
# Windows
fcitx-remote.exe
```
2. **检查日志**
- **Obsidian**: 开发者控制台 (Ctrl+Shift+I)
- **Vim**: `:messages` 命令
3. **验证配置**:确认输入法名称与系统设置匹配
#### ⚠️ 输入法切换不正确
1. **获取正确的输入法名称**
```bash
# 切换到中文输入法后执行
fcitx-remote -n
```
2. **手动测试**
```bash
# 切换到英文
fcitx-remote -s com.apple.keylayout.ABC
# 切换到中文(替换为你的输入法名称)
fcitx-remote -s com.tencent.inputmethod.wetype.pinyin
```
3. **检查冲突**:暂时禁用其他 Vim 插件测试
#### 🐛 其他问题
- **权限问题**:确保 fcitx-remote 有执行权限
- **路径问题**:检查 fcitx-remote 是否在 PATH 中
- **版本兼容**:确认 Vim 版本支持 `job_start()` (Vim 8+)
## 🛠️ 开发指南
### 环境要求
- Node.js 14+
- TypeScript 4.2+
- Rollup (构建工具)
### 构建项目
```bash
# 安装依赖
npm install
# 开发模式(监听文件变化)
npm run dev
# 生产构建
npm run build
```
### 部署
一键部署 Obsidian 插件和 Vim 插件:
```bash
./deploy.sh
### 项目结构
```
vim-im-switch/
├── main.ts # Obsidian 插件主文件
├── vim-im-switch.vim # Vim 插件文件
├── fcitx-remote.ahk # Windows 支持脚本
├── fcitx-remote-for-osx/ # macOS 支持工具
├── deploy.sh # 一键部署脚本
├── diagnose-im.sh # 诊断脚本
└── vim-im-switch-plugin/ # 构建输出目录
```
### 调试
### 调试方法
#### Obsidian 插件
#### Obsidian 插件调试
插件会在控制台输出关键日志:
- `Loading plugin...` - 插件加载
- `ESC → English` - ESC 切换到英文
- `→ Chinese` - 切换到中文
- `Error...` - 错误信息
#### Vim 插件
在 Vim 中查看消息:
```vim
:messages
```
🚀 [VimIMSwitch] Loading plugin...
🔤 [VimIMSwitch] ESC → English (saved Chinese)
🈳 [VimIMSwitch] → Chinese
❌ [VimIMSwitch] Error: ...
```
## 更新日志
查看 [CHANGELOG.md](./CHANGELOG.md) 获取详细的版本更新历史。
#### Vim 插件调试
```vim
" 查看插件消息
:messages
## 相关链接
- [更新日志](./CHANGELOG.md)
- [English Documentation](./README_en.md)
- [fcitx-remote-for-osx](https://github.com/xcodebuild/fcitx-remote-for-osx)
" 检查插件是否加载
:echo exists('g:fcitx_remote')
" 手动测试函数
:call Fcitx2en()
:call Fcitx2zh()
```
### 贡献指南
1. Fork 项目
2. 创建功能分支:`git checkout -b feature/amazing-feature`
3. 提交更改:`git commit -m 'Add amazing feature'`
4. 推送分支:`git push origin feature/amazing-feature`
5. 提交 Pull Request
## 📋 版本历史
| 版本 | 日期 | 主要更新 |
|------|------|----------|
| [v2.0.3](./CHANGELOG.md#203---2025-11-09) | 2025-11-09 | 修复 Normal 模式 ESC 键问题 |
| [v2.0.2](./CHANGELOG.md#202---2025-11-04) | 2025-11-04 | 修复终端兼容性问题 |
| [v2.0.0](./CHANGELOG.md#200---2025-11-04) | 2025-11-04 | 新增 Vim 原生插件支持 |
| [v1.0.8](./CHANGELOG.md#108---2025-01-04) | 2025-01-04 | 智能状态记忆功能 |
| [v1.0.0](./CHANGELOG.md#100---2024-06-01) | 2024-06-01 | 首次发布 |
查看 [完整更新日志](./CHANGELOG.md) 获取详细的版本更新历史。
## 🔗 相关链接
- 📖 [English Documentation](./README_en.md)
- 📝 [更新日志](./CHANGELOG.md)
- 🛠️ [fcitx-remote-for-osx](https://github.com/xcodebuild/fcitx-remote-for-osx)
- 🐛 [问题反馈](https://github.com/yourusername/vim-im-switch/issues)
- 💡 [功能建议](https://github.com/yourusername/vim-im-switch/discussions)
## 📄 许可证
本项目采用 [MIT 许可证](./LICENSE.txt)。
## 🙏 致谢
感谢以下项目和贡献者:
- [fcitx-remote-for-osx](https://github.com/xcodebuild/fcitx-remote-for-osx) - macOS 输入法控制工具
- [Obsidian](https://obsidian.md/) - 强大的知识管理工具
- [Vim](https://www.vim.org/) / [NeoVim](https://neovim.io/) - 经典的文本编辑器
---
<div align="center">
**如果这个项目对你有帮助,请给个 ⭐ Star**
Made with ❤️ for Vim users
</div>

View File

@@ -1,580 +0,0 @@
# Obsidian Vim 输入法自动切换插件# Obsidian Vim 输入法自动切换插件
[English](./README_en.md) | 中文[English](#english) | [中文](#中文)
一个为 Obsidian 编辑器的 Vim 模式设计的输入法自动切换插件。---
## 功能简介## 中文
- **自动切换输入法**:在 Vim 的 Normal 模式和 Insert 模式之间切换时,自动切换输入法### 功能简介
- **智能记忆**:记住上次 Insert 模式退出时的输入法状态,下次进入时自动恢复
- **无缝体验**:在 Insert 模式下正常输入文本时不会触发输入法切换这是一个为 Obsidian 编辑器的 Vim 模式设计的输入法自动切换插件。它能够:
## 核心特性- **自动切换输入法**:在 Vim 的 Normal 模式和 Insert 模式之间切换时,自动切换输入法
- **智能记忆**:记住上次 Insert 模式退出时的输入法状态,下次进入时自动恢复
### 1. 模式切换自动化- **无缝体验**:在 Insert 模式下正常输入文本时不会触发输入法切换
- **进入 Normal 模式**(按 ESC 或其他命令)→ 自动切换到英文输入法
- **进入 Insert 模式**(按 i, a, o 等)→ 自动恢复上次的输入法状态### 核心特性
### 2. 输入法状态记忆#### 1. 模式切换自动化
- 退出 Insert 模式时,自动检测并保存当前的输入法(中文/英文)- **进入 Normal 模式**(按 ESC 或其他命令)→ 自动切换到英文输入法
- 下次进入 Insert 模式时,自动恢复到上次保存的输入法状态- **进入 Insert 模式**(按 i, a, o 等)→ 自动恢复上次的输入法状态
- 支持中英文混合输入场景
#### 2. 输入法状态记忆
### 3. 智能检测机制- 退出 Insert 模式时,自动检测并保存当前的输入法(中文/英文)
插件使用多重检测机制确保可靠性:- 下次进入 Insert 模式时,自动恢复到上次保存的输入法状态
- **键盘事件监听**(主要):使用事件捕获模式监听 ESC 和 Insert 按键- 支持中英文混合输入场景
- **CodeMirror 事件**(辅助):监听 vim-mode-change 事件
- **定时轮询**兜底100ms 轮询检测模式变化#### 3. 智能检测机制
插件使用多重检测机制确保可靠性:
## 快速开始- **键盘事件监听**(主要):使用事件捕获模式监听 ESC 和 Insert 按键
- **CodeMirror 事件**(辅助):监听 vim-mode-change 事件
### 安装要求- **定时轮询**兜底100ms 轮询检测模式变化
#### macOS### 安装要求
```bash
brew install fcitx-remote-for-osx#### macOS
```1. 安装 [fcitx-remote-for-osx](https://github.com/xcodebuild/fcitx-remote-for-osx)
```bash
#### Linux brew install fcitx-remote-for-osx
```bash ```
# Ubuntu/Debian
sudo apt-get install fcitx2. 确认安装成功:
```bash
# Fedora fcitx-remote -n
sudo dnf install fcitx # 应该输出当前输入法的名称com.apple.keylayout.ABC
```
# Arch Linux
sudo pacman -S fcitx#### Linux
```通过系统包管理器安装 `fcitx` 输入法:
```bash
#### Windows# Ubuntu/Debian
下载 [fcitx-remote.exe](https://github.com/yuanotes/obsidian-vim-im-switch-plugin/releases/download/1.0.3/fcitx-remote.exe) 并放到系统 PATH 路径中sudo apt-get install fcitx
### 安装插件# Fedora
sudo dnf install fcitx
1. 下载插件到 Obsidian 插件目录:
```bash# Arch Linux
cd /path/to/your/vault/.obsidian/plugins/sudo pacman -S fcitx
git clone https://github.com/yourusername/vim-im-switch.git```
```
#### Windows
2. 在 Obsidian 中启用插件:下载并安装 AutoHotKey 版本的 fcitx-remote
- 打开设置 → 社区插件- [下载 fcitx-remote.exe](https://github.com/yuanotes/obsidian-vim-im-switch-plugin/releases/download/1.0.3/fcitx-remote.exe)
- 找到 "Vim Input Method Switch" 并启用- 将 exe 文件放到系统 PATH 路径中
3. 配置输入法(可选):### 安装插件
- 设置英文输入法(默认:`com.apple.keylayout.ABC`
- 设置中文输入法默认自动检测1. 下载插件文件到 Obsidian 插件目录:
```bash
## 使用方法 cd /path/to/your/vault/.obsidian/plugins/
git clone https://github.com/yourusername/vim-im-switch.git
### 基本使用场景 ```
**中文输入**2. 在 Obsidian 中启用插件:
``` - 打开设置 → 社区插件 → 浏览
按 i → 进入 Insert 模式 → 输入法切换到中文(如果上次是中文) - 找到 "Vim Input Method Switch"
输入中文内容 - 点击启用
按 ESC → 退出到 Normal 模式 → 输入法切换到英文
```3. 配置输入法:
- 打开插件设置
**英文输入** - 设置英文输入法(默认:`com.apple.keylayout.ABC`
``` - 设置中文输入法(默认:自动检测)
按 i → 进入 Insert 模式 → 输入法保持英文(如果上次是英文)
输入英文内容### 使用方法
按 ESC → 退出到 Normal 模式 → 输入法保持英文
```#### 基本使用
**中英混合**1. **中文输入**
``` ```
按 i → 自动恢复上次的输入法 按 i → 进入 Insert 模式 → 输入法切换到中文(如果上次是中文)
输入中文,然后手动切换到英文继续输入 输入中文内容
按 ESC → 保存当前输入法状态(英文) 按 ESC → 退出到 Normal 模式 → 输入法切换到英文
按 i → 自动恢复英文输入法 ```
```
2. **英文输入**
### 支持的 Vim 命令 ```
按 i → 进入 Insert 模式 → 输入法保持英文(如果上次是英文)
- **进入 Insert 模式**`i`, `I`, `a`, `A`, `o`, `O`, `s`, `S`, `c`, `C` 输入英文内容
- **退出 Insert 模式**`ESC`, 以及其他触发 Normal 模式的命令 按 ESC → 退出到 Normal 模式 → 输入法保持英文
```
## 工作原理
3. **中英混合**
```mermaid ```
graph TD 按 i → 自动恢复上次的输入法
A[Normal 模式<br/>英文输入法] -->|按 i/a/o 等| B[检测模式切换] 输入中文,然后手动切换到英文继续输入
B --> C[恢复上次保存的<br/>输入法状态] 按 ESC → 保存当前输入法状态(英文)
C --> D[Insert 模式<br/>自动恢复的输入法:<br/>中文/英文] 按 i → 自动恢复英文输入法
D -->|按 ESC| E[保存当前输入法<br/>状态 中/英] ```
E --> F[切换到英文输入法]
F --> A#### 支持的 Vim 命令
style A fill:#e1f5ff,stroke:#01579b,stroke-width:2px- **进入 Insert 模式**`i`, `I`, `a`, `A`, `o`, `O`, `s`, `S`, `c`, `C`
style D fill:#fff9c4,stroke:#f57f17,stroke-width:2px- **退出 Insert 模式**`ESC`, 以及其他触发 Normal 模式的命令
style E fill:#f3e5f5,stroke:#4a148c,stroke-width:2px
style F fill:#e8f5e9,stroke:#1b5e20,stroke-width:2px### 工作原理
```
```mermaid
### 技术细节graph TD
A[Normal 模式<br/>英文输入法] -->|按 i/a/o 等| B[检测模式切换]
**输入法检测** B --> C[恢复上次保存的<br/>输入法状态]
- 使用 `fcitx-remote -n` 获取当前输入法名称 C --> D[Insert 模式<br/>自动恢复的输入法:<br/>中文/英文]
- 对比设置中的中英文输入法名称来判断状态 D -->|按 ESC| E[保存当前输入法<br/>状态 中/英]
E --> F[切换到英文输入法]
**事件处理** F --> A
- **键盘事件**:使用 `capture` 模式尽早捕获按键
- **防抖机制**100ms 内的重复事件会被忽略
- **异步处理**:所有输入法切换操作都是异步的,不阻塞编辑 style A fill:#e1f5ff,stroke:#01579b,stroke-width:2px
style D fill:#fff9c4,stroke:#f57f17,stroke-width:2px
**状态管理** style E fill:#f3e5f5,stroke:#4a148c,stroke-width:2px
- `currentVimMode`:当前 Vim 模式normal/insert/visual style F fill:#e8f5e9,stroke:#1b5e20,stroke-width:2px
- `lastInsertModeIMStatus`:上次 Insert 模式的输入法状态```
- `imStatus`当前输入法状态Activate=中文Deactivate=英文)
### 技术细节
## 故障排除
#### 输入法检测
### 问题:插件没有效果- 使用 `fcitx-remote -n` 获取当前输入法名称
1. 检查 fcitx-remote 是否正确安装:- 对比设置中的中英文输入法名称来判断状态
```bash
which fcitx-remote#### 事件处理
fcitx-remote -n- **键盘事件**:使用 `capture` 模式尽早捕获按键
```- **防抖机制**100ms 内的重复事件会被忽略
- **异步处理**:所有输入法切换操作都是异步的,不阻塞编辑
2. 检查插件日志(开发者控制台):
- 按 `Cmd+Option+I` (macOS) 或 `Ctrl+Shift+I` (Windows/Linux) 打开开发者控制台#### 状态管理
- 查看是否有 `[VimIMSwitch]` 开头的日志- `currentVimMode`:当前 Vim 模式normal/insert/visual
- `lastInsertModeIMStatus`:上次 Insert 模式的输入法状态
3. 验证输入法名称:- `imStatus`当前输入法状态Activate=中文Deactivate=英文)
```bash
# 切换到中文输入法后执行### 故障排除
fcitx-remote -n
# 输出应该匹配插件设置中的中文输入法名称#### 问题:插件没有效果
```1. 检查 fcitx-remote 是否正确安装:
```bash
### 问题:输入法切换不正确 which fcitx-remote
1. 确认设置中的输入法名称正确 fcitx-remote -n
2. 手动切换输入法后,在开发者控制台查看日志 ```
3. 检查是否有其他插件冲突
2. 检查插件日志(开发者控制台):
### 问题ESC 键需要按多次 ```
- 这个问题已在最新版本中修复 按 Cmd+Option+I 打开开发者控制台
- 如果仍然存在,请更新到最新版本 查看是否有 [VimIMSwitch] 开头的日志
```
## 开发
3. 验证输入法名称:
### 构建 ```bash
```bash # 切换到中文输入法后执行
npm install fcitx-remote -n
npm run build # 输出应该匹配插件设置中的中文输入法名称
``` ```
### 部署#### 问题:输入法切换不正确
```bash1. 确认设置中的输入法名称正确
./deploy.sh2. 手动切换输入法后,在开发者控制台查看日志
```3. 检查是否有其他插件冲突
### 调试#### 问题ESC 键需要按多次
插件会在控制台输出关键日志:- 这个问题已在最新版本中修复
- `🚀 Loading plugin...` - 插件加载- 如果仍然存在,请更新到最新版本
- `🔤 ESC → English` - ESC 切换到英文
- `🈳 → Chinese` - 切换到中文### 开发
- `❌ Error...` - 错误信息
#### 构建
## 更新日志```bash
npm install
查看 [CHANGELOG.md](./CHANGELOG.md) 获取详细的版本更新历史。npm run build
```
## 许可证
#### 部署
MIT License```bash
./deploy.sh
## 相关链接```
- [更新日志](./CHANGELOG.md)#### 调试
- [English Documentation](./README_en.md)插件会在控制台输出关键日志:
- [fcitx-remote-for-osx](https://github.com/xcodebuild/fcitx-remote-for-osx)- `🚀 Loading plugin...` - 插件加载
- `🔤 ESC → English` - ESC 切换到英文
- `🈳 → Chinese` - 切换到中文
- `❌ Error...` - 错误信息
### 更新日志
#### v1.0.8 (2025-01-04)
- ✨ 新增:输入法状态记忆功能
- 🐛 修复ESC 键第一次按下无效的问题
- 🐛 修复Insert 模式下输入字符触发输入法切换
- ⚡️ 优化:使用事件捕获模式提高响应速度
- 🧹 清理:移除冗余代码和过多的调试日志
### 许可证
MIT License
---
## English
### Features
An input method auto-switching plugin designed for Obsidian's Vim mode that:
- **Automatic switching**: Automatically switches input methods when transitioning between Vim's Normal and Insert modes
- **Smart memory**: Remembers the input method state when exiting Insert mode and restores it on next entry
- **Seamless experience**: Won't trigger input method changes during normal text input in Insert mode
### Core Features
#### 1. Mode Switch Automation
- **Enter Normal mode** (press ESC or other commands) → Auto switch to English input method
- **Enter Insert mode** (press i, a, o, etc.) → Auto restore previous input method state
#### 2. Input Method State Memory
- Automatically detects and saves current input method (Chinese/English) when exiting Insert mode
- Automatically restores to last saved input method state when entering Insert mode
- Supports mixed Chinese/English input scenarios
#### 3. Smart Detection Mechanism
The plugin uses multiple detection mechanisms for reliability:
- **Keyboard event listening** (primary): Uses capture mode to listen for ESC and Insert keys
- **CodeMirror events** (auxiliary): Monitors vim-mode-change events
- **Polling** (fallback): 100ms polling to detect mode changes
### Requirements
#### macOS
1. Install [fcitx-remote-for-osx](https://github.com/xcodebuild/fcitx-remote-for-osx):
```bash
brew install fcitx-remote-for-osx
```
2. Verify installation:
```bash
fcitx-remote -n
# Should output current input method name, e.g.: com.apple.keylayout.ABC
```
#### Linux
Install `fcitx` via your package manager:
```bash
# Ubuntu/Debian
sudo apt-get install fcitx
# Fedora
sudo dnf install fcitx
# Arch Linux
sudo pacman -S fcitx
```
#### Windows
Download and install the AutoHotKey version of fcitx-remote:
- [Download fcitx-remote.exe](https://github.com/yuanotes/obsidian-vim-im-switch-plugin/releases/download/1.0.3/fcitx-remote.exe)
- Place the exe file in your system PATH
### Installation
1. Download plugin files to Obsidian plugins directory:
```bash
cd /path/to/your/vault/.obsidian/plugins/
git clone https://github.com/yourusername/vim-im-switch.git
```
2. Enable plugin in Obsidian:
- Open Settings → Community Plugins → Browse
- Find "Vim Input Method Switch"
- Click Enable
3. Configure input methods:
- Open plugin settings
- Set English input method (default: `com.apple.keylayout.ABC`)
- Set Chinese input method (default: auto-detect)
### Usage
#### Basic Usage
1. **Chinese Input**:
```
Press i → Enter Insert mode → IM switches to Chinese (if last time was Chinese)
Type Chinese content
Press ESC → Exit to Normal mode → IM switches to English
```
2. **English Input**:
```
Press i → Enter Insert mode → IM stays English (if last time was English)
Type English content
Press ESC → Exit to Normal mode → IM stays English
```
3. **Mixed Input**:
```
Press i → Auto restore last IM
Type Chinese, then manually switch to English
Press ESC → Save current IM state (English)
Press i → Auto restore English IM
```
#### Supported Vim Commands
- **Enter Insert mode**: `i`, `I`, `a`, `A`, `o`, `O`, `s`, `S`, `c`, `C`
- **Exit Insert mode**: `ESC`, and other commands that trigger Normal mode
### How It Works
```mermaid
graph TD
A[Normal Mode<br/>English IM] -->|Press i/a/o etc| B[Detect Mode Change]
B --> C[Restore Last Saved<br/>IM State]
C --> D[Insert Mode<br/>Restored IM:<br/>Chinese/English]
D -->|Press ESC| E[Save Current IM<br/>State CN/EN]
E --> F[Switch to English IM]
F --> A
style A fill:#e1f5ff,stroke:#01579b,stroke-width:2px
style D fill:#fff9c4,stroke:#f57f17,stroke-width:2px
style E fill:#f3e5f5,stroke:#4a148c,stroke-width:2px
style F fill:#e8f5e9,stroke:#1b5e20,stroke-width:2px
```
The plugin maintains a state machine that tracks Vim modes and input method states, automatically switching input methods when mode transitions occur while preserving user preferences.
### Troubleshooting
#### Issue: Plugin not working
1. Check if fcitx-remote is properly installed
2. Check plugin logs in developer console
3. Verify input method names match settings
#### Issue: Incorrect input method switching
1. Confirm input method names in settings are correct
2. Check logs in developer console after manually switching IM
3. Check for conflicts with other plugins
### Development
#### Build
```bash
npm install
npm run build
```
#### Deploy
```bash
./deploy.sh
```
#### Debug
The plugin outputs key logs in the console:
- `🚀 Loading plugin...` - Plugin loaded
- `🔤 ESC → English` - ESC switches to English
- `🈳 → Chinese` - Switch to Chinese
- `❌ Error...` - Error messages
### Changelog
#### v1.0.8 (2025-01-04)
- ✨ Added: Input method state memory feature
- <20><> Fixed: ESC key not working on first press
- 🐛 Fixed: Character input in Insert mode triggering IM switch
- ⚡️ Optimized: Use event capture mode for better responsiveness
- 🧹 Cleanup: Remove redundant code and excessive debug logs
### License
MIT License

View File

@@ -1,580 +0,0 @@
# Obsidian Vim 输入法自动切换插件# Obsidian Vim 输入法自动切换插件
[English](./README_en.md) | 中文[English](#english) | [中文](#中文)
一个为 Obsidian 编辑器的 Vim 模式设计的输入法自动切换插件。---
## 功能简介## 中文
- **自动切换输入法**:在 Vim 的 Normal 模式和 Insert 模式之间切换时,自动切换输入法### 功能简介
- **智能记忆**:记住上次 Insert 模式退出时的输入法状态,下次进入时自动恢复
- **无缝体验**:在 Insert 模式下正常输入文本时不会触发输入法切换这是一个为 Obsidian 编辑器的 Vim 模式设计的输入法自动切换插件。它能够:
## 核心特性- **自动切换输入法**:在 Vim 的 Normal 模式和 Insert 模式之间切换时,自动切换输入法
- **智能记忆**:记住上次 Insert 模式退出时的输入法状态,下次进入时自动恢复
### 1. 模式切换自动化- **无缝体验**:在 Insert 模式下正常输入文本时不会触发输入法切换
- **进入 Normal 模式**(按 ESC 或其他命令)→ 自动切换到英文输入法
- **进入 Insert 模式**(按 i, a, o 等)→ 自动恢复上次的输入法状态### 核心特性
### 2. 输入法状态记忆#### 1. 模式切换自动化
- 退出 Insert 模式时,自动检测并保存当前的输入法(中文/英文)- **进入 Normal 模式**(按 ESC 或其他命令)→ 自动切换到英文输入法
- 下次进入 Insert 模式时,自动恢复到上次保存的输入法状态- **进入 Insert 模式**(按 i, a, o 等)→ 自动恢复上次的输入法状态
- 支持中英文混合输入场景
#### 2. 输入法状态记忆
### 3. 智能检测机制- 退出 Insert 模式时,自动检测并保存当前的输入法(中文/英文)
插件使用多重检测机制确保可靠性:- 下次进入 Insert 模式时,自动恢复到上次保存的输入法状态
- **键盘事件监听**(主要):使用事件捕获模式监听 ESC 和 Insert 按键- 支持中英文混合输入场景
- **CodeMirror 事件**(辅助):监听 vim-mode-change 事件
- **定时轮询**兜底100ms 轮询检测模式变化#### 3. 智能检测机制
插件使用多重检测机制确保可靠性:
## 快速开始- **键盘事件监听**(主要):使用事件捕获模式监听 ESC 和 Insert 按键
- **CodeMirror 事件**(辅助):监听 vim-mode-change 事件
### 安装要求- **定时轮询**兜底100ms 轮询检测模式变化
#### macOS### 安装要求
```bash
brew install fcitx-remote-for-osx#### macOS
```1. 安装 [fcitx-remote-for-osx](https://github.com/xcodebuild/fcitx-remote-for-osx)
```bash
#### Linux brew install fcitx-remote-for-osx
```bash ```
# Ubuntu/Debian
sudo apt-get install fcitx2. 确认安装成功:
```bash
# Fedora fcitx-remote -n
sudo dnf install fcitx # 应该输出当前输入法的名称com.apple.keylayout.ABC
```
# Arch Linux
sudo pacman -S fcitx#### Linux
```通过系统包管理器安装 `fcitx` 输入法:
```bash
#### Windows# Ubuntu/Debian
下载 [fcitx-remote.exe](https://github.com/yuanotes/obsidian-vim-im-switch-plugin/releases/download/1.0.3/fcitx-remote.exe) 并放到系统 PATH 路径中sudo apt-get install fcitx
### 安装插件# Fedora
sudo dnf install fcitx
1. 下载插件到 Obsidian 插件目录:
```bash# Arch Linux
cd /path/to/your/vault/.obsidian/plugins/sudo pacman -S fcitx
git clone https://github.com/yourusername/vim-im-switch.git```
```
#### Windows
2. 在 Obsidian 中启用插件:下载并安装 AutoHotKey 版本的 fcitx-remote
- 打开设置 → 社区插件- [下载 fcitx-remote.exe](https://github.com/yuanotes/obsidian-vim-im-switch-plugin/releases/download/1.0.3/fcitx-remote.exe)
- 找到 "Vim Input Method Switch" 并启用- 将 exe 文件放到系统 PATH 路径中
3. 配置输入法(可选):### 安装插件
- 设置英文输入法(默认:`com.apple.keylayout.ABC`
- 设置中文输入法默认自动检测1. 下载插件文件到 Obsidian 插件目录:
```bash
## 使用方法 cd /path/to/your/vault/.obsidian/plugins/
git clone https://github.com/yourusername/vim-im-switch.git
### 基本使用场景 ```
**中文输入**2. 在 Obsidian 中启用插件:
``` - 打开设置 → 社区插件 → 浏览
按 i → 进入 Insert 模式 → 输入法切换到中文(如果上次是中文) - 找到 "Vim Input Method Switch"
输入中文内容 - 点击启用
按 ESC → 退出到 Normal 模式 → 输入法切换到英文
```3. 配置输入法:
- 打开插件设置
**英文输入** - 设置英文输入法(默认:`com.apple.keylayout.ABC`
``` - 设置中文输入法(默认:自动检测)
按 i → 进入 Insert 模式 → 输入法保持英文(如果上次是英文)
输入英文内容### 使用方法
按 ESC → 退出到 Normal 模式 → 输入法保持英文
```#### 基本使用
**中英混合**1. **中文输入**
``` ```
按 i → 自动恢复上次的输入法 按 i → 进入 Insert 模式 → 输入法切换到中文(如果上次是中文)
输入中文,然后手动切换到英文继续输入 输入中文内容
按 ESC → 保存当前输入法状态(英文) 按 ESC → 退出到 Normal 模式 → 输入法切换到英文
按 i → 自动恢复英文输入法 ```
```
2. **英文输入**
### 支持的 Vim 命令 ```
按 i → 进入 Insert 模式 → 输入法保持英文(如果上次是英文)
- **进入 Insert 模式**`i`, `I`, `a`, `A`, `o`, `O`, `s`, `S`, `c`, `C` 输入英文内容
- **退出 Insert 模式**`ESC`, 以及其他触发 Normal 模式的命令 按 ESC → 退出到 Normal 模式 → 输入法保持英文
```
## 工作原理
3. **中英混合**
```mermaid ```
graph TD 按 i → 自动恢复上次的输入法
A[Normal 模式<br/>英文输入法] -->|按 i/a/o 等| B[检测模式切换] 输入中文,然后手动切换到英文继续输入
B --> C[恢复上次保存的<br/>输入法状态] 按 ESC → 保存当前输入法状态(英文)
C --> D[Insert 模式<br/>自动恢复的输入法:<br/>中文/英文] 按 i → 自动恢复英文输入法
D -->|按 ESC| E[保存当前输入法<br/>状态 中/英] ```
E --> F[切换到英文输入法]
F --> A#### 支持的 Vim 命令
style A fill:#e1f5ff,stroke:#01579b,stroke-width:2px- **进入 Insert 模式**`i`, `I`, `a`, `A`, `o`, `O`, `s`, `S`, `c`, `C`
style D fill:#fff9c4,stroke:#f57f17,stroke-width:2px- **退出 Insert 模式**`ESC`, 以及其他触发 Normal 模式的命令
style E fill:#f3e5f5,stroke:#4a148c,stroke-width:2px
style F fill:#e8f5e9,stroke:#1b5e20,stroke-width:2px### 工作原理
```
```mermaid
### 技术细节graph TD
A[Normal 模式<br/>英文输入法] -->|按 i/a/o 等| B[检测模式切换]
**输入法检测** B --> C[恢复上次保存的<br/>输入法状态]
- 使用 `fcitx-remote -n` 获取当前输入法名称 C --> D[Insert 模式<br/>自动恢复的输入法:<br/>中文/英文]
- 对比设置中的中英文输入法名称来判断状态 D -->|按 ESC| E[保存当前输入法<br/>状态 中/英]
E --> F[切换到英文输入法]
**事件处理** F --> A
- **键盘事件**:使用 `capture` 模式尽早捕获按键
- **防抖机制**100ms 内的重复事件会被忽略
- **异步处理**:所有输入法切换操作都是异步的,不阻塞编辑 style A fill:#e1f5ff,stroke:#01579b,stroke-width:2px
style D fill:#fff9c4,stroke:#f57f17,stroke-width:2px
**状态管理** style E fill:#f3e5f5,stroke:#4a148c,stroke-width:2px
- `currentVimMode`:当前 Vim 模式normal/insert/visual style F fill:#e8f5e9,stroke:#1b5e20,stroke-width:2px
- `lastInsertModeIMStatus`:上次 Insert 模式的输入法状态```
- `imStatus`当前输入法状态Activate=中文Deactivate=英文)
### 技术细节
## 故障排除
#### 输入法检测
### 问题:插件没有效果- 使用 `fcitx-remote -n` 获取当前输入法名称
1. 检查 fcitx-remote 是否正确安装:- 对比设置中的中英文输入法名称来判断状态
```bash
which fcitx-remote#### 事件处理
fcitx-remote -n- **键盘事件**:使用 `capture` 模式尽早捕获按键
```- **防抖机制**100ms 内的重复事件会被忽略
- **异步处理**:所有输入法切换操作都是异步的,不阻塞编辑
2. 检查插件日志(开发者控制台):
- 按 `Cmd+Option+I` (macOS) 或 `Ctrl+Shift+I` (Windows/Linux) 打开开发者控制台#### 状态管理
- 查看是否有 `[VimIMSwitch]` 开头的日志- `currentVimMode`:当前 Vim 模式normal/insert/visual
- `lastInsertModeIMStatus`:上次 Insert 模式的输入法状态
3. 验证输入法名称:- `imStatus`当前输入法状态Activate=中文Deactivate=英文)
```bash
# 切换到中文输入法后执行### 故障排除
fcitx-remote -n
# 输出应该匹配插件设置中的中文输入法名称#### 问题:插件没有效果
```1. 检查 fcitx-remote 是否正确安装:
```bash
### 问题:输入法切换不正确 which fcitx-remote
1. 确认设置中的输入法名称正确 fcitx-remote -n
2. 手动切换输入法后,在开发者控制台查看日志 ```
3. 检查是否有其他插件冲突
2. 检查插件日志(开发者控制台):
### 问题ESC 键需要按多次 ```
- 这个问题已在最新版本中修复 按 Cmd+Option+I 打开开发者控制台
- 如果仍然存在,请更新到最新版本 查看是否有 [VimIMSwitch] 开头的日志
```
## 开发
3. 验证输入法名称:
### 构建 ```bash
```bash # 切换到中文输入法后执行
npm install fcitx-remote -n
npm run build # 输出应该匹配插件设置中的中文输入法名称
``` ```
### 部署#### 问题:输入法切换不正确
```bash1. 确认设置中的输入法名称正确
./deploy.sh2. 手动切换输入法后,在开发者控制台查看日志
```3. 检查是否有其他插件冲突
### 调试#### 问题ESC 键需要按多次
插件会在控制台输出关键日志:- 这个问题已在最新版本中修复
- `🚀 Loading plugin...` - 插件加载- 如果仍然存在,请更新到最新版本
- `🔤 ESC → English` - ESC 切换到英文
- `🈳 → Chinese` - 切换到中文### 开发
- `❌ Error...` - 错误信息
#### 构建
## 更新日志```bash
npm install
查看 [CHANGELOG.md](./CHANGELOG.md) 获取详细的版本更新历史。npm run build
```
## 许可证
#### 部署
MIT License```bash
./deploy.sh
## 相关链接```
- [更新日志](./CHANGELOG.md)#### 调试
- [English Documentation](./README_en.md)插件会在控制台输出关键日志:
- [fcitx-remote-for-osx](https://github.com/xcodebuild/fcitx-remote-for-osx)- `🚀 Loading plugin...` - 插件加载
- `🔤 ESC → English` - ESC 切换到英文
- `🈳 → Chinese` - 切换到中文
- `❌ Error...` - 错误信息
### 更新日志
#### v1.0.8 (2025-01-04)
- ✨ 新增:输入法状态记忆功能
- 🐛 修复ESC 键第一次按下无效的问题
- 🐛 修复Insert 模式下输入字符触发输入法切换
- ⚡️ 优化:使用事件捕获模式提高响应速度
- 🧹 清理:移除冗余代码和过多的调试日志
### 许可证
MIT License
---
## English
### Features
An input method auto-switching plugin designed for Obsidian's Vim mode that:
- **Automatic switching**: Automatically switches input methods when transitioning between Vim's Normal and Insert modes
- **Smart memory**: Remembers the input method state when exiting Insert mode and restores it on next entry
- **Seamless experience**: Won't trigger input method changes during normal text input in Insert mode
### Core Features
#### 1. Mode Switch Automation
- **Enter Normal mode** (press ESC or other commands) → Auto switch to English input method
- **Enter Insert mode** (press i, a, o, etc.) → Auto restore previous input method state
#### 2. Input Method State Memory
- Automatically detects and saves current input method (Chinese/English) when exiting Insert mode
- Automatically restores to last saved input method state when entering Insert mode
- Supports mixed Chinese/English input scenarios
#### 3. Smart Detection Mechanism
The plugin uses multiple detection mechanisms for reliability:
- **Keyboard event listening** (primary): Uses capture mode to listen for ESC and Insert keys
- **CodeMirror events** (auxiliary): Monitors vim-mode-change events
- **Polling** (fallback): 100ms polling to detect mode changes
### Requirements
#### macOS
1. Install [fcitx-remote-for-osx](https://github.com/xcodebuild/fcitx-remote-for-osx):
```bash
brew install fcitx-remote-for-osx
```
2. Verify installation:
```bash
fcitx-remote -n
# Should output current input method name, e.g.: com.apple.keylayout.ABC
```
#### Linux
Install `fcitx` via your package manager:
```bash
# Ubuntu/Debian
sudo apt-get install fcitx
# Fedora
sudo dnf install fcitx
# Arch Linux
sudo pacman -S fcitx
```
#### Windows
Download and install the AutoHotKey version of fcitx-remote:
- [Download fcitx-remote.exe](https://github.com/yuanotes/obsidian-vim-im-switch-plugin/releases/download/1.0.3/fcitx-remote.exe)
- Place the exe file in your system PATH
### Installation
1. Download plugin files to Obsidian plugins directory:
```bash
cd /path/to/your/vault/.obsidian/plugins/
git clone https://github.com/yourusername/vim-im-switch.git
```
2. Enable plugin in Obsidian:
- Open Settings → Community Plugins → Browse
- Find "Vim Input Method Switch"
- Click Enable
3. Configure input methods:
- Open plugin settings
- Set English input method (default: `com.apple.keylayout.ABC`)
- Set Chinese input method (default: auto-detect)
### Usage
#### Basic Usage
1. **Chinese Input**:
```
Press i → Enter Insert mode → IM switches to Chinese (if last time was Chinese)
Type Chinese content
Press ESC → Exit to Normal mode → IM switches to English
```
2. **English Input**:
```
Press i → Enter Insert mode → IM stays English (if last time was English)
Type English content
Press ESC → Exit to Normal mode → IM stays English
```
3. **Mixed Input**:
```
Press i → Auto restore last IM
Type Chinese, then manually switch to English
Press ESC → Save current IM state (English)
Press i → Auto restore English IM
```
#### Supported Vim Commands
- **Enter Insert mode**: `i`, `I`, `a`, `A`, `o`, `O`, `s`, `S`, `c`, `C`
- **Exit Insert mode**: `ESC`, and other commands that trigger Normal mode
### How It Works
```mermaid
graph TD
A[Normal Mode<br/>English IM] -->|Press i/a/o etc| B[Detect Mode Change]
B --> C[Restore Last Saved<br/>IM State]
C --> D[Insert Mode<br/>Restored IM:<br/>Chinese/English]
D -->|Press ESC| E[Save Current IM<br/>State CN/EN]
E --> F[Switch to English IM]
F --> A
style A fill:#e1f5ff,stroke:#01579b,stroke-width:2px
style D fill:#fff9c4,stroke:#f57f17,stroke-width:2px
style E fill:#f3e5f5,stroke:#4a148c,stroke-width:2px
style F fill:#e8f5e9,stroke:#1b5e20,stroke-width:2px
```
The plugin maintains a state machine that tracks Vim modes and input method states, automatically switching input methods when mode transitions occur while preserving user preferences.
### Troubleshooting
#### Issue: Plugin not working
1. Check if fcitx-remote is properly installed
2. Check plugin logs in developer console
3. Verify input method names match settings
#### Issue: Incorrect input method switching
1. Confirm input method names in settings are correct
2. Check logs in developer console after manually switching IM
3. Check for conflicts with other plugins
### Development
#### Build
```bash
npm install
npm run build
```
#### Deploy
```bash
./deploy.sh
```
#### Debug
The plugin outputs key logs in the console:
- `🚀 Loading plugin...` - Plugin loaded
- `🔤 ESC → English` - ESC switches to English
- `🈳 → Chinese` - Switch to Chinese
- `❌ Error...` - Error messages
### Changelog
#### v1.0.8 (2025-01-04)
- ✨ Added: Input method state memory feature
- <20><> Fixed: ESC key not working on first press
- 🐛 Fixed: Character input in Insert mode triggering IM switch
- ⚡️ Optimized: Use event capture mode for better responsiveness
- 🧹 Cleanup: Remove redundant code and excessive debug logs
### License
MIT License

View File

@@ -1,5 +1,7 @@
# Vim Input Method Switch Plugin
[![Version](https://img.shields.io/badge/version-2.0.3-blue.svg)](./CHANGELOG.md)
English | [中文](./README.md)
An input method auto-switching plugin designed for Vim and Obsidian's Vim mode, including:

View File

@@ -1,216 +1,219 @@
# 版本发布说明 RELEASE NOTE
# Release Notes
## v2.0.1
**发布日期**: 2025-11-04
发布说明 | Release Notes
---
### v2.0.1 - Patch Notes
## v1.0.0 - 2024-06-01
- 验证并修复终端macOS Terminal / iTerm2`vi``ESC` 时顶部短暂显示 `fcitx-remote` 的问题(使用 `nohup` + 后台执行并重定向,避免任何 stderr/stdout 导致的 UI 更新)
- 修复:删除对 `job_start` 不兼容的 `{'detach': v:true}` 选项以兼容更多 Vim 构建
- 文档微调:更新 README/CHANGELOG/RELEASE记录上述兼容性修复
### 首次发布
- 基本的 Vim 模式输入法自动切换功能
- 支持 macOS 系统
- 支持 Normal/Insert 模式切换
---
### 版本概述
## v1.0.1 - 2024-06-20
版本 2.0.0 是一个重要的里程碑版本,为 Vim 输入法自动切换插件带来了重大改进。此版本包含:
- **新增原生 Vim 插件支持**:为 Vim/NeoVim 编辑器提供相同的输入法自动切换功能
- **完善的输入法状态管理系统**:智能记忆和恢复输入法状态
- **增强的可靠性和性能**:消除 UI 闪烁,优化切换速度
- **全面的文档更新**:包含两个插件的完整使用说明
### Bug 修复
### 新增内容
- 修复了插件加载失败的问题
- 改进了兼容性
#### 主要功能
---
##### 原生 Vim 插件支持(全新)
**fcitx-osx.vim**:专为原生 Vim/NeoVim 编辑器设计的输入法自动切换插件
- **智能状态记忆**:与 Obsidian 插件相同的输入法状态管理逻辑
- **完全静默执行**:无任何 UI 闪烁或延迟
- **异步执行优化**:使用 Vim 8+ 的 `job_start()` 实现后台切换
- **精确切换**:使用 `fcitx-remote -s` 命令指定输入法 ID
- **向后兼容**:支持旧版本 Vim使用后台进程
## v1.0.2 - 2024-07-15
**安装方式**
```bash
mkdir -p ~/.vim/plugin
cp fcitx-osx.vim ~/.vim/plugin/
```
### 性能优化
**配置选项**
```vim
let g:fcitx_english_im = 'com.apple.keylayout.ABC'
let g:fcitx_chinese_im = 'com.tencent.inputmethod.wetype.pinyin'
```
- 优化了输入法切换的响应时间
- 减少了不必要的命令调用
##### 输入法状态记忆系统(两个插件共享)
插件现在具备智能状态记忆系统,能够记住您的输入法偏好:
- **智能记忆**:自动记住退出 Insert 模式时使用的输入法(中文/英文)
- **自动恢复**:再次进入 Insert 模式时,插件会恢复上次的输入法状态
- **混合输入支持**:无缝处理在同一编辑会话中中英文输入法切换的场景
---
**示例工作流程**
```
1. 按 'i' → 进入 Insert 模式 → 恢复中文输入法(如果上次是中文)
2. 输入中文内容
3. 手动切换到英文输入法,继续输入
4. 按 ESC → 保存"英文"作为最后状态
5. 再次按 'i' → 自动恢复英文输入法
```
## v1.0.3 - 2024-08-01
#### 关键 Bug 修复
### Bug 修复
##### ESC 键响应问题
- **修复内容**ESC 键需要多次按下才能切换输入法
- **解决方案**:实现事件捕获模式,在最早阶段拦截按键
- **效果**:首次按下 ESC 即可立即切换到英文输入法
- 修复了初始化时的异步问题
- 改进了命令执行的错误处理
##### Insert 模式干扰问题
- **修复内容**:在 Insert 模式下输入字符(输入 'i'、'a'、'o' 等)触发不必要的输入法切换
- **解决方案**:为键盘事件处理器添加 Vim 模式验证
- **效果**Insert 模式下的正常文本输入不再被打断
---
## v1.0.4 - 2024-09-05
### 新增功能
- 添加了对 Linux 系统的支持
- 改进了日志输出格式
---
## v1.0.5 - 2024-10-10
### Bug 修复
- 修复了 Windows 系统下的兼容性问题
- 改进了错误处理逻辑
---
## v1.0.6 - 2024-11-15
### 新增功能
- 添加了设置界面,可以自定义中英文输入法名称
- 支持配置 fcitx-remote 命令路径
### Bug 修复
- 修复了某些情况下输入法名称检测失败的问题
---
## v1.0.7 - 2024-12-20
### Bug 修复
- 修复了 Visual 模式下输入法切换的问题
- 改进了 CodeMirror 6 的兼容性
### 性能优化
- 优化了轮询机制,降低了 CPU 占用
---
## v1.0.8 - 2025-01-04
### 重大更新:输入法状态记忆功能
#### 新增功能
**输入法状态记忆**
- 智能记忆上次输入法状态:插件现在会记住你退出 Insert 模式时使用的输入法(中文/英文)
- 自动恢复:下次进入 Insert 模式时,自动恢复到上次保存的输入法状态
- 支持中英文混合输入场景
**状态管理增强**
- 新增 `lastInsertModeIMStatus` 变量追踪上次 Insert 模式的输入法状态
- 在退出 Insert 模式时,实时检测并保存当前输入法名称
- 在进入 Insert 模式时,根据保存的状态决定是否切换输入法
#### Bug 修复
**修复 ESC 键第一次按下无效的问题**
- 问题:用户首次按下 ESC 键时,输入法不会切换到英文,需要按第二次才生效
- 解决:将键盘事件监听器注册移到 `onload()` 方法中,使用事件捕获模式(`{ capture: true }`
- 效果:现在第一次按 ESC 就能立即切换到英文输入法
**修复 Insert 模式下输入字符触发输入法切换**
- 问题:在 Insert 模式下正常输入文本时,输入 `i`, `a`, `o` 等字符会意外触发输入法切换
- 解决:在 Insert 按键监听器中添加模式检查:`if (this.currentVimMode !== 'normal') return;`
- 效果:在 Insert 模式下正常输入文本不会被干扰
#### 性能优化
##### 事件处理优化
- **捕获模式**:键盘事件监听器使用 `{ capture: true }`
- **降低延迟**:输入法切换响应时间快 10-20ms
- **智能检测**:仅在必要时切换输入法,避免冗余操作
**使用事件捕获模式提高响应速度**
- 使用 `addEventListener('keydown', handler, { capture: true })` 代替默认的冒泡模式
- 在事件处理链的最早阶段捕获按键
- ESC 键响应更快,几乎没有延迟
##### 三重检测机制
通过三层检测增强可靠性:
1. **主要检测**:键盘事件捕获(最快,最高优先级)
2. **辅助检测**CodeMirror vim-mode-change 事件
3. **兜底检测**100ms 轮询作为安全保障
**优化输入法检测逻辑**
- 在 ESC 键处理时,先检测当前输入法再决定是否切换
- 避免不必要的 `fcitx-remote` 调用
- 当前已经是英文输入法时,不会重复切换
#### 代码质量
#### 技术改进
##### 代码库清理
- 移除约 50 行未使用的测试代码
- 减少插件体积约 2KB
- 提高代码可维护性和可读性
**多重检测机制**
1. 键盘事件监听(主要)- 使用 capture 模式监听 ESC 和 Insert 按键
2. CodeMirror 事件监听(辅助)- 监听 vim-mode-change 事件
3. 定时轮询(兜底)- 每 100ms 检查一次 Vim 模式
##### 简化日志输出
- 减少 70% 的控制台输出
- 保留关键的状态转换日志
- 更清晰的调试信息,便于故障排查
---
#### 文档
## v2.0.0 - 2025-11-04
##### 全面的文档
- **README.md**:详细的中文文档,包含使用示例
- **README_en.md**:完整的英文文档
- **CHANGELOG.md**:详细的中文版本历史
- **CHANGELOG_en.md**:完整的英文更新日志
- **Mermaid 图表**:可视化工作流程说明
### 重大更新:新增 Vim 原生插件支持
### 技术细节
#### 新增功能
#### 架构改进
**Vim 原生插件支持**
- 新增 fcitx-osx.vim 插件:为原生 Vim/NeoVim 编辑器提供输入法自动切换功能
- 智能输入法状态记忆:
- 记住退出 Insert 模式时的输入法(中文/英文)
- 下次进入 Insert 模式时自动恢复上次的输入法状态
- 完全无感知切换,无任何 UI 闪烁
- 使用 fcitx-remote -s 命令:
- 通过指定输入法 ID 进行精确切换
- 替代原有的 `-c`/`-o` 开关命令
- 支持自定义英文和中文输入法 ID
- 异步执行优化:
- 使用 Vim 8+ 的 `job_start()` 异步执行命令
- 完全消除输入法切换时的 UI 闪烁和延迟
- 向后兼容旧版本 Vim使用后台进程
**状态管理**
- `currentVimMode`:追踪当前 Vim 模式normal/insert/visual
- `lastInsertModeIMStatus`:存储上次 Insert 模式的输入法状态
- `imStatus`:维护当前输入法状态
**部署脚本增强**
- deploy.sh 更新:一键部署 Obsidian 插件和 Vim 插件
- 自动创建 `~/.vim/plugin/` 目录
- 同时复制两个插件到各自的目标位置
**检测系统**
- 事件捕获阶段拦截
- 使用 `fcitx-remote -n` 实时验证输入法状态
- 异步输入法切换操作
#### 改进
#### 兼容性
**Vim 插件性能优化**
- 使用 `fcitx-remote -n` 获取当前输入法名称
- 保存完整的输入法 ID 而不是简单的开关状态
- 避免不必要的输入法切换(相同输入法时跳过)
- 所有输出重定向到 `/dev/null`,确保静默执行
**平台支持**
- macOS通过 fcitx-remote-for-osx 完全支持
- Linux通过 fcitx 完全支持
- Windows通过 AutoHotKey 版本支持
#### Bug 修复
**Obsidian 兼容性**
- 兼容最新版本的 Obsidian
- 同时支持 CodeMirror 5 和 CodeMirror 6
- 修复 Vim 插件中 `fcitx-remote -c` 命令不生效的问题
- 修复标题栏显示 "fcitx-remote" 的闪烁问题
- 修复字符串比较失败(`"2\n" == 2`)的问题
### 升级指南
#### 文档更新
#### 现有用户
- 更新 README.md 和 README_en.md添加 Vim 插件安装说明
- 更新所有文档标题,从 "Obsidian" 改为包含两个插件的描述
- 添加 Vim 插件配置说明和调试方法
**从 v1.x 升级**
```bash
cd /path/to/your/vault/.obsidian/plugins/vim-im-switch/
git pull origin main
npm install
npm run build
```
---
然后重启 Obsidian 以激活新版本。
## v2.0.2 - 2025-11-04
**重要提示**
- 升级过程中会保留设置
- 无需更改配置
- 建议升级前备份您的 vault
### 修复
#### 新用户
**终端兼容性修复**
- 修复在 macOS 终端vi/vim中按 ESC 时,子进程输出导致标题栏短暂闪烁的问题
- 使用 nohup/background 方式彻底静默化命令执行
- 移除 `job_start(..., {'detach': v:true})` 不兼容选项,改以更通用的后台执行方式
**Obsidian 插件安装步骤**
1. 为您的平台安装 fcitx-remote
2. 将插件克隆到 Obsidian 插件目录
3. 在 Obsidian 设置中启用插件
4. 配置输入法名称(可选)
### 文档
**Vim 插件安装步骤**
1. 为您的平台安装 fcitx-remote
2. 复制 `fcitx-osx.vim``~/.vim/plugin/`
3. 重启 Vim插件自动加载
4.`.vimrc` 中配置输入法 ID可选
- 补充 RELEASE/README 中关于终端下行为的说明
**一键部署(推荐)**
```bash
./deploy.sh # 同时部署两个插件
```
---
详细安装说明请参阅 [README.md](./README.md) 或 [README_en.md](./README_en.md)。
## v2.0.3 - 2025-11-09
### 已知问题
### 修复
本版本无严重问题。如需故障排除,请参考:
- [中文故障排除](./README.md#故障排除)
- [English Troubleshooting](./README_en.md#troubleshooting)
**Obsidian 插件**
- **修复 Normal 模式下按 ESC 触发输入法切换的问题**
- 问题:在 Normal 模式下按 ESC 会意外触发输入法切换(从中文切到英文,再按又从英文切到英文)
- 原因:`handleKeyDown` 函数处理 ESC 键时没有检查当前 Vim 模式
- 解决:增加模式检查,只在 Insert 或 Replace 模式下按 ESC 时才保存输入法状态并切换到英文
- 效果:与原生 Vim 插件行为保持一致Normal 模式下按 ESC 不会改变输入法
### 破坏性变更
### 改进
无。本版本与 v1.x 配置完全向后兼容。
- 提高了模式检测的准确性
- 优化了 ESC 键的处理逻辑
### 废弃功能
---
本版本无废弃功能。
**查看详细更新日志**: [CHANGELOG.md](./CHANGELOG.md) | [CHANGELOG_en.md](./CHANGELOG_en.md)
### 未来规划
计划在未来版本中实现的功能:
- 支持更多输入法切换工具im-select、macism 等)
- 可配置的快捷键绑定
- Visual 模式输入法处理改进
- 与 Obsidian 原生语言切换集成
- NeoVim Lua 插件版本
### 相关链接
- [GitHub 仓库](https://github.com/yourusername/vim-im-switch)
- [问题追踪](https://github.com/yourusername/vim-im-switch/issues)
- [中文文档](./README.md)
- [English Documentation](./README_en.md)
- [更新日志](./CHANGELOG.md)
- [Changelog](./CHANGELOG_en.md)
### 技术支持
如果您遇到任何问题或有疑问:
1. 查看 [故障排除指南](./README.md#故障排除)
2. 搜索现有的 [Issues](https://github.com/yourusername/vim-im-switch/issues)
3. 创建新的 issue 并提供详细信息
### 许可证
MIT License - 详见 [LICENSE.txt](./LICENSE.txt)

36
main.js

File diff suppressed because one or more lines are too long

36
main.ts
View File

@@ -196,24 +196,28 @@ export default class VimIMSwitchPlugin extends Plugin {
}
this.lastKeyTime = currentTime;
// 处理ESC键退出到normal模式
// 处理ESC键只在insert/replace模式下才切换输入法
if (event.key === 'Escape') {
// 退出insert模式前,先保存当前输入法状态
const beforeIM = await this.runCmd(this.fcitxRemotePath, ["-n"]);
const currentIMName = beforeIM.trim();
// 检查当前输入法是中文还是英文
if (currentIMName === this.settings.chineseInputMethod) {
this.lastInsertModeIMStatus = IMStatus.Activate;
console.log('🔤 [VimIMSwitch] ESC → English (saved Chinese)');
} else {
this.lastInsertModeIMStatus = IMStatus.Deactivate;
console.log('🔤 [VimIMSwitch] ESC → English (saved English)');
// 只有在insert或replace模式下按ESC才需要处理输入法
if (this.currentVimMode === 'insert' || this.currentVimMode === 'replace') {
// 退出insert模式前先保存当前输入法状态
const beforeIM = await this.runCmd(this.fcitxRemotePath, ["-n"]);
const currentIMName = beforeIM.trim();
// 检查当前输入法是中文还是英文
if (currentIMName === this.settings.chineseInputMethod) {
this.lastInsertModeIMStatus = IMStatus.Activate;
console.log('🔤 [VimIMSwitch] ESC → English (saved Chinese)');
} else {
this.lastInsertModeIMStatus = IMStatus.Deactivate;
console.log('🔤 [VimIMSwitch] ESC → English (saved English)');
}
// 切换到英文输入法
this.currentVimMode = 'normal';
await this.deactivateIM();
}
// 切换到英文输入法
this.currentVimMode = 'normal';
await this.deactivateIM();
// 如果已经在normal模式ESC键不做任何输入法切换
}
// 处理进入insert模式的按键只在normal模式下
else if (this.currentVimMode === 'normal' &&

View File

@@ -1,7 +1,7 @@
{
"id": "obsidian-vim-im-switch-plugin",
"name": "Vim Input Method Switch",
"version": "1.0.8",
"version": "2.0.3",
"minAppVersion": "0.9.12",
"description": "Switch input method with fcitx-remote when Vim keymap is enabled.",
"author": "Yuan Chen",

1
runit.sh Symbolic link
View File

@@ -0,0 +1 @@
/Users/gavin/tools/runit.sh

View File

@@ -1,4 +1,6 @@
{
"2.0.3": "0.9.12",
"2.0.2": "0.9.12",
"2.0.0": "0.9.12",
"1.0.2": "0.9.12",
"1.0.1": "0.9.12",

File diff suppressed because one or more lines are too long

View File

@@ -1,7 +1,7 @@
{
"id": "obsidian-vim-im-switch-plugin",
"name": "Vim Input Method Switch",
"version": "1.0.8",
"version": "2.0.3",
"minAppVersion": "0.9.12",
"description": "Switch input method with fcitx-remote when Vim keymap is enabled.",
"author": "Yuan Chen",