nvim/lua/config/mappings.lua
2022-10-10 21:52:06 +02:00

274 lines
8.8 KiB
Lua

---@diagnostic disable: missing-parameter
local wk = require("which-key")
local util = require("util")
vim.o.timeoutlen = 300
wk.setup({
show_help = false,
triggers = "auto",
plugins = { spelling = true },
key_labels = { ["<leader>"] = "SPC" },
})
-- Move to window using the <ctrl> movement keys
vim.keymap.set("n", "<left>", "<C-w>h")
vim.keymap.set("n", "<down>", "<C-w>j")
vim.keymap.set("n", "<up>", "<C-w>k")
vim.keymap.set("n", "<right>", "<C-w>l")
-- Resize window using <ctrl> arrow keys
vim.keymap.set("n", "<S-Up>", "<cmd>resize +2<CR>")
vim.keymap.set("n", "<S-Down>", "<cmd>resize -2<CR>")
vim.keymap.set("n", "<S-Left>", "<cmd>vertical resize -2<CR>")
vim.keymap.set("n", "<S-Right>", "<cmd>vertical resize +2<CR>")
-- Move Lines
vim.keymap.set("n", "<A-j>", ":m .+1<CR>==")
vim.keymap.set("v", "<A-j>", ":m '>+1<CR>gv=gv")
vim.keymap.set("i", "<A-j>", "<Esc>:m .+1<CR>==gi")
vim.keymap.set("n", "<A-k>", ":m .-2<CR>==")
vim.keymap.set("v", "<A-k>", ":m '<-2<CR>gv=gv")
vim.keymap.set("i", "<A-k>", "<Esc>:m .-2<CR>==gi")
-- Switch buffers with tab
vim.keymap.set("n", "<C-Left>", "<cmd>bprevious<cr>")
vim.keymap.set("n", "<C-Right>", "<cmd>bnext<cr>")
-- Easier pasting
vim.keymap.set("n", "[p", ":pu!<cr>")
vim.keymap.set("n", "]p", ":pu<cr>")
-- Clear search with <esc>
vim.keymap.set("", "<esc>", ":noh<esc>")
vim.keymap.set("n", "gw", "*N")
vim.keymap.set("x", "gw", "*N")
-- https://github.com/mhinz/vim-galore#saner-behavior-of-n-and-n
vim.keymap.set("n", "n", "'Nn'[v:searchforward]", { expr = true })
vim.keymap.set("x", "n", "'Nn'[v:searchforward]", { expr = true })
vim.keymap.set("o", "n", "'Nn'[v:searchforward]", { expr = true })
vim.keymap.set("n", "N", "'nN'[v:searchforward]", { expr = true })
vim.keymap.set("x", "N", "'nN'[v:searchforward]", { expr = true })
vim.keymap.set("o", "N", "'nN'[v:searchforward]", { expr = true })
-- Add undo break-points
vim.keymap.set("i", ",", ",<c-g>u")
vim.keymap.set("i", ".", ".<c-g>u")
vim.keymap.set("i", ";", ";<c-g>u")
-- save in insert mode
vim.keymap.set("i", "<C-s>", "<cmd>:w<cr><esc>")
vim.keymap.set("n", "<C-s>", "<cmd>:w<cr><esc>")
vim.keymap.set("n", "<C-c>", "<cmd>normal ciw<cr>a")
-- telescope <ctrl-r> in command line
-- vim.cmd([[cmap <C-R> <Plug>(TelescopeFuzzyCommandSearch)]])
-- better indenting
vim.keymap.set("v", "<", "<gv")
vim.keymap.set("v", ">", ">gv")
vim.keymap.set("n", "<space>cu", function()
local number = math.random(math.pow(2, 127) + 1, math.pow(2, 128))
return "i" .. string.format("%.0f", number)
end, {
expr = true,
desc = "GUID",
})
-- makes * and # work on visual mode too.
vim.cmd([[
function! g:VSetSearch(cmdtype)
let temp = @s
norm! gv"sy
let @/ = '\V' . substitute(escape(@s, a:cmdtype.'\'), '\n', '\\n', 'g')
let @s = temp
endfunction
xnoremap * :<C-u>call g:VSetSearch('/')<CR>/<C-R>=@/<CR><CR>
xnoremap # :<C-u>call g:VSetSearch('?')<CR>?<C-R>=@/<CR><CR>
]])
local leader = {
["w"] = {
name = "+windows",
["o"] = { "<C-W>p", "other-window" },
["d"] = { "<C-W>c", "delete-window" },
["2"] = { "<C-W>v", "layout-double-columns" },
["h"] = { "<C-W>h", "window-left" },
["j"] = { "<C-W>j", "window-below" },
["l"] = { "<C-W>l", "window-right" },
["k"] = { "<C-W>k", "window-up" },
["H"] = { "<C-W>5<", "expand-window-left" },
["J"] = { ":resize +5", "expand-window-below" },
["L"] = { "<C-W>5>", "expand-window-right" },
["K"] = { ":resize -5", "expand-window-up" },
["="] = { "<C-W>=", "balance-window" },
["s"] = { "<C-W>s", "split-window-below" },
["v"] = { "<C-W>v", "split-window-right" },
},
c = {
name = "+code",
},
b = {
name = "+buffer",
["b"] = { "<cmd>:e #<cr>", "Switch to Other Buffer" },
["p"] = { "<cmd>:BufferLineCyclePrev<CR>", "Previous Buffer" },
["["] = { "<cmd>:BufferLineCyclePrev<CR>", "Previous Buffer" },
["n"] = { "<cmd>:BufferLineCycleNext<CR>", "Next Buffer" },
["]"] = { "<cmd>:BufferLineCycleNext<CR>", "Next Buffer" },
["d"] = { "<cmd>:Bdelete<CR>", "Delete Buffer" },
["D"] = { "<cmd>:bd<CR>", "Delete Buffer & Window" },
},
g = {
name = "+git",
l = {
function()
require("util").float_terminal("lazygit")
end,
"LazyGit",
},
c = { "<Cmd>Telescope git_commits<CR>", "commits" },
b = { "<Cmd>Telescope git_branches<CR>", "branches" },
s = { "<Cmd>Telescope git_status<CR>", "status" },
d = { "<cmd>DiffviewOpen<cr>", "DiffView" },
h = { name = "+hunk" },
},
["h"] = {
name = "+help",
t = { "<cmd>:Telescope builtin<cr>", "Telescope" },
c = { "<cmd>:Telescope commands<cr>", "Commands" },
h = { "<cmd>:Telescope help_tags<cr>", "Help Pages" },
m = { "<cmd>:Telescope man_pages<cr>", "Man Pages" },
k = { "<cmd>:Telescope keymaps<cr>", "Key Maps" },
s = { "<cmd>:Telescope highlights<cr>", "Search Highlight Groups" },
l = { [[<cmd>TSHighlightCapturesUnderCursor<cr>]], "Highlight Groups at cursor" },
f = { "<cmd>:Telescope filetypes<cr>", "File Types" },
o = { "<cmd>:Telescope vim_options<cr>", "Options" },
a = { "<cmd>:Telescope autocommands<cr>", "Auto Commands" },
p = {
name = "+packer",
p = { "<cmd>PackerSync<cr>", "Sync" },
s = { "<cmd>PackerStatus<cr>", "Status" },
i = { "<cmd>PackerInstall<cr>", "Install" },
c = { "<cmd>PackerCompile<cr>", "Compile" },
},
},
s = {
name = "+search",
g = { "<cmd>Telescope live_grep<cr>", "Grep" },
b = { "<cmd>Telescope current_buffer_fuzzy_find<cr>", "Buffer" },
s = {
function()
require("telescope.builtin").lsp_document_symbols({
symbols = { "Class", "Function", "Method", "Constructor", "Interface", "Module", "Struct", "Trait" },
})
end,
"Goto Symbol",
},
h = { "<cmd>Telescope command_history<cr>", "Command History" },
m = { "<cmd>Telescope marks<cr>", "Jump to Mark" },
r = { "<cmd>lua require('spectre').open()<CR>", "Replace (Spectre)" },
},
f = {
name = "+file",
t = { "<cmd>Neotree toggle<cr>", "NeoTree" },
f = { "<cmd>Telescope find_files<cr>", "Find File" },
r = { "<cmd>Telescope oldfiles<cr>", "Open Recent File" },
n = { "<cmd>enew<cr>", "New File" },
z = "Zoxide",
d = "Dot Files",
},
o = {
name = "+open",
p = { "<cmd>MarkdownPreview<cr>", "Markdown Preview" },
g = { "<cmd>Glow<cr>", "Markdown Glow" },
n = { "<cmd>lua require('github-notifications.menu').notifications()<cr>", "GitHub Notifications" },
},
p = {
name = "+project",
p = "Open Project",
b = { ":Telescope file_browser cwd=~/projects<CR>", "Browse ~/projects" },
},
t = {
name = "toggle",
f = {
require("plugins.lsp.formatting").toggle,
"Format on Save",
},
s = {
function()
util.toggle("spell")
end,
"Spelling",
},
w = {
function()
util.toggle("wrap")
end,
"Word Wrap",
},
n = {
function()
util.toggle("relativenumber", true)
util.toggle("number")
end,
"Line Numbers",
},
},
["<tab>"] = {
name = "tabs",
["<tab>"] = { "<cmd>tabnew<CR>", "New Tab" },
n = { "<cmd>tabnext<CR>", "Next" },
d = { "<cmd>tabclose<CR>", "Close" },
p = { "<cmd>tabprevious<CR>", "Previous" },
["]"] = { "<cmd>tabnext<CR>", "Next" },
["["] = { "<cmd>tabprevious<CR>", "Previous" },
f = { "<cmd>tabfirst<CR>", "First" },
l = { "<cmd>tablast<CR>", "Last" },
},
["`"] = { "<cmd>:e #<cr>", "Switch to Other Buffer" },
[" "] = "Find File",
["."] = { ":Telescope file_browser<CR>", "Browse Files" },
[","] = { "<cmd>Telescope buffers show_all_buffers=true<cr>", "Switch Buffer" },
["/"] = { "<cmd>Telescope live_grep<cr>", "Search" },
[":"] = { "<cmd>Telescope command_history<cr>", "Command History" },
["C"] = {
function()
util.clipman()
end,
"Paste from Clipman",
},
["P"] = {
":Telescope neoclip plus<CR>",
"Paste from Clipman",
},
q = {
name = "+quit/session",
q = { "<cmd>qa<cr>", "Quit" },
["!"] = { "<cmd>:qa!<cr>", "Quit without saving" },
s = { [[<cmd>lua require("persistence").load()<cr>]], "Restore Session" },
l = { [[<cmd>lua require("persistence").load({last=true})<cr>]], "Restore Last Session" },
d = { [[<cmd>lua require("persistence").stop()<cr>]], "Stop Current Session" },
},
x = {
name = "+errors",
x = { "<cmd>TroubleToggle workspace_diagnostics<cr>", "Trouble" },
t = { "<cmd>TodoTrouble<cr>", "Todo Trouble" },
T = { "<cmd>TodoTelescope<cr>", "Todo Telescope" },
l = { "<cmd>lopen<cr>", "Open Location List" },
q = { "<cmd>copen<cr>", "Open Quickfix List" },
},
z = { [[<cmd>ZenMode<cr>]], "Zen Mode" },
T = { [[<Plug>PlenaryTestFile]], "Plenary Test" },
}
for i = 0, 10 do
leader[tostring(i)] = "which_key_ignore"
end
wk.register(leader, { prefix = "<leader>" })
wk.register({ g = { name = "+goto" } })