From 1ac7506a87a311c09c63023e5921153d11d75469 Mon Sep 17 00:00:00 2001 From: Mads Fagerlund Date: Mon, 7 Sep 2026 23:40:24 +0200 Subject: [PATCH] add comments and simplify --- config/nvim/init.lua | 163 +++++++++++++++++++++++++------------------ 1 file changed, 95 insertions(+), 68 deletions(-) diff --git a/config/nvim/init.lua b/config/nvim/init.lua index a82dc84..534ca71 100644 --- a/config/nvim/init.lua +++ b/config/nvim/init.lua @@ -1,114 +1,141 @@ -vim.o.winborder = "rounded" -vim.o.tabstop = 2 -vim.o.ignorecase = true -vim.o.shiftwidth = 2 -vim.o.smartindent = true -vim.opt.wrap = false -vim.opt.number = true -vim.o.relativenumber = true -vim.o.termguicolors = true -vim.o.undofile = true -vim.o.incsearch = true -vim.o.signcolumn = "yes" -vim.opt.clipboard = "unnamedplus" -vim.opt.expandtab = true +vim.o.winborder = "rounded" -- floating windows (hover docs, pickers) get rounded borders +vim.o.tabstop = 2 -- a tab character is rendered 2 columns wide +vim.o.ignorecase = true -- searching is case insensitive +vim.o.shiftwidth = 2 -- >> and << indent by 2 +vim.o.smartindent = true -- indent new lines based on the line above +vim.opt.wrap = false -- long lines run off the screen instead of wrapping +vim.opt.number = true -- show line numbers +vim.o.relativenumber = true -- ...but relative to the cursor, so 5j / 5k are easy to aim +vim.o.termguicolors = true -- 24 bit color, the colorscheme needs this +vim.o.undofile = true -- keep undo history on disk, so you can undo after reopening a file +vim.o.incsearch = true -- jump to the match while you are still typing the search +vim.o.signcolumn = "yes" -- always show the left gutter (diagnostics live there) so text never jumps +vim.opt.clipboard = "unnamedplus" -- yank and paste use the system clipboard +vim.opt.expandtab = true -- pressing tab inserts spaces, not a tab character --- On NixOS the flake provides plugins (vim.g.nix_managed set in the flake); --- on Arch, vim.pack fetches them at runtime. Both paths keep working. -if not vim.g.nix_managed then - vim.pack.add({ - { src = "https://github.com/nvim-tree/nvim-web-devicons" }, - { src = "https://github.com/scottmckendry/cyberdream.nvim" }, - { src = "https://github.com/stevearc/oil.nvim" }, - { src = "https://github.com/neovim/nvim-lspconfig" }, - { src = "https://github.com/chomosuke/typst-preview.nvim" }, - { src = "https://github.com/saghen/blink.cmp" }, - { src = "https://github.com/dynamotn/Navigator.nvim" }, - { src = "https://github.com/folke/snacks.nvim" }, - { src = "https://github.com/folke/todo-comments.nvim" }, - { src = "https://github.com/folke/trouble.nvim" }, - { src = "https://github.com/nvim-treesitter/nvim-treesitter" }, - { src = "https://github.com/j-hui/fidget.nvim" }, - { src = "https://github.com/nvim-lualine/lualine.nvim" }, - }) -end +-- vim.pack is neovim's built in plugin manager. it clones these repos into +-- stdpath("data")/site/pack the first time you start. `vim.pack.update()` +-- updates them. +vim.pack.add({ + { src = "https://github.com/nvim-tree/nvim-web-devicons" }, -- filetype icons (needs a nerd font in your terminal) + { src = "https://github.com/scottmckendry/cyberdream.nvim" }, -- the colorscheme + { src = "https://github.com/stevearc/oil.nvim" }, -- file manager that is just an editable buffer + { src = "https://github.com/neovim/nvim-lspconfig" }, -- ready made configs for language servers + { src = "https://github.com/saghen/blink.cmp" }, -- the autocomplete popup + { src = "https://github.com/folke/snacks.nvim" }, -- grab bag of small tools. we use the picker, terminal and lazygit + { src = "https://github.com/folke/todo-comments.nvim" }, -- highlights TODO/FIXME/HACK and can list them + { src = "https://github.com/folke/trouble.nvim" }, -- a readable list of errors and warnings + { src = "https://github.com/nvim-treesitter/nvim-treesitter" },-- real syntax parsing, better highlighting + { src = "https://github.com/j-hui/fidget.nvim" }, -- little lsp progress spinner in the corner + { src = "https://github.com/nvim-lualine/lualine.nvim" }, -- the statusline at the bottom +}) +-- most plugins only need setup() called with no arguments to turn them on. require("blink.cmp").setup() -require("Navigator").setup() require("todo-comments").setup() require("trouble").setup() require("fidget").setup() require("lualine").setup({ options = { + -- plain | instead of the default powerline arrows, so it looks right in any font component_separators = { left = "|", right = "|" }, section_separators = { left = "|", right = "|" }, }, }) require("oil").setup({ - default_file_explorer = true, - delete_to_trash = true, - skip_confirm_for_simple_edits = true, - view_options = { show_hidden = true }, + default_file_explorer = true, -- opening a directory opens oil instead of netrw + delete_to_trash = true, -- deleting a file in oil sends it to the trash, not the void + skip_confirm_for_simple_edits = true, -- don't ask "are you sure" for renames and single deletes + view_options = { show_hidden = true }, -- show dotfiles + -- oil's own keymaps, inside an oil buffer only. keymaps = { - ["g?"] = { "actions.show_help", mode = "n" }, - [""] = "actions.select", - ["-"] = { "actions.parent", mode = "n" }, - ["_"] = { "actions.open_cwd", mode = "n" }, - ["`"] = { "actions.cd", mode = "n" }, + ["g?"] = { "actions.show_help", mode = "n" }, -- everything oil can do + [""] = "actions.select", -- open the file/directory under the cursor + ["-"] = { "actions.parent", mode = "n" }, -- go up one directory + ["_"] = { "actions.open_cwd", mode = "n" }, -- jump to the directory nvim was started in + ["`"] = { "actions.cd", mode = "n" }, -- make this directory nvim's working directory }, - use_default_keymaps = false, + use_default_keymaps = false, -- only the five above, nothing else }) -require("snacks").setup({ picker = { ui_select = true } }) +require("snacks").setup({ picker = { ui_select = true } }) -- any "pick one" prompt uses the snacks picker vim.cmd("colorscheme cyberdream") + +-- briefly flash whatever you just yanked, so you can see what got copied vim.api.nvim_create_autocmd("TextYankPost", { callback = function() vim.highlight.on_yank() end, }) -vim.api.nvim_create_autocmd("FileType", { - pattern = "typst", - callback = function() - vim.opt_local.wrap = true - vim.opt_local.linebreak = true - vim.opt_local.breakindent = true - end, -}) -vim.lsp.enable({ "lua_ls", "svelte", "tinymist", "ts_ls", "rust_analyzer", "yamlls", "html", "cssls", "jsonls", "nixd", "zls" }) +-- These are the language servers we turn on. the server programs themselves are +-- installed by nix (see programs.neovim.extraPackages in flake.nix), this line +-- only tells neovim to start them for the right filetypes. +vim.lsp.enable({ "lua_ls", "ts_ls", "rust_analyzer", "html", "cssls", "jsonls", "nixd" }) + +-- This means that we should run the callback when the event LspAttach happens +-- aka. when a language server attaches to the buffer. that is when these +-- keymaps start doing something useful, so we only set them then. vim.api.nvim_create_autocmd("LspAttach", { callback = function(ev) - vim.keymap.set('n', 'gd', vim.lsp.buf.definition) - vim.keymap.set('n', 'gD', vim.lsp.buf.declaration) - vim.keymap.set('n', 'gr', Snacks.picker.lsp_references) - vim.keymap.set('n', 'gt', Snacks.picker.lsp_type_definitions) - vim.keymap.set('n', 'ss', Snacks.picker.lsp_symbols) - vim.keymap.set('n', 'sS', Snacks.picker.lsp_workspace_symbols) - vim.keymap.set('n', 'ca', vim.lsp.buf.code_action) - vim.keymap.set('n', 'cr', vim.lsp.buf.rename) + vim.keymap.set('n', 'gd', vim.lsp.buf.definition) -- go to where the thing under the cursor is defined + vim.keymap.set('n', 'gD', vim.lsp.buf.declaration) -- go to its declaration (mostly a C/C++ thing) + vim.keymap.set('n', 'gr', Snacks.picker.lsp_references) -- list every place it is used + vim.keymap.set('n', 'gt', Snacks.picker.lsp_type_definitions) -- go to the definition of its type + vim.keymap.set('n', 'ss', Snacks.picker.lsp_symbols) -- search symbols in this file + vim.keymap.set('n', 'sS', Snacks.picker.lsp_workspace_symbols) -- search symbols in the whole project + vim.keymap.set('n', 'ca', vim.lsp.buf.code_action) -- offer the fixes/refactors the server suggests + vim.keymap.set('n', 'cr', vim.lsp.buf.rename) -- rename it everywhere at once end }) +-- This is your leader key. set it to whatever you like. but space is nice i think vim.g.mapleader = " " -vim.keymap.set('n', 'o', ':update :source') +-- space+o in normal mode saves this file and re-runs it, so config changes apply +-- without restarting neovim. (to update the plugins themselves: :lua vim.pack.update()) +vim.keymap.set('n', 'o', ':update:source') +-- space+w in normal mode writes (saves) the current buffer vim.keymap.set('n', 'w', ':write') +-- space+q in normal mode quits vim.keymap.set('n', 'q', ':quit') +-- space+f in normal mode opens a file picker where you can fuzzy search for files by their name vim.keymap.set('n', 'f', function() Snacks.picker.files() end) +-- space+sg in normal mode opens a fuzzy searcher that searches through the contents of all files vim.keymap.set('n', 'sg', function() Snacks.picker.grep() end) +-- space+e in normal mode opens a new buffer with Oil. this is the file manager or what you would call it +-- The cool thing about oil is that you can edit all of your files like neovim buffers vim.keymap.set('n', 'e', ':Oil') -vim.keymap.set({ 'n', 't' }, '', ':NavigatorLeft') -vim.keymap.set({ 'n', 't' }, '', ':NavigatorRight') -vim.keymap.set({ 'n', 't' }, '', ':NavigatorUp') -vim.keymap.set({ 'n', 't' }, '', ':NavigatorDown') -vim.keymap.set({ "n", "t" }, "", function() Snacks.terminal.toggle("zsh") end) -- Open Snacks picker + +-- ctrl+h/j/k/l move to the split left/down/up/right. h is neovim's own +-- "go to the window on the left", these just put it on an easier key. +-- normal mode only: inside the terminal these keys belong to the shell +-- (ctrl+l clears it), and you leave that terminal with ctrl+t anyway. +vim.keymap.set('n', '', 'h') +vim.keymap.set('n', '', 'l') +vim.keymap.set('n', '', 'k') +vim.keymap.set('n', '', 'j') + +-- ctrl+t toggles a floating zsh terminal. it works both in normal mode and from +-- inside the terminal itself, so the same key opens and closes it +vim.keymap.set({ "n", "t" }, "", function() Snacks.terminal.toggle("zsh") end) +-- space+g in normal mode opens lazygit in floating window (tui for git) vim.keymap.set('n', 'g', function() Snacks.lazygit() end) +-- space+t in normal mode opens a picker of todo comments where you can search through them vim.keymap.set('n', 't', function() Snacks.picker.todo_comments() end) +-- space+x in normal mode opens a bottom bar that shows warning and errors from your LSP in the current buffer vim.keymap.set('n', 'x', ":Trouble diagnostics toggle filter.buf=0") +-- space+shift+X does the same but for every file in the project vim.keymap.set('n', 'X', ":Trouble diagnostics") +-- space+| in normal mode vertically splits the current buffer vim.keymap.set('n', '|', ":vsplit") +-- space+_ in normal mode horizontally splits the current buffer vim.keymap.set('n', '_', ":split") +-- ESC in normal mode clears the highlighting left over from your last search vim.keymap.set('n', '', ":noh") +-- > in visual mode indents the selected lines (gv reselects them so you can keep pressing >) vim.keymap.set("v", ">", ">gv", { silent = true, desc = "Indent and reselect" }) +-- < in visual mode unindents the selected lines vim.keymap.set("v", "<", "lf', vim.lsp.buf.format)