Files
nvim/lua/nami/remap.lua
T

165 lines
6.3 KiB
Lua

-- 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", "<nop>", { desc = "Do not quit" }) -- Do not quit :D
vim.keymap.set("i", "<C-c>", "<Esc>") -- Exit insert mode with C-c
vim.keymap.set("n", "<C-f>", "<cmd>silent !tmux display-popup -E \"tmuxss -i\"<CR>") -- open tmuxss switcher
vim.keymap.set("n", "-", "<CMD>Oil<CR>", { desc = "Open parent directory" }) -- open file viewer
vim.keymap.set("n", "<C-q>", "<cmd>copen<CR>"); -- Open Quickfixlist
vim.keymap.set("t", "<Esc>", "<C-\\><C-n>") -- use Esc to escape terminal
-- Buffer
vim.keymap.set("n", "<C-u>", "<C-u>zz") -- Move up a page while keeping the cursor centered
vim.keymap.set("n", "<C-d>", "<C-d>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", "<C-A-k>", "<cmd>cprev<CR>zz") -- Previous Quickfixlist entry
vim.keymap.set("n", "<C-A-j>", "<cmd>cnext<CR>zz") -- Next Quickfixlist entry
-- Tabs
vim.keymap.set("n", "<C-M>", "<cmd>tabnext +<CR>") -- Next tab
vim.keymap.set("n", "<C-N>", "<cmd>tabnext -<CR>") -- Previous Tab
vim.keymap.set("n", "<C-t>", "<cmd>tabnew<CR>") -- Create new Tab
vim.keymap.set("n", "<C-w><C-t>", "<cmd>tabclose<CR>") -- Close the current Tab
-- Disable mouse
-- fuck this thing
for _, mode in ipairs({ "n", "v", "i" }) do
vim.keymap.set(mode, "<RightMouse>", 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 '>+1<CR>gv=gv") -- Move selected text up one line
vim.keymap.set("v", "K", ":m '<-2<CR>gv=gv") -- Move selected text down one line
vim.keymap.set("n", "J", "mzJ`z") -- Remove newline character on current line
vim.keymap.set("n", "<leader>f", function() -- Format buffer
vim.lsp.buf.format()
end)
-- Goated: Edit the word under the cursor using a substitute
vim.keymap.set("n", "<leader>s", ":%s/\\<<C-r><C-w>\\>/<C-r><C-w>/gI<Left><Left><Left>")
-- Yanking and Pasting
-- Yank to + register (system clipboard)
vim.keymap.set("n", "<leader>y", "\"+y")
vim.keymap.set("v", "<leader>y", "\"+y")
vim.keymap.set("n", "<leader>Y", "\"+Y")
-- Delete to void register (no clipboard)
vim.keymap.set("v", "<leader>d", "\"_d")
vim.keymap.set("n", "<leader>d", "\"_d")
vim.keymap.set("n", "<leader>c", "\"_c")
-- currently conflicting with searches
vim.keymap.set("x", "<leader>P", "\"_dP") -- Paste before without moving selection into the yank register
vim.keymap.set("x", "<leader>p", "\"_dp") -- Paste after without moving selection into the yank register
------------
-- Macros --
------------
-- format a markdown table inside visual selection
vim.keymap.set("v", "<leader>mt", ":!column -t -s '|' -o '|'<CR>", { desc = "Format markdown table" })
-- Scripting
vim.keymap.set("n", "<leader>x", "<cmd>!chmod +x %<CR>", { silent = true }) -- Make the current file executable
-- Lua editing
vim.keymap.set("n", "<space><space>l", "<cmd>source %<CR>") -- source the current buffer
vim.keymap.set("n", "<space>l", ":.lua<CR>") -- source the current line
vim.keymap.set("v", "<space>l", ":lua<CR>") -- 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 '>+1<CR>gv=gv:lua SelectionStart()<CR>gv=gvo0o$<C-v>d\"_ddkA <Esc>p0")
-- How it works:
-- :m '>+1<CR>gv=gv:lua SelectionStart()<CR>gv=gvo0o$<C-v>d\"_ddkA <Esc>p0
-- ^
-- move selection down one line
-- :m '>+1<CR>gv=gv:lua SelectionStart()<CR>gv=gvo0o$<C-v>d\"_ddkA <Esc>p0
-- ^
-- put cursor at start of line selection
-- :m '>+1<CR>gv=gv:lua SelectionStart()<CR>gv=gvo0o$<C-v>d\"_ddkA <Esc>p0
-- ^
-- go back to visual mode
-- anchor selection to bottom left and top right
-- go to visual block mode
-- :m '>+1<CR>gv=gv:lua SelectionStart()<CR>gv=gvo0o$<C-v>d\"_ddkA <Esc>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", "<leader>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", "<leader>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" })