From a2664899581e36b4ca6a3190de165af4b65f875e Mon Sep 17 00:00:00 2001 From: Thomas Alcala Schneider Date: Wed, 22 Mar 2023 17:54:34 +0100 Subject: [PATCH] feat: adding keymaps and health --- lua/custom/plugins/autocmds.lua | 72 +++++++++++++ lua/custom/plugins/health.lua | 33 ++++++ lua/custom/plugins/keymaps.lua | 149 ++++++++++++++++++++++++++ lua/custom/plugins/languages/init.lua | 5 + lua/custom/plugins/languages/rust.lua | 20 ++++ lua/custom/plugins/nvim-spectre.lua | 7 ++ lua/custom/plugins/options.lua | 58 ++++++++++ 7 files changed, 344 insertions(+) create mode 100644 lua/custom/plugins/autocmds.lua create mode 100644 lua/custom/plugins/health.lua create mode 100644 lua/custom/plugins/keymaps.lua create mode 100644 lua/custom/plugins/languages/init.lua create mode 100644 lua/custom/plugins/languages/rust.lua create mode 100644 lua/custom/plugins/nvim-spectre.lua create mode 100644 lua/custom/plugins/options.lua diff --git a/lua/custom/plugins/autocmds.lua b/lua/custom/plugins/autocmds.lua new file mode 100644 index 00000000..b4520589 --- /dev/null +++ b/lua/custom/plugins/autocmds.lua @@ -0,0 +1,72 @@ +-- Taken from https://www.lazyvim.org/configuration/general + +local function augroup(name) + return vim.api.nvim_create_augroup("lazyvim_" .. name, { clear = true }) +end + +-- Check if we need to reload the file when it changed +vim.api.nvim_create_autocmd({ "FocusGained", "TermClose", "TermLeave" }, { + group = augroup("checktime"), + command = "checktime", +}) + +-- Highlight on yank +vim.api.nvim_create_autocmd("TextYankPost", { + group = augroup("highlight_yank"), + callback = function() + vim.highlight.on_yank() + end, +}) + +-- resize splits if window got resized +vim.api.nvim_create_autocmd({ "VimResized" }, { + group = augroup("resize_splits"), + callback = function() + vim.cmd("tabdo wincmd =") + end, +}) + +-- go to last loc when opening a buffer +vim.api.nvim_create_autocmd("BufReadPost", { + group = augroup("last_loc"), + callback = function() + local mark = vim.api.nvim_buf_get_mark(0, '"') + local lcount = vim.api.nvim_buf_line_count(0) + if mark[1] > 0 and mark[1] <= lcount then + pcall(vim.api.nvim_win_set_cursor, 0, mark) + end + end, +}) + +-- close some filetypes with +vim.api.nvim_create_autocmd("FileType", { + group = augroup("close_with_q"), + pattern = { + "PlenaryTestPopup", + "help", + "lspinfo", + "man", + "notify", + "qf", + "query", -- :InspectTree + "spectre_panel", + "startuptime", + "tsplayground", + }, + callback = function(event) + vim.bo[event.buf].buflisted = false + vim.keymap.set("n", "q", "close", { buffer = event.buf, silent = true }) + end, +}) + +-- wrap and check for spell in text filetypes +vim.api.nvim_create_autocmd("FileType", { + group = augroup("wrap_spell"), + pattern = { "gitcommit", "markdown" }, + callback = function() + vim.opt_local.wrap = true + vim.opt_local.spell = true + end, +}) + +return {} diff --git a/lua/custom/plugins/health.lua b/lua/custom/plugins/health.lua new file mode 100644 index 00000000..f0b67a60 --- /dev/null +++ b/lua/custom/plugins/health.lua @@ -0,0 +1,33 @@ +local function health() + vim.health.report_start() + + if vim.fn.has("nvim-0.8.0") == 1 then + vim.health.report_ok("Using Neovim >= 0.8.0") + else + vim.health.report_error("Neovim >= 0.8.0 is required") + end + + for _, cmd in ipairs({ "git", "rg", { "fd", "fdfind" }, "lazygit" }) do + local name = type(cmd) == "string" and cmd or vim.inspect(cmd) + local commands = type(cmd) == "string" and { cmd } or cmd + ---@cast commands string[] + local found = false + + for _, c in ipairs(commands) do + if vim.fn.executable(c) == 1 then + name = c + found = true + end + end + + if found then + vim.health.report_ok(("`%s` is installed"):format(name)) + else + vim.health.report_warn(("`%s` is not installed"):format(name)) + end + end +end + +vim.api.nvim_create_user_command("Health", health, {}) + +return {} diff --git a/lua/custom/plugins/keymaps.lua b/lua/custom/plugins/keymaps.lua new file mode 100644 index 00000000..d403c94b --- /dev/null +++ b/lua/custom/plugins/keymaps.lua @@ -0,0 +1,149 @@ +-- Taken from https://www.lazyvim.org/configuration/general + +local Util = require("lazy.core.util") + +local function map(mode, lhs, rhs, opts) + vim.keymap.set(mode, lhs, rhs, opts) +end + +local function has(plugin) + return require("lazy.core.config").plugins[plugin] ~= nil +end + +-- better up/down +map("n", "j", "v:count == 0 ? 'gj' : 'j'", { expr = true, silent = true }) +map("n", "k", "v:count == 0 ? 'gk' : 'k'", { expr = true, silent = true }) + +-- Move to window using the hjkl keys +map("n", "", "h", { desc = "Go to left window" }) +map("n", "", "j", { desc = "Go to lower window" }) +map("n", "", "k", { desc = "Go to upper window" }) +map("n", "", "l", { desc = "Go to right window" }) + +-- Resize window using arrow keys +map("n", "", "resize +2", { desc = "Increase window height" }) +map("n", "", "resize -2", { desc = "Decrease window height" }) +map("n", "", "vertical resize -2", { desc = "Decrease window width" }) +map("n", "", "vertical resize +2", { desc = "Increase window width" }) + +-- Move Lines +map("n", "", "m .+1==", { desc = "Move down" }) +map("n", "", "m .-2==", { desc = "Move up" }) +map("i", "", "m .+1==gi", { desc = "Move down" }) +map("i", "", "m .-2==gi", { desc = "Move up" }) +map("v", "", ":m '>+1gv=gv", { desc = "Move down" }) +map("v", "", ":m '<-2gv=gv", { desc = "Move up" }) + +-- buffers +if has("bufferline.nvim") then + map("n", "", "BufferLineCyclePrev", { desc = "Prev buffer" }) + map("n", "", "BufferLineCycleNext", { desc = "Next buffer" }) + map("n", "[b", "BufferLineCyclePrev", { desc = "Prev buffer" }) + map("n", "]b", "BufferLineCycleNext", { desc = "Next buffer" }) +else + map("n", "", "bprevious", { desc = "Prev buffer" }) + map("n", "", "bnext", { desc = "Next buffer" }) + map("n", "[b", "bprevious", { desc = "Prev buffer" }) + map("n", "]b", "bnext", { desc = "Next buffer" }) +end +map("n", "bb", "e #", { desc = "Switch to Other Buffer" }) +map("n", "`", "e #", { desc = "Switch to Other Buffer" }) + +-- Clear search with +map({ "i", "n" }, "", "noh", { desc = "Escape and clear hlsearch" }) + +-- Clear search, diff update and redraw +-- taken from runtime/lua/_editor.lua +map( + "n", + "ur", + "nohlsearchdiffupdatenormal! ", + { desc = "Redraw / clear hlsearch / diff update" } +) + +map({ "n", "x" }, "gw", "*N", { desc = "Search word under cursor" }) + +-- https://github.com/mhinz/vim-galore#saner-behavior-of-n-and-n +map("n", "n", "'Nn'[v:searchforward]", { expr = true, desc = "Next search result" }) +map("x", "n", "'Nn'[v:searchforward]", { expr = true, desc = "Next search result" }) +map("o", "n", "'Nn'[v:searchforward]", { expr = true, desc = "Next search result" }) +map("n", "N", "'nN'[v:searchforward]", { expr = true, desc = "Prev search result" }) +map("x", "N", "'nN'[v:searchforward]", { expr = true, desc = "Prev search result" }) +map("o", "N", "'nN'[v:searchforward]", { expr = true, desc = "Prev search result" }) + +-- Add undo break-points +map("i", ",", ",u") +map("i", ".", ".u") +map("i", ";", ";u") + +-- save file +map({ "i", "v", "n", "s" }, "", "w", { desc = "Save file" }) + +-- better indenting +map("v", "<", "", ">gv") + +-- lazy +map("n", "l", ":Lazy", { desc = "Lazy" }) + +-- new file +map("n", "fn", "enew", { desc = "New File" }) + +map("n", "xl", "lopen", { desc = "Location List" }) +map("n", "xq", "copen", { desc = "Quickfix List" }) + +if not has("trouble.nvim") then + map("n", "[q", vim.cmd.cprev, { desc = "Previous quickfix" }) + map("n", "]q", vim.cmd.cnext, { desc = "Next quickfix" }) +end + +-- stylua: ignore start + +-- toggle options +-- map("n", "uf", require("lazyvim.plugins.lsp.format").toggle, { desc = "Toggle format on Save" }) +map("n", "us", function() Util.toggle("spell") end, { desc = "Toggle Spelling" }) +map("n", "uw", function() Util.toggle("wrap") end, { desc = "Toggle Word Wrap" }) +map("n", "ul", function() + Util.toggle("relativenumber", true) + Util.toggle("number") +end, { desc = "Toggle Line Numbers" }) +-- map("n", "ud", Util.toggle_diagnostics, { desc = "Toggle Diagnostics" }) +local conceallevel = vim.o.conceallevel > 0 and vim.o.conceallevel or 3 +map("n", "uc", function() Util.toggle("conceallevel", false, { 0, conceallevel }) end, + { desc = "Toggle Conceal" }) + +-- lazygit +map("n", "gg", function() Util.float_term({ "lazygit" }, { cwd = Util.get_root() }) end, + { desc = "Lazygit (root dir)" }) +map("n", "gG", function() Util.float_term({ "lazygit" }) end, { desc = "Lazygit (cwd)" }) + +-- quit +map("n", "qq", "qa", { desc = "Quit all" }) + +-- highlights under cursor +if vim.fn.has("nvim-0.9.0") == 1 then + map("n", "ui", vim.show_pos, { desc = "Inspect Pos" }) +end + +-- floating terminal +map("n", "ft", function() Util.float_term(nil, { cwd = Util.get_root() }) end, { desc = "Terminal (root dir)" }) +map("n", "fT", function() Util.float_term() end, { desc = "Terminal (cwd)" }) +map("t", "", "", { desc = "Enter Normal Mode" }) + +-- windows +map("n", "ww", "p", { desc = "Other window" }) +map("n", "wd", "c", { desc = "Delete window" }) +map("n", "w-", "s", { desc = "Split window below" }) +map("n", "w|", "v", { desc = "Split window right" }) +map("n", "-", "s", { desc = "Split window below" }) +map("n", "|", "v", { desc = "Split window right" }) + +-- tabs +map("n", "l", "tablast", { desc = "Last Tab" }) +map("n", "f", "tabfirst", { desc = "First Tab" }) +map("n", "", "tabnew", { desc = "New Tab" }) +map("n", "]", "tabnext", { desc = "Next Tab" }) +map("n", "d", "tabclose", { desc = "Close Tab" }) +map("n", "[", "tabprevious", { desc = "Previous Tab" }) + +return {} diff --git a/lua/custom/plugins/languages/init.lua b/lua/custom/plugins/languages/init.lua new file mode 100644 index 00000000..97868c69 --- /dev/null +++ b/lua/custom/plugins/languages/init.lua @@ -0,0 +1,5 @@ +-- You can add your own plugins here or in other files in this directory! +-- I promise not to create any merge conflicts in this directory :) +-- +-- See the kickstart.nvim README for more information +return { import = "custom.plugins.languages" } diff --git a/lua/custom/plugins/languages/rust.lua b/lua/custom/plugins/languages/rust.lua new file mode 100644 index 00000000..b39be955 --- /dev/null +++ b/lua/custom/plugins/languages/rust.lua @@ -0,0 +1,20 @@ +return {'simrat39/rust-tools.nvim', + dependencies = {"rust-lang/rust.vim"}, + config = function() + vim.g.rustfmt_autosave = 1 + vim.g.rust_clip_command = "pbcopy" + + local rt = require("rust-tools") + + rt.setup({ + server = { + on_attach = function(_, bufnr) + -- Hover actions + vim.keymap.set("n", "", rt.hover_actions.hover_actions, { buffer = bufnr }) + -- Code action groups + vim.keymap.set("n", "a", rt.code_action_group.code_action_group, { buffer = bufnr }) + end, + }, + }) + end +} diff --git a/lua/custom/plugins/nvim-spectre.lua b/lua/custom/plugins/nvim-spectre.lua new file mode 100644 index 00000000..acb3de69 --- /dev/null +++ b/lua/custom/plugins/nvim-spectre.lua @@ -0,0 +1,7 @@ +return { + "windwp/nvim-spectre", + -- stylua: ignore + keys = { + { "sr", function() require("spectre").open() end, desc = "Replace in files (Spectre)" }, + }, +} diff --git a/lua/custom/plugins/options.lua b/lua/custom/plugins/options.lua new file mode 100644 index 00000000..532c6de2 --- /dev/null +++ b/lua/custom/plugins/options.lua @@ -0,0 +1,58 @@ +-- Taken from https://www.lazyvim.org/configuration/general + +vim.g.mapleader = " " +vim.g.maplocalleader = " " + +local opt = vim.opt + +opt.autowrite = true -- Enable auto write +opt.clipboard = "unnamedplus" -- Sync with system clipboard +opt.completeopt = "menu,menuone,noselect" +opt.conceallevel = 3 -- Hide * markup for bold and italic +opt.confirm = true -- Confirm to save changes before exiting modified buffer +opt.cursorline = true -- Enable highlighting of the current line +opt.expandtab = true -- Use spaces instead of tabs +opt.formatoptions = "jcroqlnt" -- tcqj +opt.grepformat = "%f:%l:%c:%m" +opt.grepprg = "rg --vimgrep" +opt.ignorecase = true -- Ignore case +opt.inccommand = "nosplit" -- preview incremental substitute +opt.laststatus = 0 +opt.list = true -- Show some invisible characters (tabs... +opt.mouse = "a" -- Enable mouse mode +opt.number = true -- Print line number +opt.pumblend = 10 -- Popup blend +opt.pumheight = 10 -- Maximum number of entries in a popup +opt.relativenumber = true -- Relative line numbers +opt.scrolloff = 4 -- Lines of context +opt.sessionoptions = { "buffers", "curdir", "tabpages", "winsize" } +opt.shiftround = true -- Round indent +opt.shiftwidth = 2 -- Size of an indent +opt.shortmess:append { W = true, I = true, c = true } +opt.showmode = false -- Dont show mode since we have a statusline +opt.sidescrolloff = 8 -- Columns of context +opt.signcolumn = "yes" -- Always show the signcolumn, otherwise it would shift the text each time +opt.smartcase = true -- Don't ignore case with capitals +opt.smartindent = true -- Insert indents automatically +opt.spelllang = { "en" } +opt.splitbelow = true -- Put new windows below current +opt.splitright = true -- Put new windows right of current +opt.tabstop = 2 -- Number of spaces tabs count for +opt.termguicolors = true -- True color support +opt.timeoutlen = 300 +opt.undofile = true +opt.undolevels = 10000 +opt.updatetime = 200 -- Save swap file and trigger CursorHold +opt.wildmode = "longest:full,full" -- Command-line completion mode +opt.winminwidth = 5 -- Minimum window width +opt.wrap = false -- Disable line wrap + +if vim.fn.has("nvim-0.9.0") == 1 then + opt.splitkeep = "screen" + opt.shortmess:append { C = true } +end + +-- Fix markdown indentation settings +vim.g.markdown_recommended_style = 0 + +return {}