nvim: ft keymaps via ftplugin/

This commit is contained in:
js0ny 2025-11-17 16:54:15 +00:00
parent 51e6343b9e
commit 32429308f3
22 changed files with 112 additions and 213 deletions

View file

@ -1,8 +1,8 @@
vim.keymap.set("n", "<leader>mp", '<cmd>!jq<CR>', { local prefmap = {
desc = "Pretiffy json", { keys = "p", cmd = "<cmd>%!jq<CR>", opts = { desc = "Mark the file as executable" } },
buffer = true }
})
vim.keymap.set("i", "<C-m>p", '<cmd>%!jq<CR>', { local set_buf_keymaps_prefix = require("keymaps.utils").set_buf_keymaps_prefix
desc = "Pretiffy json", -- local set_buf_keymaps = require("keymaps.utils").set_buf_keymaps
buffer = true
}) set_buf_keymaps_prefix(prefmap)

View file

@ -1,8 +1,8 @@
vim.keymap.set("n", "<leader>mp", '<cmd>!jq<CR>', { local prefmap = {
desc = "Pretiffy json", { keys = "p", cmd = "<cmd>%!jq<CR>", opts = { desc = "Mark the file as executable" } },
buffer = true }
})
vim.keymap.set("i", "<C-m>p", '<cmd>%!jq<CR>', { local set_buf_keymaps_prefix = require("keymaps.utils").set_buf_keymaps_prefix
desc = "Pretiffy json", -- local set_buf_keymaps = require("keymaps.utils").set_buf_keymaps
buffer = true
}) set_buf_keymaps_prefix(prefmap)

View file

@ -1,3 +1,16 @@
vim.bo.shiftwidth = 2 vim.bo.shiftwidth = 2
vim.bo.softtabstop = 2 vim.bo.softtabstop = 2
vim.bo.expandtab = true vim.bo.expandtab = true
local bufmap = {
{ 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" } },
}
-- local set_buf_keymaps_prefix = require("keymaps.utils").set_buf_keymaps_prefix
local set_buf_keymaps = require("keymaps.utils").set_buf_keymaps
set_buf_keymaps(bufmap)

View file

@ -0,0 +1,11 @@
local prefmap = {
{ 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 = "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" } },
}
local set_buf_keymaps_prefix = require("keymaps.utils").set_buf_keymaps_prefix
-- local set_buf_keymaps = require("keymaps.utils").set_buf_keymaps
set_buf_keymaps_prefix(prefmap)

View file

@ -1,16 +1,9 @@
vim.keymap.set("n", "<leader>mx", '<cmd>!chmod +x "%"<CR>', { local prefmap = {
desc = "Mark the file as executable", { keys = "x", cmd = '<cmd>!chmod +x "%"<CR>', opts = { desc = "Mark the file as executable" } },
buffer = true { keys = "X", cmd = '<cmd>!chmod u+x "%"<CR>', opts = { desc = "Mark the file as executable (current user only)" } },
}) }
vim.keymap.set("i", "<C-m>x", '<cmd>!chmod +x "%"<CR>', {
desc = "Mark the file as executable", local set_buf_keymaps_prefix = require("keymaps.utils").set_buf_keymaps_prefix
buffer = true -- local set_buf_keymaps = require("keymaps.utils").set_buf_keymaps
})
vim.keymap.set("n", "<leader>mX", '<cmd>!chmod u+x "%"<CR>', { set_buf_keymaps_prefix(prefmap)
desc = "Mark the file as executable (current user only)",
buffer = true
})
vim.keymap.set("i", "<C-m>X", '<cmd>!chmod u+x "%"<CR>', {
desc = "Mark the file as executable (current user only)",
buffer = true
})

View file

