marleyvim/modules/nixvim/keymaps/default.nix
2024-11-24 19:46:48 -08:00

430 lines
8.7 KiB
Nix

{ lib, ... }:
let
inherit (lib)
map
mapAttrsToList
toUpper
substring
mapCartesianProduct
toLower
;
in
{
globals = {
mapleader = " ";
maplocalleader = "\\";
};
keymaps =
# Disable Arrow Key Movement - - - - - - - - - - - - - - - - - - - - - - - -
(map
(d: {
mode = [
"i"
"n"
];
key = d;
action = "<nop>";
})
[
"<Down>"
"<Up>"
"<Left>"
"<Right>"
]
)
# Better Up/Down - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
++ (mapAttrsToList
(d: k: {
mode = [
"n"
"x"
];
key = k;
action = "v:count == 0 ? 'g${k}' : '${k}'";
options = {
desc = d;
expr = true;
silent = true;
};
})
{
Down = "j";
Up = "k";
}
)
# Resize Windows - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
++ (mapAttrsToList
(
k: d:
let
s = if (d == "Increase") then "+" else "-";
in
{
mode = [ "n" ];
key = "<C-${k}>";
action = "<cmd>resize ${s}4<cr>";
options = {
desc = "${d} Window Height";
};
}
)
{
Up = "Increase";
Down = "Decrease";
Left = "Decrease";
Right = "Increase";
}
)
# Buffers - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
++ (mapAttrsToList
(
k: d:
let
# previous -> Prev
prettyDir = (toUpper (substring 0 1 d)) + (substring 1 3 d);
in
{
mode = [ "n" ];
key = k;
action = "<cmd>b${d}<cr>";
options = {
desc = "${prettyDir} Buffer";
};
}
)
{
"<S-h>" = "previous";
"<S-l>" = "next";
"[b" = "previous";
"]b" = "next";
}
)
++ [
{
mode = [ "n" ];
key = "<leader>bD";
action = "<cmd>bd<cr>";
options = {
desc = "Delete Buffer and Window";
};
}
]
# Clear Search/Diff Update/Redraw - - - - - - - - - - - - - - - - - - - - -
++ [
{
mode = [
"i"
"n"
];
key = "<esc>";
action = "<cmd>noh<cr><esc>";
options = {
desc = "Escape and Clear hlsearch";
};
}
{
mode = [ "n" ];
key = "<leader>ur";
action = "<cmd>nohlsearch<bar>diffupdate<bar>normal! <C-l><cr>";
options = {
desc = "Redraw / Clear hlsearch / Diff Update";
};
}
]
# Better n & N - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
++ (mapCartesianProduct
(
{ m, d }:
let
nn = if (d == "Next") then "Nn" else "nN";
zv = if (m == "n") then ".'zv'" else "";
in
{
mode = [ m ];
key = if (d == "Next") then "n" else "N";
action = "'${nn}'[v:searchforward]${zv}";
options = {
desc = "${d} Search Result";
expr = true;
};
}
)
{
m = [
"n"
"x"
"o"
];
d = [
"Next"
"Prev"
];
}
)
# Undo Break-Points - - - - - - - - - - - - - - - - - - - - - - - - - - - -
++ (map
(c: {
mode = [ "i" ];
key = c;
action = "${c}<c-g>u";
})
[
","
"."
";"
]
)
# Search Docs (keywordprog) - - - - - - - - - - - - - - - - - - - - - - - -
# https://til.codeinthehole.com/posts/about-how-to-use-keywordprg-effectively/
++ [
{
mode = [ "n" ];
key = "<leader>K";
action = "<cmd>norm! K<cr>";
options = {
desc = "Search <keywordprog> for word";
};
}
]
# Better Indenting - - - - - - - - - - - - - - - - - - - - - - - - - - - -
++ (map
(c: {
mode = [ "v" ];
key = c;
action = "${c}gv";
})
[
"<"
">"
]
)
# Commenting - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
++ (mapAttrsToList
(k: d: {
mode = [ "n" ];
key = "gc${k}";
action = "${k}<esc>Vcx<esc><cmd>normal gcc<cr>fxa<bs>";
options = {
desc = "Add Comment ${d}";
};
})
{
o = "Below";
O = "Above";
}
)
# New File - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
++ [
{
mode = [ "n" ];
key = "<leader>fn";
action = "<cmd>enew<cr>";
options = {
desc = "New File";
};
}
]
# Locations/Quickfixes - - - - - - - - - - - - - - - - - - - - - - - - - - -
++ [
{
mode = [ "n" ];
key = "<leader>xl";
action = "<cmd>lopen<cr>";
options = {
desc = "Location List";
};
}
{
mode = [ "n" ];
key = "<leader>xq";
action = "<cmd>copen<cr>";
options = {
desc = "Quickfix list";
};
}
]
++ (mapAttrsToList
(
k: d:
let
cmd = substring 0 4 (toLower d);
in
{
mode = [ "n" ];
key = "${k}q";
action = "<cmd>c${cmd}<cr>";
options = {
desc = "${d} Quickfix";
};
}
)
{
"[" = "Previous";
"]" = "Next";
}
)
# Diagnostics - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
++ [
{
mode = [ "n" ];
key = "<leader>cd";
action.__raw = # lua
"function() vim.diagnostic.open_float() end";
options = {
desc = "Line Diagnostics";
};
}
]
++ (mapCartesianProduct
(
{ dir, key }:
let
sevs = {
d = {
key = "nil";
desc = "Diagnostic";
};
e = {
key = "vim.diagnostic.severity.ERROR";
desc = "Error";
};
w = {
key = "vim.diagnostic.severity.WARN";
desc = "Warning";
};
};
cmd = toLower dir;
sev = sevs."${key}".key;
in
{
mode = [ "n" ];
key = "${if (dir == "Next") then "]" else "["}${key}";
action.__raw = # lua
"function() vim.diagnostic.goto_${cmd}({ severity = ${sev} }) end";
options = {
desc = "${dir} ${sevs."${key}".desc}";
};
}
)
{
dir = [
"Next"
"Prev"
];
key = [
"d"
"e"
"w"
];
}
)
# Quit - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
++ [
{
mode = [ "n" ];
key = "<leader>qq";
action = "<cmd>qa<cr>";
options = {
desc = "Quit All";
};
}
]
# Inspect - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
++ [
{
mode = [ "n" ];
key = "<leader>ui";
action.__raw = # lua
"vim.show_pos";
options = {
desc = "Inspect Position";
};
}
]
# Window Management - - - - - - - - - - - - - - - - - - - - - - - - - - - -
++ [
{
mode = [ "n" ];
key = "<leader>w";
action = "<C-w>";
options = {
desc = "Windows";
remap = true;
};
}
{
mode = [ "n" ];
key = "<leader>-";
action = "<C-w>s";
options = {
desc = "Split Window Below";
remap = true;
};
}
{
mode = [ "n" ];
key = "<leader>|";
action = "<C-w>v";
options = {
desc = "Split Window Right";
remap = true;
};
}
{
mode = [ "n" ];
key = "<leader>wd";
action = "<C-w>c";
options = {
desc = "Delete Window";
remap = true;
};
}
]
# Tab Management - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
++ (mapAttrsToList
(k: a: {
mode = [ "n" ];
key = "<leader><tab>${k}";
action = "<cmd>tab${toLower a}<cr>";
options = {
desc = if (a == "Only") then "Close Other Tabs" else "${a} Tab";
};
})
{
l = "Last";
o = "Only";
f = "First";
"<Tab>" = "New";
"]" = "Next";
d = "Close";
"[" = "Previous";
}
);
}