nvim/nvim/lua/keybinds.lua
2025-01-10 09:28:34 +01:00

123 lines
5.5 KiB
Lua
Executable File

local opts = require("utils").opts
local keymap = vim.keymap
-- write to file (sudo if readonly)
if vim.g.vscode then
keymap.set("n", "W", ":Write<CR>", opts("write file (vscode)"))
else
keymap.set("n", "W", ":w!<CR>", opts("write file"))
keymap.set("n", "<leader>W", ":lua require('utils').sudo_write()<CR>", opts("sudo write file"))
end
-- Handy VSCode Shortcuts
-- Move lines up and down
vim.api.nvim_set_keymap('n', '<A-Up>', ':m .-2<CR>==', { noremap = true, silent = true })
vim.api.nvim_set_keymap('n', '<A-Down>', ':m .+1<CR>==', { noremap = true, silent = true })
-- Move lines in insert mode
vim.api.nvim_set_keymap('i', '<A-Up>', '<Esc>:m .-2<CR>i', { noremap = true, silent = true })
vim.api.nvim_set_keymap('i', '<A-Down>', '<Esc>:m .+1<CR>i', { noremap = true, silent = true })
vim.api.nvim_set_keymap('i', '<A-S-k>', '<Esc>:m .-2<CR>i', { noremap = true, silent = true })
vim.api.nvim_set_keymap('i', '<A-S-j>', '<Esc>:m .+1<CR>i', { noremap = true, silent = true })
-- Copy lines up and down in normal mode
vim.api.nvim_set_keymap('n', '<C-A-S-Up>', ':t .-1<CR>', { noremap = true, silent = true })
vim.api.nvim_set_keymap('n', '<C-A-S-Down>', ':t .-1<CR>', { noremap = true, silent = true })
vim.api.nvim_set_keymap('n', '<C-A-S-k>', ':t .-1<CR>', { noremap = true, silent = true })
vim.api.nvim_set_keymap('n', '<C-A-S-j>', ':t .+1<CR>', { noremap = true, silent = true })
-- Copy lines up and down in insert mode
vim.api.nvim_set_keymap('i', '<C-A-S-Up>', '<Esc>:t.-1<CR>i', { noremap = true, silent = true })
vim.api.nvim_set_keymap('i', '<C-A-S-Down>', '<Esc>:t.<CR>i', { noremap = true, silent = true })
vim.api.nvim_set_keymap('i', '<C-A-S-k>', '<Esc>:t.-1<CR>i', { noremap = true, silent = true })
vim.api.nvim_set_keymap('i', '<C-A-S-j>', '<Esc>:t.<CR>i', { noremap = true, silent = true })
-- Save shortcut
keymap.set("i", "<C-s>", "<Esc>:w<CR>a", opts("Save file (shortcut)"))
keymap.set("n", "<C-s>", ":w<CR>", opts("Save file (shortcut)"))
-- Save and quit (sudo and no sudo)
keymap.set("n", "E", ":wq!<CR>", opts("write and quit file"))
keymap.set("n", "<leader>E", ":lua require('utils').sudo_write()<CR>:q!<CR>", opts("sudo write and quit file"))
-- Quite file
keymap.set("n", "Q", ":q!<CR>", opts("quit file"))
-- Duplicate line
keymap.set("n", "<leader>dd", ":t.<CR>", opts("Duplicate line"))
-- Editor focus
keymap.set("n", "<C-b>", ":wincmd p<CR>", opts("Cycle focus"))
-- Key Mappings for LaTeX workflow
keymap.set('n', '<leader>lc', ':VimtexCompile<CR>', opts("Compile latex document")) -- Compile LaTeX file
keymap.set('n', '<leader>lv', ':VimtexView<CR>', opts("View compiled latex pdf")) -- View compiled PDF
keymap.set('n', '<leader>lq', ':VimtexStop<CR>', opts("Stop Compiling document")) -- Stop compilation
-- Markdown-Preview
keymap.set('n', '<leader><leader>m', ':MarkdownPreview<CR>', opts("Preview Markdown document in browser")) -- Stop compilation
-- window navigation
keymap.set("n", "<A-h>", "<C-w>h", opts())
keymap.set("n", "<A-j>", "<C-w>j", opts())
keymap.set("n", "<A-k>", "<C-w>k", opts())
keymap.set("n", "<A-l>", "<C-w>l", opts())
-- window resize
keymap.set("n", "<A-C-h>", ":vertical resize -2<CR>", opts())
keymap.set("n", "<A-C-j>", ":resize -2<CR>", opts())
keymap.set("n", "<A-C-k>", ":resize +2<CR>", opts())
keymap.set("n", "<A-C-l>", ":vertical resize +2<CR>", opts())
-- nohlsearch
keymap.set("n", "<leader>h", ":nohlsearch<CR>", opts("remove search highlighting"))
-- run file
keymap.set("n", "<leader>r", ":!%:p<CR>", opts("run current file"))
keymap.set("n", "<leader><leader>r", ":!%:p", opts("run current file with arguments"))
-- chmod +x
keymap.set("n", "<leader>m", ":!chmod +x %:p<CR>", opts("make current file executable"))
-- script setup
keymap.set("n", "<leader>#", ":set syntax=sh<CR>:w<CR>:!chmod +x %:p<CR>:LspStart bashls<CR>i#!/bin/sh<CR><CR>", opts("setup sh script"))
keymap.set("n", "<leader><leader>#", ":set syntax=python<CR>:w<CR>:!chmod +x %:p<CR>:LspStart pyright<CR>i#!/usr/bin/env python3<CR><CR>", opts("setup python script"))
-- CTRL + Backspace
keymap.set("i", "<C-BackSpace>", "<C-w>", opts())
-- select all
keymap.set("n", "<leader>a", "ggVG", opts("select all"))
-- move cursor in insert mode
keymap.set("i", "<C-h>", "<Left>", opts())
keymap.set("i", "<C-j>", "<Down>", opts())
keymap.set("i", "<C-k>", "<Up>", opts())
keymap.set("i", "<C-l>", "<Right>", opts())
-- open in neovide
-- keymap.set("n", "<leader>n", ":!neovide --no-fork %:p<CR>", opts("open in neovide (wip)"))
-- deleting doesn't yank except when used with <leader>
keymap.set({ "n", "v" }, "d", '"_d', opts())
keymap.set({ "n", "v" }, "D", '"_D', opts())
keymap.set({ "n", "v" }, "x", '"_x', opts())
keymap.set({ "n", "v" }, "X", '"_X', opts())
keymap.set({ "n", "v" }, "c", '"_c', opts())
keymap.set({ "n", "v" }, "C", '"_C', opts())
keymap.set({ "n", "v" }, "<leader>d", "d", opts("yank and delete"))
keymap.set({ "n", "v" }, "<leader>D", "D", opts("YANK and DELETE to end"))
-- <leader> + yank/paste/delete uses system clipboard
keymap.set({ "n", "v" }, "<leader>y", '"+y', opts("yank to system clipboard"))
keymap.set({ "n", "v" }, "<leader>Y", '"+y$', opts("YANK to system clipboard"))
keymap.set({ "n", "v" }, "<leader>p", '"+p', opts("paste from system clipboard"))
keymap.set({ "n", "v" }, "<leader>P", '"+P', opts("PASTE from system clipboard"))
keymap.set({ "n", "v" }, "<leader><leader>d", '"+d', opts("yank to system clipboard and delete"))
keymap.set({ "n", "v" }, "<leader><leader>D", '"+D', opts("YANK to system clipboard and DELETE"))
keymap.set("n", "<leader>wk", ":WhichKey<CR>", opts("Toggle WhichKey"))