{ config, lib, ... }: let inherit (config.myvim.mkKey) mkKeymap mkKeymapWithOpts wKeyObj; inherit (lib.nixvim) mkRaw; # general mappings v = [ (mkKeymap "v" "" ":w" "Saving File") (mkKeymap "v" "" "" "Escape") (mkKeymap "v" "" ":m '>+1gv-gv" "Move Selected Line Down") (mkKeymap "v" "" ":m '-2gv-gv" "Move Selected Line Up") (mkKeymap "v" "<" "" ">gv" "Indent in") (mkKeymap "v" "" "" "Mapped to Nothing") ]; xv = [ (mkKeymapWithOpts "x" "j" ''v:count || mode(1)[0:1] == "no" ? "j" : "gj"'' "Move down" { expr = true; }) (mkKeymapWithOpts "x" "k" ''v:count || mode(1)[0:1] == "no" ? "k" : "gk"'' "Move up" { expr = true; }) ]; insert = [ (mkKeymap "i" "jk" "" "Normal Mode") (mkKeymap "i" "" ":w ++p" "Save file") (mkKeymap "i" "" ":m .+1==gi" "Move Line Down") (mkKeymap "i" "" ":m .-2==gi" "Move Line Up") ]; normal = [ (mkKeymap "n" "" ":lua require('smart-splits').resize_left()" "Resize Left") (mkKeymap "n" "" ":lua require('smart-splits').resize_down()" "Resize Down") (mkKeymap "n" "" ":lua require('smart-splits').resize_up()" "Resize Up") (mkKeymap "n" "" ":lua require('smart-splits').resize_right()" "Resize Right") (mkKeymap "n" "?" ( # lua mkRaw '' function() require('flash').jump({ forward = true, wrap = true, multi_window = true }) end '' ) "Flash Search") (mkKeymap "n" "" ":lua require('smart-splits').move_cursor_left()" "Move Cursor Left") (mkKeymap "n" "" ":lua require('smart-splits').move_cursor_down()" "Move Cursor Down") (mkKeymap "n" "" ":lua require('smart-splits').move_cursor_up()" "Move Cursor Up") (mkKeymap "n" "" ":lua require('smart-splits').move_cursor_right()" "Move Cursor Right") (mkKeymap "n" "" ":lua require('smart-splits').move_cursor_previous()" "Move Cursor Previous" ) (mkKeymap "n" "dd" ( # lua mkRaw '' function() local any_diff = false for _, w in ipairs(vim.api.nvim_tabpage_list_wins(0)) do if vim.api.nvim_win_get_option(w, "diff") then any_diff = true break end end if any_diff then vim.cmd("windo diffoff") else vim.cmd("windo diffthis") end end '' ) "Toggle Diff the opened windows" ) (mkKeymap "n" "" "w ++p" "Save the file") (mkKeymap "n" "" "" "Increase Number") (mkKeymap "n" "" "" "Decrease Number") (mkKeymap "n" "" "m .+1==" "Move line Down") (mkKeymap "n" "" "m .-2==" "Move line up") (mkKeymap "n" "qq" "quitall!" "Quit!") (mkKeymap "n" "qw" ( # lua mkRaw '' function() local wins = vim.api.nvim_tabpage_list_wins(0) if #wins > 1 then local ok, err = pcall(vim.cmd, 'close') if not ok then vim.notify("Cannot close the last window!", vim.log.levels.WARN) end else vim.notify("Cannot close the last window!", vim.log.levels.WARN) end end '' ) "Close Window!") (mkKeymap "n" "" "nohl" "no highlight!") (mkKeymap "n" "" ":nohlsearch" "escape") (mkKeymap "n" "A" "ggVG" "select All") (mkKeymap "n" "|" "vsplit" "vertical split") (mkKeymap "n" "-" "split" "horizontal split") # quickfix (mkKeymap "n" "cn" "cnext" "quickfix next") (mkKeymap "n" "cp" "cprev" "quickfix prev") (mkKeymap "n" "cq" "cclose" "quit quickfix") (mkKeymap "n" "id" ( # lua mkRaw '' function() local date = "# " .. os.date("%d-%m-%y") local row, col = unpack(vim.api.nvim_win_get_cursor(0)) local line = vim.api.nvim_get_current_line() -- Insert date at cursor position local new_line = line:sub(1, col) .. date .. line:sub(col + 1) vim.api.nvim_set_current_line(new_line) -- Move cursor to next line vim.api.nvim_win_set_cursor(0, { row + 1, 0 }) end '' ) "Insert Date at cursor position") (mkKeymap "n" "n" "nzzzv" "Move to center") (mkKeymap "n" "N" "Nzzzv" "Moving to center") (mkKeymap "n" "uC" ( # lua mkRaw '' require('stay-centered').toggle '' ) "Toggle stay-centered.nvim") (mkKeymap "n" "ft" ( # lua mkRaw '' function() vim.ui.input({ prompt = "Enter FileType: " }, function(input) local ft = input if not input or input == "" then ft = vim.bo.filetype end vim.o.filetype = ft end) end '' ) "Set Filetype") (mkKeymap "n" "o" ( # lua mkRaw '' function() local word = vim.fn.expand("") -- Gets file-like word under cursor if word:match("^https?://") then -- Detect OS and choose browser opener local open_cmd if vim.fn.has("macunix") == 1 then open_cmd = "open" elseif vim.fn.has("unix") == 1 then open_cmd = "xdg-open" elseif vim.fn.has("win32") == 1 or vim.fn.has("win64") == 1 then open_cmd = "start" else print("Unsupported OS") return end vim.fn.jobstart({ open_cmd, word }, { detach = true }) elseif vim.fn.filereadable(word) == 1 or vim.fn.isdirectory(word) == 1 then -- It's a file or directory; open in current window vim.cmd("edit " .. vim.fn.fnameescape(word)) else print("Not a valid file or URL: " .. word) end end '' ) "Open") (mkKeymapWithOpts "n" "j" ''v:count || mode(1)[0:1] == "no" ? "j" : "gj"'' "Move down" { expr = true; }) (mkKeymapWithOpts "n" "k" ''v:count || mode(1)[0:1] == "no" ? "k" : "gk"'' "Move up" { expr = true; }) ]; in { keymaps = insert ++ normal ++ v ++ xv; wKeyList = [ (wKeyObj [ "A" "" "" "true" ]) (wKeyObj [ "" "" "" "true" ]) (wKeyObj [ "q" "" "quit/session" ]) (wKeyObj [ "i" "" "Insert" ]) (wKeyObj [ "v" "󰩬" "Insert" ]) (wKeyObj [ "z" "" "fold" ]) (wKeyObj [ "g" "" "goto" ]) (wKeyObj [ "[" "" "next" ]) (wKeyObj [ "]" "" "prev" ]) (wKeyObj [ "u" "󰔎" "ui" ]) (wKeyObj [ "o" "" "Open" ]) (wKeyObj [ "d" "" "diff" ]) (wKeyObj [ "|" "" "vsplit" ]) (wKeyObj [ "-" "" "split" ]) (wKeyObj [ "c" "󰁨" "quickfix" ]) ]; extraConfigLua = # lua '' -- Use black hole register for 'x', 'X', 'c', 'C' vim.api.nvim_set_keymap('n', 'x', '"_x', { noremap = true, silent = true }) vim.api.nvim_set_keymap('n', 'X', '"_X', { noremap = true, silent = true }) vim.api.nvim_set_keymap('n', 'c', '"_c', { noremap = true, silent = true }) vim.api.nvim_set_keymap('n', 'C', '"_C', { noremap = true, silent = true }) -- Visual mode vim.api.nvim_set_keymap('v', 'x', '"_d', { noremap = true, silent = true }) vim.api.nvim_set_keymap('v', 'X', '"_d', { noremap = true, silent = true }) vim.api.nvim_set_keymap('v', 'c', '"_c', { noremap = true, silent = true }) vim.api.nvim_set_keymap('v', 'C', '"_c', { noremap = true, silent = true }) -- In visual mode, paste from the clipboard without overwriting it vim.api.nvim_set_keymap("v", "p", '"_dP', { noremap = true, silent = true }) -- Only this hack works in command mode vim.cmd([[ cnoremap cnoremap ]]) ''; }