neovim configuration using rocks.nvim plugin manager
at main 153 lines 4.1 kB view raw
1local aug = function(group_name, clear) 2 clear = vim.F.if_nil(clear, true) 3 return vim.api.nvim_create_augroup(group_name, { clear = clear }) 4end 5 6local au = vim.api.nvim_create_autocmd 7 8au({ "FocusGained", "TermClose", "TermLeave" }, { 9 group = aug("checktime"), 10 command = "checktime", 11}) 12 13-- Highlight on yank 14au("TextYankPost", { 15 group = aug("highlight_yank"), 16 callback = function() 17 vim.highlight.on_yank() 18 end, 19}) 20 21-- -- Resize splits if window got resized 22-- au("VimResized", { 23-- group = aug("resize_splits"), 24-- command = "tabdo wincmd =", 25-- }) 26 27-- Go to last location when opening a buffer 28au("BufReadPost", { 29 group = aug("last_loc"), 30 callback = function() 31 local mark = vim.api.nvim_buf_get_mark(0, '"') 32 local lcount = vim.api.nvim_buf_line_count(0) 33 if mark[1] > 0 and mark[1] <= lcount then 34 pcall(vim.api.nvim_win_set_cursor, 0, mark) 35 end 36 end, 37}) 38 39-- Auto create dir when saving a file, in case some intermediate directory does not exists 40au("BufWritePre", { 41 group = aug("auto_create_dir"), 42 callback = function(event) 43 if event.match:match("^%w%w+://") then 44 return 45 end 46 -- TODO: confirm to create parent directories 47 local file = vim.uv.fs_realpath(event.match) or event.match 48 vim.fn.mkdir(vim.fn.fnamemodify(file, ":p:h"), "p") 49 end, 50}) 51 52-- TODO: rewrite this in style of v0.11 implementation 53 54-- Set options for terminal buffer 55-- Use `BufWinEnter term://*` instead of just `TermOpen` 56-- just `TermOpen` isn't enough when terminal buffer is created in background 57au({ "TermOpen", "BufWinEnter" }, { 58 group = aug("terminal_options"), 59 pattern = "term://*", 60 callback = function() 61 -- I should use `setlocal` than `vim.wo` or `vim.bo` 62 -- vim.wo[winid] only works with specific window id 63 vim.cmd([[ 64 setlocal nonu 65 setlocal nornu 66 setlocal nolist 67 setlocal signcolumn=no 68 setlocal foldcolumn=0 69 setlocal statuscolumn= 70 setlocal nocursorline 71 setlocal scrolloff=0 72 setlocal sidescrolloff=0 73 ]]) 74 end, 75}) 76 77local ftplugins = aug("ftplugins") 78 79au("FileType", { 80 group = ftplugins, 81 pattern = { "json", "jsonc" }, 82 callback = function() 83 vim.wo.conceallevel = 0 84 end, 85}) 86 87-- HACK: umm... is this right way..? 88au("BufWinEnter", { 89 group = ftplugins, 90 pattern = "NeogitStatus", 91 callback = function() 92 vim.wo.foldcolumn = "0" 93 vim.wo.statuscolumn = "" 94 end, 95}) 96 97-- prevent editing module files 98au("BufNew", { 99 group = ftplugins, 100 pattern = { 101 "node_modules/**", 102 vim.fs.normalize(vim.fs.joinpath(vim.env.CARGO_HOME or "~/.cargo", "registry/**")), 103 vim.fs.normalize(vim.fs.joinpath(vim.env.CARGO_HOME or "~/.cargo", "git/**")), 104 vim.fs.normalize("~/.rustup/toolchains/*/lib/rustlib/**"), 105 ".venv/**", 106 "^/nix/store/**", 107 vim.fs.normalize("~/go/pkg/mod/**"), 108 }, 109 callback = function(event) 110 vim.bo[event.buf].modifiable = false 111 end, 112}) 113 114-- HACK: ignore htmldjango 115au("FileType", { 116 group = ftplugins, 117 pattern = "htmldjango", 118 callback = function (ev) 119 vim.bo[ev.buf].filetype = "html" 120 end 121}) 122 123au("CmdlineEnter", { 124 group = aug("auto_hlsearch"), 125 callback = vim.schedule_wrap(function() 126 vim.cmd.nohlsearch() 127 end), 128}) 129 130au("ColorScheme", { 131 group = aug("user_colorscheme"), 132 -- NOTE: wrap with schedule to prevent require loop 133 callback = vim.schedule_wrap(function (ev) 134 local tbl = require("core.highlights") 135 local callback = tbl[ev.match] 136 if callback then 137 callback() 138 end 139 end), 140}) 141 142if vim.o.inccommand == "split" then 143 au("BufEnter", { 144 group = aug("fix_quicker_nvim"), 145 callback = vim.schedule_wrap(function () 146 if vim.bo.buftype == "quickfix" then 147 vim.o.inccommand = "nosplit" 148 else 149 vim.o.inccommand = "split" 150 end 151 end) 152 }) 153end