581 lines
16 KiB
Plaintext
581 lines
16 KiB
Plaintext
# 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
|