-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgenerate-keymaps-markdown.lua
50 lines (43 loc) · 1.63 KB
/
generate-keymaps-markdown.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
local function generate_keys_markdown_table()
-- Get all key mappings
local modes = { "n", "i", "v", "x", "c", "t" }
local keymaps = {}
for _, mode in ipairs(modes) do
local mappings = vim.api.nvim_get_keymap(mode)
for _, mapping in ipairs(mappings) do
if
mapping.lhs
and mapping.rhs
and mapping.desc
and mapping.lhs ~= ""
and mapping.rhs ~= ""
and mapping.desc ~= ""
then
local lhs = mapping.lhs:gsub(" ", "<Leader>"):gsub("|", "\\|"):gsub("`", "\\`")
local rhs = mapping.rhs:gsub("|", "\\|"):gsub("`", "\\`")
local desc = mapping.desc:gsub("|", "\\|"):gsub("`", "\\`")
table.insert(keymaps, { mode = mode, lhs = lhs, rhs = rhs, desc = desc })
end
end
end
-- Generate Markdown table
local lines = {}
table.insert(lines, "# ⌨️ Neovim Key Mappings")
table.insert(lines, "This file was generated by [`generate-keymaps-markdown.lua`](./generate-keymaps-markdown.lua).")
table.insert(lines, "")
table.insert(lines, "| Mode | Keys | Description | Command | ")
table.insert(lines, "|------|------|-------------|---------|")
for _, map in ipairs(keymaps) do
table.insert(lines, string.format("| %s | `%s` | `%s` | `%s` |", map.mode, map.lhs, map.desc, map.rhs))
end
local filename = "README.md"
-- Write to a markdown file
local script_path = debug.getinfo(1).source:match("@?(.*/)")
local file = io.open(script_path .. filename, "w")
for _, line in ipairs(lines) do
file:write(line .. "\n")
end
file:close()
print("Key mappings have been written to " .. filename)
end
generate_keys_markdown_table()