-- vim.g.mapleader = " " -- This has to be configured inside lazy.lua due to telescope loading before this takes effect ---------------- -- Navigation -- ---------------- vim.cmd("command! W w") -- I'm missing w for W too much vim.keymap.set("n", "Q", "", { desc = "Do not quit" }) -- Do not quit :D vim.keymap.set("i", "", "") -- Exit insert mode with C-c vim.keymap.set("n", "", "silent !tmux display-popup -E \"tmuxss -i\"") -- open tmuxss switcher vim.keymap.set("n", "-", "Oil", { desc = "Open parent directory" }) -- open file viewer vim.keymap.set("n", "", "copen"); -- Open Quickfixlist vim.keymap.set("t", "", "") -- use Esc to escape terminal -- Buffer vim.keymap.set("n", "", "zz") -- Move up a page while keeping the cursor centered vim.keymap.set("n", "", "zz") -- Move down a page while keeping the cursor centered vim.keymap.set("n", "n", "nzzzv") -- Jump to next occurance and center cursor vim.keymap.set("n", "N", "Nzzzv") -- Jump to previous occurance and center cursor -- Quickfixlist vim.keymap.set("n", "", "cprevzz") -- Previous Quickfixlist entry vim.keymap.set("n", "", "cnextzz") -- Next Quickfixlist entry -- Tabs vim.keymap.set("n", "", "tabnext +") -- Next tab vim.keymap.set("n", "", "tabnext -") -- Previous Tab vim.keymap.set("n", "", "tabnew") -- Create new Tab vim.keymap.set("n", "", "tabclose") -- Close the current Tab -- Disable mouse -- fuck this thing for _, mode in ipairs({ "n", "v", "i" }) do vim.keymap.set(mode, "", function() end) vim.keymap.set(mode, "<2-RightMouse>", function() end) vim.keymap.set(mode, "<3-RightMouse>", function() end) vim.keymap.set(mode, "<4-RightMouse>", function() end) end ------------------ -- Text editing -- ------------------ vim.keymap.set("v", "J", ":m '>+1gv=gv") -- Move selected text up one line vim.keymap.set("v", "K", ":m '<-2gv=gv") -- Move selected text down one line vim.keymap.set("n", "J", "mzJ`z") -- Remove newline character on current line vim.keymap.set("n", "f", function() -- Format buffer vim.lsp.buf.format() end) -- Goated: Edit the word under the cursor using a substitute vim.keymap.set("n", "s", ":%s/\\<\\>//gI") -- Yanking and Pasting -- Yank to + register (system clipboard) vim.keymap.set("n", "y", "\"+y") vim.keymap.set("v", "y", "\"+y") vim.keymap.set("n", "Y", "\"+Y") -- Delete to void register (no clipboard) vim.keymap.set("v", "d", "\"_d") vim.keymap.set("n", "d", "\"_d") vim.keymap.set("n", "c", "\"_c") -- currently conflicting with searches vim.keymap.set("x", "P", "\"_dP") -- Paste before without moving selection into the yank register vim.keymap.set("x", "p", "\"_dp") -- Paste after without moving selection into the yank register ------------ -- Macros -- ------------ -- Create a header in this form: -- ######## -- # Text # -- ######## -- Use it like so: -- Text -- # (Border line, place your cursor here -- Run Macro vim.keymap.set("n", "h", "0\"xy$k\"xPa A \"xpyyP:s;.;x;gj$k$olkdyyjpj\"_dd") -- How it works: -- 0\"xy$k\"xPa A \"xpyyP:s;.;x;gj$k$olkdyyjpj\"_dd -- ^ -- - yank text on line into register x -- - paste it before and after the line aboth -- 0\"xy$k\"xPa A \"xpyyP:s;.;x;gj$k$olkdyyjpj\"_dd -- ^ -- - yank the text line and paste it aboth -- - substitute every character with the filler text: :s;.;x:g ( = read register) -- 0\"xy$k\"xPa A \"xpyyP:s;.;x;gj$k$olkdyyjpj\"_dd -- ^ -- - with visual block make both line equal length -- - make a copy of the upper line and paste it below -- - delete the left over line -- format a markdown table inside visual selection vim.keymap.set("v", "mt", ":!column -t -s '|' -o '|'", { desc = "Format markdown table" }) -- Scripting vim.keymap.set("n", "x", "!chmod +x %", { silent = true }) -- Make the current file executable -- Lua editing vim.keymap.set("n", "l", "source %") -- source the current buffer vim.keymap.set("n", "l", ":.lua") -- source the current line vim.keymap.set("v", "l", ":lua") -- source the visual selection -- Places content on current line at the back of next line -- This: -- // returns -- return -- Turns into this: -- return // retuns vim.keymap.set("n", "L", "0d$jA pk\"_dd$") -- Does the same on a selection of lines -- This: -- // returns -- // something -- return -- Turns into this: -- return // returns -- // something vim.keymap.set("v", "L", ":m '>+1gv=gv:lua SelectionStart()gv=gvo0o$d\"_ddkA p0") -- How it works: -- :m '>+1gv=gv:lua SelectionStart()gv=gvo0o$d\"_ddkA p0 -- ^ -- move selection down one line -- :m '>+1gv=gv:lua SelectionStart()gv=gvo0o$d\"_ddkA p0 -- ^ -- put cursor at start of line selection -- :m '>+1gv=gv:lua SelectionStart()gv=gvo0o$d\"_ddkA p0 -- ^ -- go back to visual mode -- anchor selection to bottom left and top right -- go to visual block mode -- :m '>+1gv=gv:lua SelectionStart()gv=gvo0o$d\"_ddkA p0 -- ^ -- delete the block -- delete one line to void -- add block to the back of the line -- Moves the cursor to the start of the current selection function SelectionStart() local v = vim.fn.getpos("v") local c = vim.fn.getpos(".") if v[2] < c[2] then vim.api.nvim_input("o") end end -- Swap all occurances of two sequences with each --- @param start number line of chunk --- @param end_ number line of chunk function SwapWords(start, end_) local w1 = vim.fn.input("Word 1: ") local w2 = vim.fn.input("Word 2: ") if w1 == "" or w2 == "" then return end local lines = vim.api.nvim_buf_get_lines(0, start - 1, end_, false) for i, line in ipairs(lines) do line = line:gsub(vim.pesc(w1), "__TMP__") line = line:gsub(vim.pesc(w2), w1) line = line:gsub("__TMP__", w2) lines[i] = line end vim.api.nvim_buf_set_lines(0, start - 1, end_, false, lines) end -- Swap two words in current selection vim.keymap.set("v", "ws", function() local start = vim.fn.getpos("v")[2] local end_ = vim.fn.getpos(".")[2] SwapWords(math.min(start, end_), math.max(start, end_)) end, { desc = "Swap two words in selection" }) -- Swap two words in previous selection vim.keymap.set("n", "ws", function() local start = vim.fn.getpos("'<")[2] local end_ = vim.fn.getpos("'>")[2] SwapWords(math.min(start, end_), math.max(start, end_)) end, { desc = "Swap two words in previous selection" })