nvim keymap boilerplate function

This commit is contained in:
Noah Masur 2021-11-13 16:29:09 -05:00
parent 1c95039847
commit 5f794c626b

View File

@ -444,98 +444,105 @@ vim.api.nvim_exec([[
-- Key Mapping -- Key Mapping
-- =========================================================================== -- ===========================================================================
-- Function to cut down config boilerplate
local key = function(mode, key_sequence, action, params)
params = params or {}
params["noremap"] = true
vim.api.nvim_set_keymap(mode, key_sequence, action, params)
end
-- Remap space as leader key -- Remap space as leader key
vim.api.nvim_set_keymap("", "<Space>", "<Nop>", {noremap=true, silent=true}) key("", "<Space>", "<Nop>", {silent=true})
vim.g.mapleader = " " vim.g.mapleader = " "
vim.g.maplocalleader = " " vim.g.maplocalleader = " "
-- Keep selection when changing indentation -- Keep selection when changing indentation
vim.api.nvim_set_keymap("v", "<", "<gv", {noremap=true}) key("v", "<", "<gv")
vim.api.nvim_set_keymap("v", ">", ">gv", {noremap=true}) key("v", ">", ">gv")
-- Unset search pattern register -- Unset search pattern register
vim.api.nvim_set_keymap("n", "<CR>", ":noh<CR><CR>", {noremap=true, silent=true}) key("n", "<CR>", ":noh<CR><CR>", {silent=true})
-- Shuffle lines around -- Shuffle lines around
vim.api.nvim_set_keymap("n", "<A-j>", ":m .+1<CR>==", {noremap=true}) key("n", "<A-j>", ":m .+1<CR>==")
vim.api.nvim_set_keymap("n", "<A-k>", ":m .-2<CR>==", {noremap=true}) key("n", "<A-k>", ":m .-2<CR>==")
vim.api.nvim_set_keymap("i", "<A-j>", "<Esc>:m .+1<CR>==gi", {noremap=true}) key("i", "<A-j>", "<Esc>:m .+1<CR>==gi")
vim.api.nvim_set_keymap("i", "<A-k>", "<Esc>:m .-2<CR>==gi", {noremap=true}) key("i", "<A-k>", "<Esc>:m .-2<CR>==gi")
vim.api.nvim_set_keymap("v", "<A-j>", ":m '>+1<CR>gv=gv", {noremap=true}) key("v", "<A-j>", ":m '>+1<CR>gv=gv")
vim.api.nvim_set_keymap("v", "<A-k>", ":m '<-2<CR>gv=gv", {noremap=true}) key("v", "<A-k>", ":m '<-2<CR>gv=gv")
-- Telescope (fuzzy finder) -- Telescope (fuzzy finder)
vim.api.nvim_set_keymap("n", "<Leader>/", ":Telescope live_grep<CR>", {noremap=true}) key("n", "<Leader>/", ":Telescope live_grep<CR>")
vim.api.nvim_set_keymap("n", "<Leader>ff", ":Telescope find_files<CR>", {noremap=true}) key("n", "<Leader>ff", ":Telescope find_files<CR>")
vim.api.nvim_set_keymap("n", "<Leader>fa", ":Telescope file_browser<CR>", {noremap=true}) key("n", "<Leader>fa", ":Telescope file_browser<CR>")
vim.api.nvim_set_keymap("n", "<Leader>wt", ":Telescope tmux sessions<CR>", {noremap=true}) key("n", "<Leader>wt", ":Telescope tmux sessions<CR>")
vim.api.nvim_set_keymap("n", "<Leader>ww", ":Telescope tmux windows<CR>", {noremap=true}) key("n", "<Leader>ww", ":Telescope tmux windows<CR>")
vim.api.nvim_set_keymap("n", "<Leader>w/", ":Telescope tmux pane_contents<CR>", {noremap=true}) key("n", "<Leader>w/", ":Telescope tmux pane_contents<CR>")
vim.api.nvim_set_keymap("n", "<Leader>fz", ":Telescope zoxide list<CR>", {noremap=true}) key("n", "<Leader>fz", ":Telescope zoxide list<CR>")
vim.api.nvim_set_keymap("n", "<Leader>b", ":Telescope buffers<CR>", {noremap=true}) key("n", "<Leader>b", ":Telescope buffers<CR>")
vim.api.nvim_set_keymap("n", "<Leader>hh", ":Telescope help_tags<CR>", {noremap=true}) key("n", "<Leader>hh", ":Telescope help_tags<CR>")
vim.api.nvim_set_keymap("n", "<Leader>fr", ":Telescope oldfiles<CR>", {noremap=true}) key("n", "<Leader>fr", ":Telescope oldfiles<CR>")
vim.api.nvim_set_keymap("n", "<Leader>cc", ":Telescope commands<CR>", {noremap=true}) key("n", "<Leader>cc", ":Telescope commands<CR>")
vim.api.nvim_set_keymap("n", "<Leader>cr", ":Telescope command_history<CR>", {noremap=true}) key("n", "<Leader>cr", ":Telescope command_history<CR>")
vim.api.nvim_set_keymap("n", "<Leader>y", ":Telescope neoclip<CR>", {noremap=true}) key("n", "<Leader>y", ":Telescope neoclip<CR>")
vim.api.nvim_set_keymap("n", "<Leader>s", ":Telescope current_buffer_fuzzy_find<CR>", {noremap=true}) key("n", "<Leader>s", ":Telescope current_buffer_fuzzy_find<CR>")
vim.api.nvim_set_keymap("n", "<Leader>gc", ":Telescope git_commits<CR>", {noremap=true}) key("n", "<Leader>gc", ":Telescope git_commits<CR>")
vim.api.nvim_set_keymap("n", "<Leader>gf", ":Telescope git_bcommits<CR>", {noremap=true}) key("n", "<Leader>gf", ":Telescope git_bcommits<CR>")
vim.api.nvim_set_keymap("n", "<Leader>gb", ":Telescope git_branches<CR>", {noremap=true}) key("n", "<Leader>gb", ":Telescope git_branches<CR>")
vim.api.nvim_set_keymap("n", "<Leader>gs", ":Telescope git_status<CR>", {noremap=true}) key("n", "<Leader>gs", ":Telescope git_status<CR>")
-- LSP -- LSP
vim.api.nvim_set_keymap("n", "gd", "<Cmd>lua vim.lsp.buf.definition()<CR>", {silent=true, noremap=true}) key("n", "gd", "<Cmd>lua vim.lsp.buf.definition()<CR>", {silent=true})
vim.api.nvim_set_keymap("n", "gi", "<Cmd>lua vim.lsp.buf.implementation()<CR>", {silent=true, noremap=true}) key("n", "gi", "<Cmd>lua vim.lsp.buf.implementation()<CR>", {silent=true})
vim.api.nvim_set_keymap("n", "gh", "<Cmd>lua vim.lsp.buf.hover()<CR>", {silent=true, noremap=true}) key("n", "gh", "<Cmd>lua vim.lsp.buf.hover()<CR>", {silent=true})
vim.api.nvim_set_keymap("n", "]e", "<Cmd>lua vim.lsp.diagnostic.goto_next()<CR>", {silent=true, noremap=true}) key("n", "]e", "<Cmd>lua vim.lsp.diagnostic.goto_next()<CR>", {silent=true})
vim.api.nvim_set_keymap("n", "[e", "<Cmd>lua vim.lsp.diagnostic.goto_prev()<CR>", {silent=true, noremap=true}) key("n", "[e", "<Cmd>lua vim.lsp.diagnostic.goto_prev()<CR>", {silent=true})
vim.api.nvim_set_keymap("n", "<Leader>e", "<Cmd>lua vim.lsp.diagnostic.show_line_diagnostics()<CR>", {silent=true, noremap=true}) key("n", "<Leader>e", "<Cmd>lua vim.lsp.diagnostic.show_line_diagnostics()<CR>", {silent=true})
-- File commands -- File commands
vim.api.nvim_set_keymap("n", "<Leader>q", ":quit<CR>", {noremap=true}) key("n", "<Leader>q", ":quit<CR>")
vim.api.nvim_set_keymap("n", "<Leader>Q", ":quitall<CR>", {noremap=true}) key("n", "<Leader>Q", ":quitall<CR>")
vim.api.nvim_set_keymap("n", "<Leader>fs", ":write<CR>", {noremap=true}) key("n", "<Leader>fs", ":write<CR>")
vim.api.nvim_set_keymap("n", "<Leader>fe", ":!chmod 755 %<CR><CR>", {noremap=true}) key("n", "<Leader>fe", ":!chmod 755 %<CR><CR>")
vim.api.nvim_set_keymap("n", "<Leader>fn", ":!chmod 644 %<CR><CR>", {noremap=true}) key("n", "<Leader>fn", ":!chmod 644 %<CR><CR>")
vim.api.nvim_set_keymap("n", "<Leader>fd", ":lcd %:p:h<CR>", {silent=true, noremap=true}) key("n", "<Leader>fd", ":lcd %:p:h<CR>", {silent=true})
vim.api.nvim_set_keymap("n", "<Leader>fu", ":lcd ..<CR>", {silent=true, noremap=true}) key("n", "<Leader>fu", ":lcd ..<CR>", {silent=true})
vim.api.nvim_set_keymap("n", "<Leader><Tab>", ":b#<CR>", {silent=true, noremap=true}) key("n", "<Leader><Tab>", ":b#<CR>", {silent=true})
vim.api.nvim_set_keymap("n", "<Leader>gr", ":!gh repo view -w<CR><CR>", {silent=true, noremap=true}) key("n", "<Leader>gr", ":!gh repo view -w<CR><CR>", {silent=true})
vim.api.nvim_set_keymap("n", "<Leader>tt", key("n", "<Leader>tt",
[[<Cmd>exe 'edit ~/notes/journal/'.strftime("%Y-%m-%d_%a").'.md'<CR>]], {noremap=true} [[<Cmd>exe 'edit ~/notes/journal/'.strftime("%Y-%m-%d_%a").'.md'<CR>]]
) )
-- Window commands -- Window commands
vim.api.nvim_set_keymap("n", "<Leader>wv", ":vsplit<CR>", {noremap=true}) key("n", "<Leader>wv", ":vsplit<CR>")
vim.api.nvim_set_keymap("n", "<Leader>wh", ":split<CR>", {noremap=true}) key("n", "<Leader>wh", ":split<CR>")
vim.api.nvim_set_keymap("n", "<Leader>wm", ":only<CR>", {noremap=true}) key("n", "<Leader>wm", ":only<CR>")
-- Tabularize -- Tabularize
vim.api.nvim_set_keymap("", "<Leader>ta", ":Tabularize /", {noremap=true}) key("", "<Leader>ta", ":Tabularize /")
vim.api.nvim_set_keymap("", "<Leader>t#", ":Tabularize /#<CR>", {noremap=true}) key("", "<Leader>t#", ":Tabularize /#<CR>")
vim.api.nvim_set_keymap("", "<Leader>t\"", ":Tabularize /\"<CR>", {noremap=true}) key("", "<Leader>t\"", ":Tabularize /\"<CR>")
vim.api.nvim_set_keymap("", "<Leader>tl", ":Tabularize /--<CR>", {noremap=true}) key("", "<Leader>tl", ":Tabularize /--<CR>")
vim.api.nvim_set_keymap("", "<Leader>tL", ":Tabularize /---<CR>", {noremap=true}) key("", "<Leader>tL", ":Tabularize /---<CR>")
-- Vimrc editing -- Vimrc editing
vim.api.nvim_set_keymap("n", "<Leader>fv", ":edit $MYVIMRC<CR>", {noremap=true}) key("n", "<Leader>fv", ":edit $MYVIMRC<CR>")
vim.api.nvim_set_keymap("n", "<Leader>rr", ":luafile $MYVIMRC<CR>", {noremap=true}) key("n", "<Leader>rr", ":luafile $MYVIMRC<CR>")
vim.api.nvim_set_keymap("n", "<Leader>rp", ":luafile $MYVIMRC<CR>:PackerInstall<CR>:PackerCompile<CR>", {noremap=true}) key("n", "<Leader>rp", ":luafile $MYVIMRC<CR>:PackerInstall<CR>:PackerCompile<CR>")
vim.api.nvim_set_keymap("n", "<Leader>rc", ":luafile $MYVIMRC<CR>:PackerCompile<CR>", {noremap=true}) key("n", "<Leader>rc", ":luafile $MYVIMRC<CR>:PackerCompile<CR>")
-- Keep cursor in place -- Keep cursor in place
vim.api.nvim_set_keymap("n", 'n', "nzz", {noremap=true}) key("n", 'n', "nzz")
vim.api.nvim_set_keymap("n", 'N', "Nzz", {noremap=true}) key("n", 'N', "Nzz")
vim.api.nvim_set_keymap("n", 'J', "mzJ`z", {noremap=true}) --- Mark and jump back to it key("n", 'J', "mzJ`z") --- Mark and jump back to it
-- Add undo breakpoints -- Add undo breakpoints
vim.api.nvim_set_keymap("i", ',', ",<C-g>u", {noremap=true}) key("i", ',', ",<C-g>u")
vim.api.nvim_set_keymap("i", '.', ".<C-g>u", {noremap=true}) key("i", '.', ".<C-g>u")
vim.api.nvim_set_keymap("i", '!', "!<C-g>u", {noremap=true}) key("i", '!', "!<C-g>u")
vim.api.nvim_set_keymap("i", '?', "?<C-g>u", {noremap=true}) key("i", '?', "?<C-g>u")
-- Other -- Other
vim.api.nvim_set_keymap("t", "<A-CR>", "<C-\\><C-n>", {noremap=true}) --- Exit terminal mode key("t", "<A-CR>", "<C-\\><C-n>") --- Exit terminal mode
vim.api.nvim_set_keymap("n", "<A-CR>", ":noh<CR>", {noremap=true, silent=true}) --- Clear search key("n", "<A-CR>", ":noh<CR>", {silent=true}) --- Clear search
vim.api.nvim_set_keymap('n', 'Y', 'y$', { noremap = true}) --- Copy to end of line key("n", "Y", "y$") --- Copy to end of line