marleyvim/modules/nixvim/keymaps/default.nix

250 lines
6.6 KiB
Nix

{
lib,
helpers,
...
}: let
inherit
(lib)
map
mapAttrsToList
toUpper
substring
mapCartesianProduct
toLower
;
inherit (lib.marleyvim) keys;
inherit (helpers) mkRaw;
in {
globals = {
mapleader = " ";
maplocalleader = "\\";
};
keymaps =
#
# Disable Arrow Key Movement - - - - - - - - - - - - - - - - - - - - - - - -
(map (d: keys.mk' ["i" "n"] d "<NOP>") ["<DOWN>" "<UP>" "<LEFT>" "<RIGHT>"])
#
# Better Up/Down - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
++ (
mapAttrsToList
(d: k:
keys.mkWithOpts ["n" "x"] k "v:count == 0 ? 'g${k}' : '${k}'" d
{
expr = true;
silent = true;
})
{
Down = "j";
Up = "k";
}
)
#
# Resize Windows - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
++ (
mapAttrsToList
(
k: d: let
s =
if (d == "Increase")
then "+"
else "-";
in
keys.mk ["n"] "<C-${k}>" "<CMD>resize ${s}4<CR>" "${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
keys.mk ["n"] k "<CMD>b${d}<CR>" "${prettyDir} Buffer"
)
{
"<S-H>" = "previous";
"<S-L>" = "next";
"[b" = "previous";
"]b" = "next";
}
)
++ [(keys.mk ["n"] "<LEADER>bD" "<CMD>bd<CR>" "Delete Buffer and Window")]
#
# Clear Search/Diff Update/Redraw - - - - - - - - - - - - - - - - - - - - -
++ [
(keys.mk ["i" "n"] "<ESC>" "<CMD>noh<CR><ESC>" "Escape and Clear hlsearch")
(
keys.mk ["n"] "<LEADER>ur"
"<CMD>nohlsearch<BAR>diffupdate<BAR>normal! <C-l><CR>"
"Redraw / Clear hlsearch / Diff Update"
)
]
#
# Better n & N - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
++ (
mapCartesianProduct (
{
m,
d,
}: let
k =
if (d == "Next")
then "n"
else "N";
nn =
if (d == "Next")
then "Nn"
else "nN";
zv =
if (m == "n")
then ".'zv'"
else "";
in
keys.mkWithOpts [m] k "'${nn}'[v:searchforward]${zv}"
"${d} Search Result" {expr = true;}
)
{
m = ["n" "x" "o"];
d = ["Next" "Prev"];
}
)
#
# Undo Break-Points - - - - - - - - - - - - - - - - - - - - - - - - - - - -
++ (map (c: keys.mk' ["i"] c "${c}<C-g>u") ["," "." ";"])
#
# Search Docs (keywordprog) - - - - - - - - - - - - - - - - - - - - - - - -
# https://til.codeinthehole.com/posts/about-how-to-use-keywordprg-effectively/
++ [(keys.mk ["n"] "<LEADER>K" "<CMD>norm! K<CR>" "Search <KEYWORDPROG> for word")]
#
# Better Indenting - - - - - - - - - - - - - - - - - - - - - - - - - - - -
++ (map (c: keys.mk' ["v"] c "${c}gv") ["<" ">"])
#
# Commenting - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
++ (
mapAttrsToList (
k: d:
keys.mk ["n"] "gc${k}" "${k}<ESC>Vcx<ESC><CMD>normal gcc<CR>fxa<BS>"
"Add Comment ${d}"
)
{
o = "Below";
O = "Above";
}
)
#
# New File - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
++ [(keys.mk ["n"] "<LEADER>fn" "<CMD>enew<CR>" "New File")]
#
# Locations/Quickfixes - - - - - - - - - - - - - - - - - - - - - - - - - - -
++ [
(keys.mk ["n"] "<LEADER>xl" "<CMD>lopen<CR>" "Location List")
(keys.mk ["n"] "<LEADER>xq" "<CMD>copen<CR>" "Quickfix list")
]
++ (
mapAttrsToList (
k: d: let
cmd = substring 0 4 (toLower d);
in
keys.mk ["n"] "${k}q" "<CMD>c${cmd}<CR>" "${d} Quickfix"
)
{
"[" = "Previous";
"]" = "Next";
}
)
#
# Diagnostics - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
++ [
(keys.mk ["n"] "<LEADER>cd" (
mkRaw ''
function() vim.diagnostic.open_float() end
''
) "Line Diagnostics")
]
++ (
mapCartesianProduct (
{
dir,
key,
}: let
kmap = "${
if (dir == "Next")
then "]"
else "["
}${key}";
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
keys.mk ["n"] kmap (
mkRaw ''
function() vim.diagnostic.goto_${cmd}({ severity = ${sev} }) end
''
) "${dir} ${sevs."${key}".desc}"
)
{
dir = ["Next" "Prev"];
key = ["d" "e" "w"];
}
)
#
# Quit - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
++ [(keys.mk ["n"] "<LEADER>qq" "<CMD>qa<CR>" "Quit All")]
#
# Inspect - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
++ [(keys.mk ["n"] "<LEADER>ui" (mkRaw "vim.show_pos") "Inspect Position")]
#
# Window Management - - - - - - - - - - - - - - - - - - - - - - - - - - - -
++ [
(keys.mkWithOpts ["n"] "<LEADER>w" "<C-w>" "Windows" {remap = true;})
(keys.mkWithOpts ["n"] "<LEADER>-" "<C-w>s" "Split Window Below" {remap = true;})
(keys.mkWithOpts ["n"] "<LEADER>|" "<C-w>v" "Split Window Right" {remap = true;})
(keys.mkWithOpts ["n"] "<LEADER>wd" "<C-w>c" "Delete Window" {remap = true;})
]
#
# Tab Management - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
++ (
mapAttrsToList (
k: a: let
desc =
if (a == "Only")
then "Close Other Tabs"
else "${a} Tab";
in
keys.mk ["n"] "<LEADER><TAB>${k}" "<CMD>tab${toLower a}<CR>" desc
)
{
l = "Last";
o = "Only";
f = "First";
"<TAB>" = "New";
"]" = "Next";
d = "Close";
"[" = "Previous";
}
);
}