mirror of
https://github.com/js0ny/dotfiles.git
synced 2025-12-21 08:43:00 +00:00
nvim: ft keymaps via ftplugin/
This commit is contained in:
parent
51e6343b9e
commit
32429308f3
22 changed files with 112 additions and 213 deletions
|
|
@ -1,6 +1,6 @@
|
|||
local mode_arrow = { "n", "v", "s", "x", "o" }
|
||||
|
||||
local keymaps_basic = { -- Modification of Original Keymap - Colemak
|
||||
local keymaps_basic = {
|
||||
-- https://github.com/LazyVim/LazyVim/blob/d1529f650fdd89cb620258bdeca5ed7b558420c7/lua/lazyvim/config/keymaps.lua#L8
|
||||
{
|
||||
mode = mode_arrow,
|
||||
|
|
@ -95,6 +95,8 @@ local keymaps_basic = { -- Modification of Original Keymap - Colemak
|
|||
cmd = "5e",
|
||||
opts = { desc = "Down 5 Lines" },
|
||||
},
|
||||
{ keys = "<Tab>", cmd = "<C-w><C-w>", opts = { desc = "Focus on other panel" } },
|
||||
{ keys = "<CR>", cmd = "%" },
|
||||
{ keys = "Y", cmd = "y$", opts = { desc = "Yank to End of Line" } },
|
||||
{ mode = mode_arrow, keys = "J", cmd = "5j" },
|
||||
{ mode = mode_arrow, keys = "K", cmd = "5k" },
|
||||
|
|
|
|||
|
|
@ -1,96 +0,0 @@
|
|||
-- Use leader + m for `major`, inspired by emacs
|
||||
-- If under mode = 'n' / 'v': <leader>m and <localleader> will be added by default
|
||||
-- If under `i`
|
||||
local bufmap = {
|
||||
markdown = {
|
||||
{ mode = "x", keys = "i", cmd = 'c*<C-r>"*', opt = { desc = "Add italic to selected text" } },
|
||||
{ mode = "x", keys = "b", cmd = 'c**<C-r>"**', opt = { desc = "Add bold to selected text" } },
|
||||
{ mode = "x", keys = "c", cmd = 'c`<CR><C-r>"<CR>`', opt = { desc = "Add code block to selected text" } },
|
||||
{ mode = "x", keys = "d", cmd = 'c~~<C-r>"~~', opt = { desc = "Add strikethrough to selected text" } },
|
||||
{ mode = "x", keys = "h", cmd = 'c==<C-r>"==', opt = { desc = "Add highlight to selected text" } },
|
||||
},
|
||||
tex = {
|
||||
{ mode = "x", keys = "i", cmd = 'c\\textit{<C-r>"}', opt = { desc = "Add italic to selected text" } },
|
||||
{ mode = "x", keys = "b", cmd = 'c\\textbf{<C-r>"}', opt = { desc = "Add bold to selected text" } },
|
||||
{
|
||||
mode = "x",
|
||||
keys = "c",
|
||||
cmd = 'c\\begin{verbatim}<CR><C-r>"<CR>\\end{verbatim}',
|
||||
opt = { desc = "Add code block to selected text" },
|
||||
},
|
||||
{ mode = "x", keys = "d", cmd = 'c\\sout{<C-r>"}', opt = { desc = "Add strikethrough to selected text" } },
|
||||
{ mode = "x", keys = "h", cmd = 'c\\hl{<C-r>"}', opt = { desc = "Add highlight to selected text" } },
|
||||
{ mode = "n", keys = "<leader>cc", cmd = "<cmd>w<CR>", opt = { desc = "Save and compile tex file" } },
|
||||
-- { mode = "i", keys = "<C-m><C-m>", cmd = "<cmd>w<CR>", opt = { desc = "Save and compile tex file" } },
|
||||
},
|
||||
org = {
|
||||
{ mode = "x", keys = "i", cmd = 'c/<C-r>/"', opt = { desc = "Add italic to selected text" } },
|
||||
{ mode = "x", keys = "b", cmd = 'c*<C-r>"*', opt = { desc = "Add bold to selected text" } },
|
||||
-- {
|
||||
-- mode = "x",
|
||||
-- keys = "c",
|
||||
-- cmd = 'c#+BEGIN_SRC<CR><C-r>"<CR>#+END_SRC',
|
||||
-- opt = { desc = "Add code block to selected text" },
|
||||
-- },
|
||||
{ mode = "x", keys = "d", cmd = 'c+<C-r>"+', opt = { desc = "Add strikethrough to selected text" } },
|
||||
{ mode = "x", keys = "h", cmd = 'c~<C-r>"~', opt = { desc = "Add highlight to selected text" } },
|
||||
},
|
||||
sh = {
|
||||
{ mode = "n", keys = "<leader>mx", cmd = "<cmd>!chmod u+x %<CR>", opt = { desc = "Mark the file as executable" } },
|
||||
},
|
||||
json = {
|
||||
{ mode = "n", keys = "<leader>mp", cmd = "<cmd>%!jq", opt = { desc = "Prettify json" } },
|
||||
},
|
||||
}
|
||||
|
||||
-- Shallow copy
|
||||
local function extend(tbl1, tbl2)
|
||||
local t = {}
|
||||
for _, v in ipairs(tbl1 or {}) do
|
||||
table.insert(t, v)
|
||||
end
|
||||
for _, v in ipairs(tbl2 or {}) do
|
||||
table.insert(t, v)
|
||||
end
|
||||
return t
|
||||
end
|
||||
|
||||
for _, ft in ipairs({ "bash", "zsh", "fish", "python" }) do
|
||||
bufmap[ft] = extend(bufmap[ft], bufmap.sh)
|
||||
end
|
||||
|
||||
local function setup_buffer_maps(buffer_map)
|
||||
-- 遍历 buffer_map 中的每个文件类型
|
||||
for ft, mappings in pairs(buffer_map) do
|
||||
-- 1. 为现有缓冲区设置键位映射
|
||||
for _, buf in ipairs(vim.api.nvim_list_bufs()) do
|
||||
if vim.api.nvim_buf_get_option(buf, "filetype") == ft then
|
||||
for _, mapping in ipairs(mappings) do
|
||||
-- 合并选项,添加 buffer 以确保映射是缓冲区局部的
|
||||
if mapping.mode ~= "n" then
|
||||
mapping.keys = "<C-m>" .. mapping.keys
|
||||
end
|
||||
local opts = vim.tbl_extend("force", mapping.opt, { buffer = buf })
|
||||
vim.keymap.set(mapping.mode, mapping.keys, mapping.cmd, opts)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- 2. 为未来缓冲区设置自动命令
|
||||
vim.api.nvim_create_autocmd("FileType", {
|
||||
pattern = ft, -- 匹配文件类型,例如 "markdown"
|
||||
callback = function(args)
|
||||
local buf = args.buf -- 获取触发事件的缓冲区号
|
||||
for _, mapping in ipairs(mappings) do
|
||||
local opts = vim.tbl_extend("force", mapping.opt, { buffer = buf })
|
||||
if mapping.mode ~= "n" then
|
||||
mapping.keys = "<C-m>" .. mapping.keys
|
||||
end
|
||||
vim.keymap.set(mapping.mode, mapping.keys, mapping.cmd, opts)
|
||||
end
|
||||
end,
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
setup_buffer_maps(bufmap)
|
||||
|
|
@ -11,7 +11,7 @@ local keymaps_basic = require("keymaps.basic")
|
|||
local keymaps_modifier = require("keymaps.modifier")
|
||||
local keymaps_terminal = require("keymaps.tmap")
|
||||
|
||||
require("keymaps.buffer")
|
||||
-- require("keymaps.buffer")
|
||||
|
||||
utils.set_keymaps(keymaps_general)
|
||||
utils.set_keymaps(keymaps_basic)
|
||||
|
|
|
|||
|
|
@ -86,6 +86,16 @@ local leader_mappings = {
|
|||
cmd = ":let @+ = expand('%') . ':' . line('.')<CR>",
|
||||
opts = { desc = "Copy relative file path with line number" },
|
||||
},
|
||||
{ keys= "p",
|
||||
cmd = function()
|
||||
local filepath = vim.fn.expand('%:.')
|
||||
if filepath == '' then
|
||||
vim.notify("No file path (buffer is unnamed)", vim.log.levels.WARN)
|
||||
else
|
||||
vim.notify(filepath, vim.log.levels.INFO)
|
||||
end
|
||||
end, opts = { desc = "Print current file path" }
|
||||
}
|
||||
},
|
||||
g = { -- +git/version control
|
||||
},
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
-- Terminal Keymaps
|
||||
-- Use <C-w>h|j|k|l to navigate directly
|
||||
local M = {
|
||||
{
|
||||
keys = "<C-w>h",
|
||||
|
|
|
|||
|
|
@ -21,13 +21,28 @@ M.set_lang_keymaps = function(maps)
|
|||
})
|
||||
end
|
||||
|
||||
-- This fuction is used in ftplugin/*.lua
|
||||
M.set_buf_keymaps = function(maps)
|
||||
vim.api.create_autocmd("BufEnter", {
|
||||
pattern = maps.filetype,
|
||||
callback = function()
|
||||
M.set_keymaps(maps.keymaps)
|
||||
end,
|
||||
})
|
||||
if not maps then
|
||||
return
|
||||
end
|
||||
for _, map in ipairs(maps) do
|
||||
local opts = vim.tbl_extend("force", map.opt or {}, { buffer = true })
|
||||
vim.keymap.set(map.mode, map.keys, map.cmd, opts)
|
||||
end
|
||||
end
|
||||
|
||||
M.set_buf_keymaps_prefix = function(maps)
|
||||
local n_prefix = "<leader>m"
|
||||
local i_prefix = "<C-m>"
|
||||
if not maps then
|
||||
return
|
||||
end
|
||||
for _, map in ipairs(maps) do
|
||||
local opts = vim.tbl_extend("force", map.opt or {}, { buffer = true })
|
||||
vim.keymap.set("n", n_prefix .. map.keys, map.cmd, opts)
|
||||
vim.keymap.set("i", i_prefix .. map.keys, map.cmd, opts)
|
||||
end
|
||||
end
|
||||
|
||||
return M
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue