-- Custom keymaps require("utils.functions") require("custom.uni") -- TODO: move this to a config file local season = "S3" local links = require("utils.linker") local user = vim.fn.system('whoami'):gsub('\n', '') local function open_cal() local current_date = os.date("%Y-%m-%d") local week_number = os.date("%V") local day_of_week = os.date("%a") local path = "~/synced/brainstore/calendar/calendar_" .. os.date("%Y") .. ".txt" local keys = ":e " .. path .. "/" .. current_date .. " w" .. tonumber(week_number) .. " " .. day_of_week .. "$" vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(keys, true, false, true), 'n', true) end ------------------------------------------------------- --------------------- KEYMAPS ------------------------- ------------------------------------------------------- -- Fast quitter vim.keymap.set('n', 'q', function() pcall(function() vim.cmd('wa') end) vim.cmd('qa!') end) -- Fast window switch vim.keymap.set("n", "w", "w") --------------------- NORMAL ------------------------- vim.keymap.set('n', 'zq', ':e ~/synced/brainstore/zettelkasten/input.txt`.zz') -- Buildin vim vim.keymap.set("n", "me", ":mes") vim.keymap.set("n", "snt", "set nu") vim.keymap.set("n", "snf", "set nonu") vim.keymap.set("n", "nv", function() select_course_directory() end, { desc = "Open UniCourse menu" }) vim.keymap.set("n", "n", "nzz", { silent = true }) vim.keymap.set("n", "N", "Nzz", { silent = true }) vim.keymap.set("n", "", "zz", { silent = true }) vim.keymap.set("n", "", "zz", { silent = true }) vim.keymap.set("n", "", "zz", { silent = true }) vim.keymap.set("n", "", "zz", { silent = true }) vim.keymap.set('n', 'a', 'm9ggVG"+y`9') vim.keymap.set('n', 'va', 'ggVG') -- Get a ready to use terminal vim.keymap.set('n', 'tr', ':tabnew:termi') -- This needs to be refined for quick access to a new file or a recently edited one vim.keymap.set('n', 'ov', function() local uni_dir = vim.fn.expand("~/projects/university/" .. season) require('telescope.builtin').find_files { prompt_title = "Select Vorlesung in " .. season, cwd = uni_dir, find_command = { "eza", "-1", "-D" }, } end ) -- new quick note file -- TODO: make this smarter vim.keymap.set("n", "nn", ":e ~/synced/brainstore/zettelkasten/quick", { silent = true }) vim.keymap.set("n", "r", function() local current_file = vim.fn.expand('%:p:h') -- get directory of current file local cmd = 'git -C ' .. vim.fn.fnameescape(current_file) .. ' status' vim.fn.system(cmd) if vim.v.shell_error == 0 then local git_root = vim.fn.systemlist('git -C ' .. vim.fn.fnameescape(current_file) .. ' rev-parse --show-toplevel') [1] vim.cmd('cd ' .. vim.fn.fnameescape(git_root)) else vim.cmd('cd ' .. vim.fn.fnameescape(current_file)) end end ) -- Quickly open some buffers vim.keymap.set('n', 'occ', ':e ~/.config/nvim/init.lua`.zz') vim.keymap.set('n', 'oct', ':e ~/synced/vault/contacts/contacts.txt`.zz') vim.keymap.set('n', 'ock', ':e ~/.config/nvim/lua/config/keymaps.lua`.zz') vim.keymap.set('n', 'ocd', ':e ~/.config/nvim/lua/config/autocmds.lua`.zz') vim.keymap.set('n', 'oco', ':e ~/.config/nvim/lua/config/options.lua`.zz') vim.keymap.set('n', 'ocl', ':e ~/.config/nvim/lua/config/lazy.lua`.zz') vim.keymap.set('n', 'oczl', ':e ~/.config/nvim/lua/config/lsp.lua`.zz') vim.keymap.set('n', 'ocp', ':e ~/.config/nvim/lua/plugins/misc.lua`.zz') vim.keymap.set('n', 'ocf', ':e ~/.config/nvim/lua/utils/functions.lua`.zz') vim.keymap.set('n', 'oca', ':e ~/.config/nvim/lua/utils/after.lua`.zz') vim.keymap.set('n', 'oq', ':e ~/synced/brainstore/input.txt`.zz') vim.keymap.set('n', 'ohh', ':e ~/configuration/nixos/users/' .. user .. '/home.nix`.zz') vim.keymap.set('n', 'op', ':e ~/configuration/nixos/users/' .. user .. '/packages.nix`.zz') vim.keymap.set('n', 'on', ':e ~/configuration/nixos/configuration.nix`.zz') vim.keymap.set('n', 'om', ':e ~/configuration/nixos/modules') vim.keymap.set('n', 'ow', ':e ~/synced/brainstore/waste.txt') vim.keymap.set('n', 'oho', ':e ~/configuration/nixos/hosts') vim.keymap.set('n', 'os', ':e ~/configuration/nixos/modules/server') vim.keymap.set('n', 'ot', ':e ~/synced/brainstore/todos/todo.txt`.zz') vim.keymap.set('n', 'od', ':e ~/synced/brainstore/todos/done.txt`.zz') vim.keymap.set('n', 'ou', ':e ~/projects/university/' .. season .. '/input.txt`.zz') vim.keymap.set('n', 'oz', ':e ~/.zshrc`.zz') vim.keymap.set('n', 'oaa', ':e ~/.common_shell`.zz') vim.keymap.set("n", "or", function() local last_file_path = vim.fn.stdpath('data') .. "/lastfile.txt" local file = io.open(last_file_path, "r") if file then local last_file = file:read("*line") file:close() if last_file and vim.fn.filereadable(last_file) == 1 then vim.cmd("edit " .. last_file) pcall(function() vim.cmd('normal! `.') -- Go to the last edit position vim.cmd('normal! zz') -- Center the cursor on the screen end) else print("Last file does not exist or is not readable") end else print("No last file found") end end , { noremap = true, silent = true }) vim.keymap.set('n', 'ok', open_cal) ------------------------------------------------------------------------------------- vim.keymap.set("n", "lf", links.insert_brainstore_link, { desc = "Link Brainstore file" }) vim.keymap.set("n", "lm", links.insert_mail_link, { desc = "Link Mail" }) vim.keymap.set('n', 'll', ':Lazy') vim.keymap.set("n", "lp", links.insert_project_link, { desc = "Link Project" }) vim.keymap.set("n", "lc", links.insert_contact_link, { desc = "Link Contact" }) vim.keymap.set("n", "ld", links.insert_date_link, { desc = "Link Contact" }) -- Indent all will be replaced by the formatting of lsp where the lsp is installed vim.keymap.set('n', 'ia', 'gg=Gzz') vim.keymap.set('n', 'ya', 'ggVG"+y') vim.keymap.set('n', 'ss', ':wa') vim.keymap.set('n', 'sw', function() local word = vim.fn.expand("") local replacement = vim.fn.input("Replace '" .. word .. "' with: ") if replacement ~= "" then vim.cmd(string.format("%%s/\\<%s\\>/%s/gI", vim.fn.escape(word, '\\/'), vim.fn.escape(replacement, '\\/'))) end end, { desc = "Substitute word under cursor (prompt)" }) -- Substitution in visual mode vim.keymap.set('v', 'sv', function() -- Save the current selection local save_reg = vim.fn.getreg('"') local save_regtype = vim.fn.getregtype('"') -- Yank the visual selection into the " register vim.cmd('normal! ""y') local selection = vim.fn.getreg('"') -- Escape magic characters for the search selection = vim.fn.escape(selection, '\\/.*$^~[]') -- Prompt for the replacement text local replacement = vim.fn.input("Replace '" .. selection .. "' with: ") if replacement ~= "" then vim.cmd(string.format("%%s/%s/%s/gI", selection, replacement)) end -- Restore previous register vim.fn.setreg('"', save_reg, save_regtype) end, { desc = "Substitute selection in file" }) vim.keymap.set('n', 'pp', function() vim.api.nvim_command('normal! "+p') end, { desc = 'Paste from system clipboard' }) vim.keymap.set('v', 'p', function() vim.cmd('normal! "+p') end, { desc = 'Yank to clipboard and keep the selection' }) ------------------------ VISUAL ------------------ vim.keymap.set('v', 'p', function() local unnamed_content = vim.fn.getreg('""') vim.api.nvim_command('normal! p') vim.fn.setreg('""', unnamed_content) end, { desc = 'Paste from unnamed register (don\'t overwrite it) in visual mode' }) vim.keymap.set('v', 'y', function() vim.cmd('normal! "+y') vim.cmd('normal! gv') end, { desc = 'Yank to clipboard and keep the selection' }) ---------------------------- INSERT --------------------------- vim.keymap.set('i', '', function() local col = vim.fn.col('.') local line = vim.fn.line('.') local line_len = vim.fn.col('$') - 1 if col <= line_len then vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('', true, false, true), 'n', true) else if line < vim.fn.line('$') then vim.cmd('normal! j^') end end end) -- Move left with wrapping vim.keymap.set('i', '', function() local col = vim.fn.col('.') local line = vim.fn.line('.') if col > vim.fn.indent(line) + 1 then -- not at very beginning (after indent), move left vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('', true, false, true), 'n', true) else if line > 1 then vim.cmd('normal! k$') vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('', true, false, true), 'n', true) end end end, { noremap = true, silent = true })