@ -0,0 +1,17 @@
local bufmap = {
{ 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" } },
}
local set_buf_keymaps = require("keymaps.utils").set_buf_keymaps
set_buf_keymaps(bufmap)

View file

@ -10,12 +10,12 @@
require("config.options") require("config.options")
local term = os.getenv("TERM") or "" local term = os.getenv("TERM") or ""
if vim.g.vscode then -- TODO: VSCode Neovim Integration if vim.g.vscode then -- TODO: VSCode Neovim Integration
require("config.vscode") require("profiles.vscode")
elseif term == "linux" then -- Under tty elseif term == "linux" then -- Under tty
require("config.tty") require("profiles.tty")
vim.cmd("colorscheme vim") -- Use minimal colorscheme vim.cmd("colorscheme vim") -- Use minimal colorscheme
else else
require("config.plugins") require("profiles.vanilla")
require("config.colorscheme") require("config.colorscheme")
end end
require("config.keymaps") require("config.keymaps")

View file

@ -1,3 +1,5 @@
-- Source all lsp definition in
-- ~/.config/nvim/lsp/*.lua
local lsp_configs = {} local lsp_configs = {}
for _, v in ipairs(vim.api.nvim_get_runtime_file("lsp/*", true)) do for _, v in ipairs(vim.api.nvim_get_runtime_file("lsp/*", true)) do
local name = vim.fn.fnamemodify(v, ":t:r") local name = vim.fn.fnamemodify(v, ":t:r")

View file

@ -1,7 +1,8 @@
-- <leader> is space -- <leader> is space
vim.g.mapleader = " " vim.g.mapleader = " "
vim.g.maplocalleader = "\\" vim.g.maplocalleader = "\\"
-- Disable netrw (file explorer) use NvimTree instead -- Disable netrw (file explorer) use neo-tree instead
-- See: lua/plugins/mod/neo-tree.lua
vim.g.loaded_netrw = 1 vim.g.loaded_netrw = 1
vim.g.loaded_netrwPlugin = 1 vim.g.loaded_netrwPlugin = 1
-- Disable Perl -- Disable Perl

View file

@ -1,36 +0,0 @@
--- Available LSP goes here
--- Check https://github.com/neovim/nvim-lspconfig/blob/master/doc/configs.md
--- for available server and name
local M = {}
-- Ensure that the following servers are installed and set
-- Use :Mason to list all available servers
M.servers = {
"bashls", -- Bash
"clangd", -- C/C++
"gopls", -- Go
"html", -- HTML
"jsonls", -- JSON
"lua_ls", -- Lua
-- "markdown_oxide", -- Markdown
"pyright", -- Python
"rust_analyzer", -- Rust
"taplo", -- TOML
"ts_ls", -- TypeScript
"vimls", -- vimscript
"yamlls", -- YAML
"beancount", -- Beancount
}
-- Configuration for each server defines here
M.server_config = {
lua_ls = {
capabilities = vim.lsp.protocol.make_client_capabilities(),
settings = {
Lua = {
diagnostics = { globals = { "vim" } },
},
},
},
}
return M

View file

@ -1,6 +1,6 @@
local mode_arrow = { "n", "v", "s", "x", "o" } 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 -- https://github.com/LazyVim/LazyVim/blob/d1529f650fdd89cb620258bdeca5ed7b558420c7/lua/lazyvim/config/keymaps.lua#L8
{ {
mode = mode_arrow, mode = mode_arrow,
@ -95,6 +95,8 @@ local keymaps_basic = { -- Modification of Original Keymap - Colemak
cmd = "5e", cmd = "5e",
opts = { desc = "Down 5 Lines" }, 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" } }, { keys = "Y", cmd = "y$", opts = { desc = "Yank to End of Line" } },
{ mode = mode_arrow, keys = "J", cmd = "5j" }, { mode = mode_arrow, keys = "J", cmd = "5j" },
{ mode = mode_arrow, keys = "K", cmd = "5k" }, { mode = mode_arrow, keys = "K", cmd = "5k" },

View file

@ -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)

View file

@ -11,7 +11,7 @@ local keymaps_basic = require("keymaps.basic")
local keymaps_modifier = require("keymaps.modifier") local keymaps_modifier = require("keymaps.modifier")
local keymaps_terminal = require("keymaps.tmap") local keymaps_terminal = require("keymaps.tmap")
require("keymaps.buffer") -- require("keymaps.buffer")
utils.set_keymaps(keymaps_general) utils.set_keymaps(keymaps_general)
utils.set_keymaps(keymaps_basic) utils.set_keymaps(keymaps_basic)

View file

@ -86,6 +86,16 @@ local leader_mappings = {
cmd = ":let @+ = expand('%') . ':' . line('.')<CR>", cmd = ":let @+ = expand('%') . ':' . line('.')<CR>",
opts = { desc = "Copy relative file path with line number" }, 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 g = { -- +git/version control
}, },

View file

@ -1,3 +1,5 @@
-- Terminal Keymaps
-- Use <C-w>h|j|k|l to navigate directly
local M = { local M = {
{ {
keys = "<C-w>h", keys = "<C-w>h",

View file

@ -21,13 +21,28 @@ M.set_lang_keymaps = function(maps)
}) })
end end
-- This fuction is used in ftplugin/*.lua
M.set_buf_keymaps = function(maps) M.set_buf_keymaps = function(maps)
vim.api.create_autocmd("BufEnter", { if not maps then
pattern = maps.filetype, return
callback = function() end
M.set_keymaps(maps.keymaps) for _, map in ipairs(maps) do
end, 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 end
return M return M

View file

@ -1,29 +0,0 @@
-- This won't be loaded
-- I keep this since render-markdown sometimes buggy
return {
{
"OXY2DEV/markview.nvim",
lazy = false,
dependencies = {
"nvim-treesitter/nvim-treesitter",
"nvim-tree/nvim-web-devicons",
},
opts = {
checkboxes = require("markview-presets").checkboxes.nerd,
headings = {
enable = true,
shift_width = 1,
heading_1 = {
style = "label",
hl = "MarkviewH1",
},
},
code_blocks = {
style = "language",
language_direction = "right",
hl = "MarkviewCode",
info_hl = "MarkviewCodeInfo",
},
},
},
}

View file

@ -69,7 +69,7 @@ return {
-- require("telescope").load_extension("orgmode") -- require("telescope").load_extension("orgmode")
-- vim.keymap.set("n", "<leader>r", require("telescope").extensions.orgmode.refile_heading) -- vim.keymap.set("n", "<leader>r", require("telescope").extensions.orgmode.refile_heading)
-- vim.keymap.set("n", "<leader>oP", require("telescope").extensions.orgmode.search_headings) -- vim.keymap.set("n", "<leader>oP", require("telescope").extensions.orgmode.search_headings)
vim.keymap.set("n", "<leader>op", "<cmd>FzfLua files cwd=~/OrgFiles<CR>") -- vim.keymap.set("n", "<leader>op", "<cmd>FzfLua files cwd=~/OrgFiles<CR>")
-- vim.keymap.set("n", "<leader>li", require("telescope").extensions.orgmode.insert_link) -- vim.keymap.set("n", "<leader>li", require("telescope").extensions.orgmode.insert_link)
end, end,
}, },

View file

@ -6,7 +6,7 @@ if not (vim.uv or vim.loop).fs_stat(lazypath) then
if vim.v.shell_error ~= 0 then if vim.v.shell_error ~= 0 then
vim.api.nvim_echo({ vim.api.nvim_echo({
{ "Failed to clone lazy.nvim:\n", "ErrorMsg" }, { "Failed to clone lazy.nvim:\n", "ErrorMsg" },
{ out, "WarningMsg" }, { out, "WarningMsg" },
{ "\nPress any key to exit..." }, { "\nPress any key to exit..." },
}, true, {}) }, true, {})
vim.fn.getchar() vim.fn.getchar()
@ -14,9 +14,3 @@ if not (vim.uv or vim.loop).fs_stat(lazypath) then
end end
end end
vim.opt.rtp:prepend(lazypath) vim.opt.rtp:prepend(lazypath)
-- Make sure to setup `mapleader` and `maplocalleader` before
-- loading lazy.nvim so that mappings are correct.
-- This is also a good place to setup other settings (vim.opt)
vim.g.mapleader = " "
vim.g.maplocalleader = "\\"