dotfiles/.config/nvim/init.lua
2026-01-15 14:17:10 +01:00

311 lines
9.3 KiB
Lua

-- Set <space> as the leader key
-- See `:help mapleader`
-- NOTE: Must happen before plugins are loaded (otherwise wrong leader will be used)
vim.g.mapleader = " "
vim.o.undofile = true
-- [[ Setting options ]] See `:h vim.o`
-- NOTE: You can change these options as you wish!
-- For more options, you can see `:help option-list`
-- To see documentation for an option, you can use `:h 'optionname'`, for example `:h 'number'`
-- (Note the single quotes)
vim.o.mouse = ""
-- Print the line number in front of each line
vim.o.number = true
local augroup = vim.api.nvim_create_augroup("numbertoggle", {})
vim.api.nvim_create_autocmd({ "BufEnter", "FocusGained", "InsertLeave", "CmdlineLeave", "WinEnter" }, {
pattern = "*",
group = augroup,
callback = function()
if vim.o.nu and vim.api.nvim_get_mode().mode ~= "i" then
vim.opt.relativenumber = true
end
end,
})
vim.api.nvim_create_autocmd({ "BufLeave", "FocusLost", "InsertEnter", "CmdlineEnter", "WinLeave" }, {
pattern = "*",
group = augroup,
callback = function()
if vim.o.nu then
vim.opt.relativenumber = false
-- Conditional taken from https://github.com/rockyzhang24/dotfiles/commit/03dd14b5d43f812661b88c4660c03d714132abcf
-- Workaround for https://github.com/neovim/neovim/issues/32068
if not vim.tbl_contains({ "@", "-" }, vim.v.event.cmdtype) then
vim.cmd("redraw")
end
end
end,
})
-- Sync clipboard between OS and Neovim. Schedule the setting after `UiEnter` because it can
-- increase startup-time. Remove this option if you want your OS clipboard to remain independent.
-- See `:help 'clipboard'`
vim.api.nvim_create_autocmd("UIEnter", {
callback = function()
vim.o.clipboard = "unnamedplus"
end,
})
vim.o.termguicolors = true
vim.cmd.colorscheme("gruvbox")
-- Case-insensitive searching UNLESS \C or one or more capital letters in the search term
vim.o.ignorecase = true
vim.o.smartcase = true
vim.o.showmode = false
vim.o.signcolumn = "yes"
-- Highlight the line where the cursor is on
vim.o.cursorline = true
-- Column at 80 characters
vim.o.colorcolumn = "80"
-- Minimal number of screen lines to keep above and below the cursor.
vim.o.scrolloff = 10
-- Show <tab> and trailing spaces
vim.o.list = true
vim.opt.listchars = { tab = "» ", trail = "·", nbsp = "" }
-- Disable line wrap
vim.o.linebreak = true
vim.o.breakindent = true
vim.o.showbreak = ""
vim.o.tabstop = 4
vim.o.shiftwidth = 4
-- if performing an operation that would fail due to unsaved changes in the buffer (like `:q`),
-- instead raise a dialog asking if you wish to save the current file(s) See `:help 'confirm'`
vim.o.confirm = true
-- [[ Set up keymaps ]] See `:h vim.keymap.set()`, `:h mapping`, `:h keycodes`
-- Use <Esc> to exit terminal mode
vim.keymap.set("t", "<Esc>", "<C-\\><C-n>")
-- Map <A-j>, <A-k>, <A-h>, <A-l> to navigate between windows in any modes
vim.keymap.set({ "t", "i" }, "<A-h>", "<C-\\><C-n><C-w>h")
vim.keymap.set({ "t", "i" }, "<A-j>", "<C-\\><C-n><C-w>j")
vim.keymap.set({ "t", "i" }, "<A-k>", "<C-\\><C-n><C-w>k")
vim.keymap.set({ "t", "i" }, "<A-l>", "<C-\\><C-n><C-w>l")
vim.keymap.set({ "n" }, "<A-h>", "<C-w>h")
vim.keymap.set({ "n" }, "<A-j>", "<C-w>j")
vim.keymap.set({ "n" }, "<A-k>", "<C-w>k")
vim.keymap.set({ "n" }, "<A-l>", "<C-w>l")
-- [[ Basic Autocommands ]].
-- See `:h lua-guide-autocommands`, `:h autocmd`, `:h nvim_create_autocmd()`
-- Highlight when yanking (copying) text.
-- Try it with `yap` in normal mode. See `:h vim.hl.on_yank()`
vim.api.nvim_create_autocmd("TextYankPost", {
desc = "Highlight when yanking (copying) text",
callback = function()
vim.hl.on_yank()
end,
})
-- [[ Create user commands ]]
-- See `:h nvim_create_user_command()` and `:h user-commands`
-- Create a command `:GitBlameLine` that print the git blame for the current line
vim.api.nvim_create_user_command("GitBlameLine", function()
local line_number = vim.fn.line(".") -- Get the current line number. See `:h line()`
local filename = vim.api.nvim_buf_get_name(0)
print(vim.fn.system({ "git", "blame", "-L", line_number .. ",+1", filename }))
end, { desc = "Print the git blame for the current line" })
-- [[ Add optional packages ]]
-- Nvim comes bundled with a set of packages that are not enabled by
-- default. You can enable any of them by using the `:packadd` command.
-- For example, to add the "nohlsearch" package to automatically turn off search highlighting after
-- 'updatetime' and when going to insert mode
vim.cmd("packadd! nohlsearch")
vim.pack.add({"https://github.com/nvim-treesitter/nvim-treesitter"})
-- require("nvim-treesitter.install").update("all") -- automatically update all TS-grammars
-- require("nvim-treesitter.config").setup()
-- require("nvim-treesitter").setup({
-- sync_install = true,
-- modules = {},
-- ignore_install = {},
-- ensure_installed = {
-- "lua",
-- "c",
-- "ocaml",
-- "latex",
-- },
-- auto_install = true, -- autoinstall languages that are not installed yet
-- highlight = {
-- enable = true,
-- },
-- })
-- Treat Ipe stylesheets as xml code
vim.filetype.add({
extension = {
isy = "xml",
},
})
-- See `:h lspconfig-all` for available servers and their settings
local lsp_servers = {
lua_ls = {
-- https://luals.github.io/wiki/settings/ | `:h nvim_get_runtime_file`
Lua = { workspace = { library = vim.api.nvim_get_runtime_file("lua", true) } },
},
clangd = {},
rust_analyzer = {},
texlab = {},
}
-- See `:h lsp-quickstart` for more details.
vim.pack.add({
"https://github.com/neovim/nvim-lspconfig", -- default configs for lsps
"https://github.com/mason-org/mason.nvim", -- package manager
"https://github.com/mason-org/mason-lspconfig.nvim", -- lspconfig bridge
"https://github.com/WhoIsSethDaniel/mason-tool-installer.nvim", -- auto installer
}, { confirm = false })
require("mason").setup()
require("mason-lspconfig").setup()
require("mason-tool-installer").setup({
ensure_installed = vim.tbl_keys(lsp_servers),
})
-- Configure each lsp server on the table
-- To check what clients are attached to the current buffer, use `:checkhealth vim.lsp`.
-- To view default lsp keybindings, use `:h lsp-defaults`.
for server, config in pairs(lsp_servers) do
vim.lsp.config(server, {
settings = config,
-- only create the keymaps if the server attaches successfully
on_attach = function(_, bufnr)
-- Put mappings here
vim.keymap.set(
"n",
"<leader>ld",
vim.lsp.buf.definition,
{ buffer = bufnr, desc = "vim.lsp.buf.definition()" }
)
vim.keymap.set("n", "<leader>lf", vim.lsp.buf.format, { buffer = bufnr, desc = "vim.lsp.buf.format()" })
end,
})
end
vim.diagnostic.config({
signs = {
text = {
[vim.diagnostic.severity.ERROR] = "W ",
[vim.diagnostic.severity.WARN] = "w ",
[vim.diagnostic.severity.INFO] = "I ",
[vim.diagnostic.severity.HINT] = "H ",
},
},
virtual_text = true, -- Show inline diagnostics
})
vim.keymap.set("n", "<leader>dn", "]d") -- Jump to next diagnostic
vim.keymap.set("n", "<leader>dp", "[d") -- Jump to next diagnostic
vim.keymap.set("n", "<leader>dN", "]D") -- Jump to last diagnostic
vim.keymap.set("n", "<leader>dP", "[D") -- Jump to first diagnostic
vim.keymap.set("n", "<leader>ds", "<C-w>d") -- Display diagnostic on current line
require("nvim-treesitter").install('latex')
vim.pack.add({
"https://github.com/nvim-mini/mini.completion",
"https://github.com/nvim-mini/mini.snippets",
"https://github.com/nvim-mini/mini.icons",
"https://github.com/nvim-mini/mini.statusline",
"https://github.com/nvim-mini/mini-git",
"https://github.com/nvim-mini/mini.diff",
"https://github.com/nvim-mini/mini.clue",
"https://github.com/nvim-mini/mini.hipatterns",
"https://github.com/nvim-mini/mini.notify",
}, { confirm = false })
require("mini.completion").setup()
require("mini.snippets").setup()
require("mini.icons").setup()
require("mini.statusline").setup()
require("mini.git").setup()
require("mini.diff").setup()
local miniclue = require("mini.clue")
miniclue.setup({
triggers = {
-- Leader triggers
{ mode = { "n", "x" }, keys = "<Leader>" },
-- `[` and `]` keys
{ mode = "n", keys = "[" },
{ mode = "n", keys = "]" },
-- Built-in completion
{ mode = "i", keys = "<C-x>" },
-- `g` key
{ mode = { "n", "x" }, keys = "g" },
-- Marks
{ mode = { "n", "x" }, keys = "'" },
{ mode = { "n", "x" }, keys = "`" },
-- Registers
{ mode = { "n", "x" }, keys = '"' },
{ mode = { "i", "c" }, keys = "<C-r>" },
-- Window commands
{ mode = "n", keys = "<C-w>" },
-- `z` key
{ mode = { "n", "x" }, keys = "z" },
},
clues = {
-- Enhance this by adding descriptions for <Leader> mapping groups
miniclue.gen_clues.square_brackets(),
miniclue.gen_clues.builtin_completion(),
miniclue.gen_clues.g(),
miniclue.gen_clues.marks(),
miniclue.gen_clues.registers(),
miniclue.gen_clues.windows(),
miniclue.gen_clues.z(),
},
window = {
delay = 200,
}
})
local hipatterns = require('mini.hipatterns')
hipatterns.setup({
highlighters = {
-- Highlight standalone 'FIXME', 'HACK', 'TODO', 'NOTE'
fixme = { pattern = '%f[%w]()FIXME()%f[%W]', group = 'MiniHipatternsFixme' },
hack = { pattern = '%f[%w]()HACK()%f[%W]', group = 'MiniHipatternsHack' },
todo = { pattern = '%f[%w]()TODO()%f[%W]', group = 'MiniHipatternsTodo' },
note = { pattern = '%f[%w]()NOTE()%f[%W]', group = 'MiniHipatternsNote' },
-- Highlight hex color strings (`#rrggbb`) using that color
hex_color = hipatterns.gen_highlighter.hex_color(),
},
})
require("mini.notify").setup()