Files
nvim/lua/core/mappings.lua
T

181 lines
6.0 KiB
Lua

-- Tab mappings ---
vim.keymap.set("n", "tn", vim.cmd.tabnew)
vim.keymap.set("n", "tq", vim.cmd.tabclose)
-- Clipboard
if vim.env.TMUX and vim.fn.executable("tmux") then
vim.keymap.set(
{ "n", "x" },
"<leader>y",
"\"+y:call system('tmux load-buffer -w -', @+)<CR>"
)
else
vim.keymap.set({ "n", "x" }, "<leader>y", '"+y')
end
vim.keymap.set({ "n", "x" }, "<leader>p", '"+p')
vim.keymap.set({ "n", "x" }, "<leader>P", '"+P')
vim.keymap.set({ "n", "x" }, "<leader>+", ":call setreg('+', @\")<CR>")
-- Exit insert mode in terminal using <ESC>
vim.keymap.set("t", "<Esc>", "<C-\\><C-n>")
-- Feed ESC in terminal mode using <C-\>
vim.keymap.set("t", "<C-\\>", function()
vim.api.nvim_feedkeys(
vim.api.nvim_replace_termcodes("<Esc>", true, false, true),
"n",
false
)
end)
-- Enable <C-w> window commands in terminal mode
vim.keymap.set("t", "<C-w>", "<C-\\><C-n><C-w>")
-- Use :diffput/get instead of normal one to allow staging visual selection
vim.keymap.set({ "n", "x" }, "<leader>dp", ":diffput<CR>")
vim.keymap.set({ "n", "x" }, "<leader>do", ":diffget<CR>")
local close_pum = function()
if vim.fn.pumvisible() ~= 0 then
return "<cmd>pclose<cr>"
end
for _, winid in pairs(vim.api.nvim_tabpage_list_wins(0)) do
if vim.api.nvim_win_get_config(winid).relative ~= "" then
return "<cmd>fclose<cr>"
end
end
end
vim.keymap.set({ "n", "i" }, "<C-c>", function()
return close_pum()
end, { expr = true })
---@param force boolean
local function delete_buffer(force)
local buffers =
vim.split(vim.fn.execute("buffers t"), "\n", { trimempty = true })
if #buffers < 2 then
return
end
local current = tonumber(buffers[1]:match("^%s*(%d+)"))
local previous = tonumber(buffers[2]:match("^%s*(%d+)"))
if not current or not previous then
return
end
if not force and vim.bo[current].modified then
vim.api.nvim_echo(
{ { "Buffer has unsaved changes", "ErrorMsg" } },
false,
{}
)
return
end
vim.api.nvim_set_current_buf(previous)
vim.api.nvim_buf_delete(current, { force = force })
end
vim.keymap.set("n", "<C-q>", function()
delete_buffer(false)
end)
vim.keymap.set("n", "<M-q>", function()
delete_buffer(true)
end)
vim.keymap.set("n", "<C-Space>", ":b#<CR>")
-- Allow (de)indenting without deselecting
vim.keymap.set({ "x" }, "<", "<gv")
vim.keymap.set({ "x" }, ">", ">gv")
-- Remove default mappings
vim.keymap.set("", "<C-LeftMouse>", "")
vim.keymap.set("n", "gr", "<Nop>")
vim.keymap.set("n", "K", function()
if vim.bo.filetype == "vim" or vim.bo.filetype == "help" then
vim.cmd("help " .. vim.fn.expand("<cword>"))
else
vim.cmd("Man " .. vim.fn.expand("<cword>"))
end
end)
-- Remove right-click menu items
vim.cmd.aunmenu({ "PopUp.-1-" })
vim.cmd.aunmenu({ "PopUp.How-to\\ disable\\ mouse" })
vim.keymap.set("n", "<leader>ve", function()
if vim.o.virtualedit == "all" then
vim.o.virtualedit = "block"
else
vim.o.virtualedit = "all"
end
end)
-- Replace all occurrences of word
vim.keymap.set("n", "<Leader>r", function()
local word = vim.fn.expand("<cword>")
local replacement = vim.fn.input('Replace "' .. word .. '" by? ')
if replacement ~= "" then
local cursor_pos = vim.api.nvim_win_get_cursor(0)
vim.cmd(":%s/\\<" .. word .. "\\>/" .. replacement .. "/g")
vim.api.nvim_win_set_cursor(0, cursor_pos)
end
end)
vim.keymap.set("n", "]c", ":cn<CR>")
vim.keymap.set("n", "[c", ":cp<CR>")
-- Default bindings that are good to know:
-- insert mode:
-- <C-T> - indent, see :h i_CTRL-T
-- <C-D> - un-indent, see :h i_CTRL-D
-- normal mode:
-- H/M/L - Jump to highest/middle/lowest line in window.
-- <count?><C-E> - scroll window down <count> lines, see :h CTRL-E
-- <count?><C-Y> - scroll window up <count> lines, see :h CTRL-Y
-- <C-A> - Increment
-- <C-X> - Decrement
-- <C-w>H - Move window to the left
-- <C-w>J - Move window down
-- <C-w>K - Move window up
-- <C-w>L - Move window to the right
-- gq{motion} - format word-wrap of the line that {motion} moves over
-- {Visual}gq - format word-wrap of the visually selected area
-- gqq - format word-wrap of the current line
-- commands:
-- :make - execute makeprg with given args, and put the output in
-- quickfix list
-- :grep - execute grepprg with given args, and put the results in
-- quickfix list
-- :cex {expr} - Create a quickfix list using the result of {expr} and
-- jump to the first error. For example:
-- :cex system('make')
-- :cgete {expr} - same as :cex but don't jump to the first error
-- :copen - open quickfix list
-- :cdo {cmd} - execute {cmd} in each valid entry in the quickfix list.
-- works like this:
-- :cfirst
-- :{cmd}
-- :cnext
-- :{cmd}
-- etc.
-- :cfdo {cmd} - same as :cdo but on each file in quickfix list
-- :cn - go to the next error in quickfix list that includes a file name
-- :cp - go to the previous error in quickfix list that includes a file name
-- :cc [num] - go to the specified error in quickfix list
-- @: - repeat last command
-- :s/foo/bar/ - substitute the first match of foo with bar in the current line
-- :s/foo/bar/g - same as above but for all matches in the current line
-- :%s/foo/bar/g - same as above, but for all lines in buffer
-- :%s/foo/bar/gc - same as above but asking for confirmation on each match
-- :lua << EOF - run a lua snippet using lua-heredoc syntax
-- local tbl = {1, 2, 3}
-- for k, v in ipairs(tbl) do
-- print(v)
-- end
-- EOF
-- :diffsplit <other-file> - open diff split