Skip to content

Commit 89c1212

Browse files
committed
Rework to better track key presses
1 parent 1ba4ea2 commit 89c1212

File tree

3 files changed

+63
-51
lines changed

3 files changed

+63
-51
lines changed

.example.png

84.1 KB
Loading

README.md

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# vim-kaizen
2-
3-
`vim-kaizen` is a NeoVim plugin to encourage continuous improvement in your Vim keybindings.
4-
It reminds you to use alternative and better keys for increased productivity and efficiency.
2+
Inspired by the Japanese philosophy of kaizen (改善)—the practice of continuous
3+
improvement—`vim-kaizen` helps you refine your keystrokes to make you a more
4+
efficient Vim user.
55

66
## Features
77

@@ -31,21 +31,16 @@ Vim commands and their alternatives:
3131
| ⬇️ | j |
3232
| $a | A |
3333
| ^o | o |
34-
| ca) | cab |
35-
| ca) | cab |
36-
| ci) | cib |
37-
| ci) | cib |
38-
| ca( | cab |
39-
| ca( | cab |
40-
| ci( | cib |
4134
| ^i | I |
4235
| $a | A |
43-
| j^ | + |
36+
| ^j | + |
4437

4538
## Installation
4639

47-
#### `vim-plug`
40+
| Plugin Manager | Plugin Section |
41+
| --- | --- |
42+
|Vim-Plug | `Plug 'mo42/vim-kaizen'` |
43+
44+
## License
4845

49-
```vim
50-
Plug 'mo42/vim-kaizen'
51-
```
46+
This project is licensed under the MIT License - see the LICENSE file for details.

plugin/vim-kaizen.lua

Lines changed: 53 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,53 @@
1-
function VimKaizen(pat, alt)
1+
_G.vim_kaizen_buffer = {}
2+
3+
_G.vim_kaizen_patterns = {
4+
['d$'] = "D",
5+
['jj'] = "2j",
6+
['y$'] = "Y",
7+
['ggVG'] = 'yG',
8+
['cc'] = 'S',
9+
['xi'] = 's',
10+
['cl'] = 's',
11+
['dwi'] = 'cw',
12+
['dsi'] = 'cs',
13+
['dpi'] = 'cp',
14+
['d2wi'] = 'c2w',
15+
['d3wi'] = 'c3w',
16+
['<Right>'] = 'h',
17+
['<Left>'] = 'l',
18+
['<Up>'] = 'k',
19+
['<Down>'] = 'j',
20+
['$a'] = 'A',
21+
['^o'] = 'o',
22+
['^i'] = 'I',
23+
['$a'] = 'A',
24+
['^j'] = '+',
25+
}
26+
27+
function vim_kaizen_keypress(key)
28+
local max_buffer_length = 3
29+
table.insert(_G.vim_kaizen_buffer, key)
30+
if #_G.vim_kaizen_buffer > max_buffer_length then
31+
table.remove(_G.vim_kaizen_buffer, 1)
32+
end
33+
if #_G.vim_kaizen_buffer > 1 then
34+
for i = 1, #_G.vim_kaizen_buffer do
35+
local pat = table.concat(_G.vim_kaizen_buffer, "", i, #_G.vim_kaizen_buffer)
36+
if _G.vim_kaizen_patterns[pat] then
37+
vim_kaizen(pat)
38+
end
39+
end
40+
end
41+
end
42+
43+
vim.on_key(function(key)
44+
if vim.api.nvim_get_mode().mode ~= 'i' then
45+
vim_kaizen_keypress(vim.fn.keytrans(key))
46+
end
47+
end, vim.api.nvim_create_namespace("global_key_listener"))
48+
49+
function vim_kaizen(pat)
50+
local alt = _G.vim_kaizen_patterns[pat]
251
local txt1 = string.format('You entered "%s".', pat)
352
local txt2 = string.format('Consider using "%s" from now on.', alt)
453
local txt3 = 'Press "q" or ESC to close window.'
@@ -8,6 +57,9 @@ function VimKaizen(pat, alt)
857
end
958
local buf = vim.api.nvim_create_buf(false, true)
1059
vim.api.nvim_buf_set_lines(buf, 0, -1, false, {txt1, txt2, '', txt3, '', txt4})
60+
vim.api.nvim_buf_set_option(buf, "buftype", "nofile")
61+
vim.api.nvim_buf_set_option(buf, "modifiable", false)
62+
vim.api.nvim_buf_set_option(buf, "readonly", true)
1163
local ui = vim.api.nvim_list_uis()[1] or vim.api.nvim_list_uis()[0]
1264

1365
local width = 50
@@ -28,38 +80,3 @@ function VimKaizen(pat, alt)
2880
vim.api.nvim_buf_set_keymap(buf, 'n', '<Esc>', '<Cmd>bwipeout!<CR>', {noremap = true, silent = true})
2981
vim.api.nvim_buf_set_keymap(buf, 'n', 'q', '<Cmd>bwipeout!<CR>', {noremap = true, silent = true})
3082
end
31-
32-
local vimKaizenPatterns = {
33-
{'n', 'd$', 'D'},
34-
{'n', 'y$', 'Y'},
35-
{'n', 'ggVG', 'yG'},
36-
{'n', 'cc', 'S'},
37-
{'n', 'xi', 's'},
38-
{'n', 'cl', 's'},
39-
{'n', 'dwi', 'cw'},
40-
{'n', 'dsi', 'cs'},
41-
{'n', 'dpi', 'cp'},
42-
{'n', 'd2wi', 'c2w'},
43-
{'n', 'd3wi', 'c3w'},
44-
{'', '<Right>', 'h'},
45-
{'', '<Left>', 'l'},
46-
{'', '<Up>', 'k'},
47-
{'', '<Down>', 'j'},
48-
{'n', '$a', 'A'},
49-
{'n', '^o', 'o'},
50-
{'n', 'ca)','cab'},
51-
{'n', 'ca)','cab'},
52-
{'n', 'ci)','cib'},
53-
{'n', 'ci)','cib'},
54-
{'n', 'ca(','cab'},
55-
{'n', 'ca(','cab'},
56-
{'n', 'ci(','cib'},
57-
{'n', '^i', 'I'},
58-
{'n', '$a', 'A'},
59-
{'n', 'j^', '+'},
60-
}
61-
62-
for _, tuple in ipairs(vimKaizenPatterns) do
63-
local map, pat, alt = table.unpack(tuple)
64-
vim.keymap.set(map, pat, function() VimKaizen(pat, alt) end, { noremap = true, silent = true })
65-
end

0 commit comments

Comments
 (0)