Skip to content

Commit 3614e02

Browse files
committed
feat: <CR> respects mode-specific keymaps
Pass <CR> through handleInsertKeybind in insert mode. Route normal mode <CR> through processKeystroke for keybind resolution. Fallback to submit when normal mode <CR> has no mapping. Document input_submit interaction and add Chinese translation.
1 parent 5de81e9 commit 3614e02

7 files changed

Lines changed: 538 additions & 71 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ node_modules/
22
.opencode/
33
docs/*
44
!docs/configuration.md
5+
!docs/configuration.zh.md
56
assets/*.mp4
67
assets/screenshot-*.png

README.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@ opencode plugin opencode-vim@latest --global
1414

1515
## Supported Keys
1616

17-
| Key | Behavior |
18-
| --- | --- |
19-
| `<Esc>`, `<C-[>` | Enter normal mode |
20-
| `i`, `a`, `A`, `o`, `O` | Return to insert mode |
21-
| `h`, `j`, `k`, `l`, `w`, `b`, `e`, `$`, `0` | Move through the prompt |
22-
| `x`, `d`, `c`, `y`, `p`, `u`, `<C-r>` | Edit, yank, paste, undo, redo |
23-
| `v`, `V` | Visual and visual-line selection |
24-
| `3w`, `diw`, `ci"`, `yiq`, `dip`, `yib` | Counts and text objects |
25-
| `<CR>` in normal mode | Submit the prompt |
26-
| `/vim` | Toggle Vim mode on or off |
17+
| Key | Behavior |
18+
| ------------------------------------------- | -------------------------------------------- |
19+
| `<Esc>`, `<C-[>` | Enter normal mode |
20+
| `i`, `a`, `A`, `o`, `O` | Return to insert mode |
21+
| `h`, `j`, `k`, `l`, `w`, `b`, `e`, `$`, `0` | Move through the prompt |
22+
| `x`, `d`, `c`, `y`, `p`, `u`, `<C-r>` | Edit, yank, paste, undo, redo |
23+
| `v`, `V` | Visual and visual-line selection |
24+
| `3w`, `diw`, `ci"`, `yiq`, `dip`, `yib` | Counts and text objects |
25+
| `<CR>` in normal mode | Submit (default). Configurable via `keymaps` |
26+
| `/vim` | Toggle Vim mode on or off |
2727

2828
See [docs/configuration.md](./docs/configuration.md) for configuration options and keymap examples.
29+
30+
中文文档:[README.zh.md](./README.zh.md) | [docs/configuration.zh.md](./docs/configuration.zh.md)

README.zh.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# opencode-vim
2+
3+
为 OpenCode 输入框添加 Vim 风格的 insert 和 normal 模式编辑。
4+
5+
![Demo](./assets/demo2.gif)
6+
7+
## 安装
8+
9+
通过命令行安装:
10+
11+
```bash
12+
opencode plugin opencode-vim@latest --global
13+
```
14+
15+
## 快捷键
16+
17+
| 按键 | 说明 |
18+
| ------------------------------------------- | ---------------------------------------- |
19+
| `<Esc>`, `<C-[>` | 进入 normal 模式 |
20+
| `i`, `a`, `A`, `o`, `O` | 返回 insert 模式 |
21+
| `h`, `j`, `k`, `l`, `w`, `b`, `e`, `$`, `0` | 移动光标 |
22+
| `x`, `d`, `c`, `y`, `p`, `u`, `<C-r>` | 编辑、复制、粘贴、撤销、重做 |
23+
| `v`, `V` | 可视模式和可视行模式 |
24+
| `3w`, `diw`, `ci"`, `yiq`, `dip`, `yib` | 计数和文本对象 |
25+
| normal 模式下 `<CR>` | 提交 prompt(默认,可通过 keymaps 配置) |
26+
| `/vim` | 开关 Vim 模式 |
27+
28+
配置文档:[docs/configuration.zh.md](./docs/configuration.zh.md)

docs/configuration.md

Lines changed: 58 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ Add `opencode-vim` to the `plugin` array in your OpenCode `tui.jsonc` file:
88

99
```jsonc
1010
{
11-
"$schema": "https://opencode.ai/tui.json",
12-
"plugin": [
13-
[
14-
"./plugin/opencode-vim",
15-
{
16-
"autoUpdate": true,
17-
"vim": {
18-
"defaultMode": "insert"
19-
}
20-
}
21-
]
22-
]
11+
"$schema": "https://opencode.ai/tui.json",
12+
"plugin": [
13+
[
14+
"./plugin/opencode-vim",
15+
{
16+
"autoUpdate": true,
17+
"vim": {
18+
"defaultMode": "insert",
19+
},
20+
},
21+
],
22+
],
2323
}
2424
```
2525

@@ -29,41 +29,41 @@ If your plugin is installed somewhere else, change the plugin path to match your
2929

3030
```jsonc
3131
{
32-
"$schema": "https://opencode.ai/tui.json",
33-
"plugin": [
34-
[
35-
"./plugin/opencode-vim",
36-
{
37-
"autoUpdate": true,
38-
"vim": {
39-
"defaultMode": "insert",
40-
"keymapTimeout": 500,
41-
"pendingDisplayDelay": 120,
42-
"cursorStyles": {
43-
"insert": {
44-
"style": "line",
45-
"blinking": true
46-
},
47-
"normal": {
48-
"style": "block",
49-
"blinking": true
50-
}
51-
},
52-
"debug": false,
53-
"debugPath": "/home/you/.cache/opencode/opencode-vim.log",
54-
"keymaps": {
55-
"insert": {
56-
"kj": "normal"
32+
"$schema": "https://opencode.ai/tui.json",
33+
"plugin": [
34+
[
35+
"./plugin/opencode-vim",
36+
{
37+
"autoUpdate": true,
38+
"vim": {
39+
"defaultMode": "insert",
40+
"keymapTimeout": 500,
41+
"pendingDisplayDelay": 120,
42+
"cursorStyles": {
43+
"insert": {
44+
"style": "line",
45+
"blinking": true,
46+
},
47+
"normal": {
48+
"style": "block",
49+
"blinking": true,
50+
},
51+
},
52+
"debug": false,
53+
"debugPath": "/home/you/.cache/opencode/opencode-vim.log",
54+
"keymaps": {
55+
"insert": {
56+
"kj": "normal",
57+
},
58+
"normal": {
59+
"<CR>": "submit",
60+
"Y": "y$",
61+
},
62+
},
63+
},
5764
},
58-
"normal": {
59-
"<CR>": "submit",
60-
"Y": "y$"
61-
}
62-
}
63-
}
64-
}
65-
]
66-
]
65+
],
66+
],
6767
}
6868
```
6969

@@ -268,11 +268,14 @@ Each keymap entry maps a key sequence to an action:
268268

269269
Supported built-in actions:
270270

271-
- `"normal"` exits insert mode and enters normal mode.
272-
- `"insert"` enters insert mode.
273-
- `"submit"` submits the OpenCode prompt.
271+
| Action | Modes | Effect |
272+
|---|---|---|
273+
| `"submit"` | insert, normal | Submits the prompt |
274+
| `"normal"` | insert only | Exits insert mode, enters normal mode |
275+
| `"insert"` | normal only | Enters insert mode |
276+
| `"<vim keys>"` | normal only | Executes the configured Vim key sequence |
274277

275-
Any other action string is treated as a Vim key sequence. For example, this maps `Y` to yank from the cursor to the end of the line:
278+
For example, this maps `Y` to yank from the cursor to the end of the line:
276279

277280
```jsonc
278281
"keymaps": {
@@ -282,6 +285,10 @@ Any other action string is treated as a Vim key sequence. For example, this maps
282285
}
283286
```
284287

288+
Unlike other keys, `<CR>` in normal mode defaults to `"submit"` when no mapping is configured. In insert mode, if `<CR>` is unmapped and OpenCode's `input_submit` is not `return`, it inserts a newline instead of submitting.
289+
290+
> **Note:** When `input_submit` is not `"return"`, make sure to also set `"input_newline": "return"` in the OpenCode `keybinds` config. Without it, `<CR>` in insert mode does nothing (neither submits nor inserts a newline).
291+
285292
## Keymap Syntax
286293

287294
Key sequences can contain printable ASCII characters, except literal spaces. Use `<Space>` for the space key.
@@ -331,7 +338,7 @@ Use `kj` or `jk` to leave insert mode:
331338
}
332339
```
333340

334-
Submit the prompt with Enter in normal mode:
341+
Submit with `<CR>` in normal mode is the default — this keymap is optional:
335342

336343
```jsonc
337344
"keymaps": {

0 commit comments

Comments
 (0)