repos / dotfiles

my dotfiles

commit
393cd74
parent
a530a08
author
Eric Bower
date
2025-09-25 23:40:49 -0400 EDT
refactor(nvim): <leader>b now opens a quickfix with all buffers
1 files changed,  +63, -23
M dot_config/nvim/init.lua
+63, -23
  1@@ -11,7 +11,7 @@
  2     - plugins must be integral to workflow
  3 ]]
  4 local vim = vim -- suppress lsp warnings
  5-local cqf
  6+local cqf, bqf, toggle_qf
  7 local o = vim.opt
  8 o.tabstop = 2
  9 o.shiftwidth = 2
 10@@ -49,7 +49,9 @@ map("n", "<C-h>", "<cmd>wincmd h<cr>", opts)
 11 map("n", "<C-l>", "<cmd>wincmd l<cr>", opts)
 12 map("n", "<leader>t", "<cmd>bd!<cr>", opts)
 13 map("n", "<leader>f", "<cmd>term fish<cr>", opts)
 14-map("n", "<leader>b", ":b ")
 15+map("n", "<leader>b", function()
 16+	bqf()
 17+end)
 18 map({ "n", "v" }, "<leader>u", "<cmd>GitLink<cr>", opts)
 19 map("n", "<leader>e", vim.diagnostic.open_float, opts)
 20 map("n", "<leader>y", function() -- copy relative filepath to clipboard
 21@@ -67,14 +69,8 @@ map("n", "<leader>r", function()
 22 		vim.diagnostic.setloclist({ open = true })
 23 	end
 24 end, opts)
 25-map("n", "<leader>q", function() -- toggle quickfix
 26-	for _, win in ipairs(vim.fn.getwininfo()) do
 27-		if win.quickfix == 1 then
 28-			vim.cmd("cclose")
 29-			return
 30-		end
 31-	end
 32-	vim.cmd("copen")
 33+map("n", "<leader>q", function()
 34+	toggle_qf() -- toggle quickfix
 35 end, opts)
 36 map("n", "<leader>g", function() -- grep entire project
 37 	vim.ui.input({ prompt = "grep: " }, function(search)
 38@@ -82,17 +78,8 @@ map("n", "<leader>g", function() -- grep entire project
 39 		vim.cmd("copen")
 40 	end)
 41 end, opts)
 42-map("n", "<leader>d", function()
 43-	cqf({ cmd = "git diff --name-only" })
 44-end)
 45-map("n", "<leader>s", function() -- fuzzy match files into quickfix
 46-	vim.ui.input({ prompt = "file: " }, function(file)
 47-		cqf({
 48-			cmd = string.format("fd -t f | fzf -f %s", file),
 49-			auto_jump = true,
 50-		})
 51-	end)
 52-end)
 53+map("n", "<leader>d", "<cmd>Qf git diff --name-only<cr>")
 54+map("n", "<leader>s", ":Qfa rg --files | rg ") -- fuzzy match files into quickfix
 55 
 56 local augroup = vim.api.nvim_create_augroup("erock.cfg", { clear = true })
 57 local autocmd = vim.api.nvim_create_autocmd
 58@@ -195,8 +182,12 @@ require("neoscroll").setup({ duration_multiplier = 0.3 })
 59 require("gitlinker").setup()
 60 
 61 vim.api.nvim_create_user_command("Qf", function(props)
 62-	cqf({ cmd = props.args })
 63-end, { nargs = "+", desc = "convert command to quickfix" })
 64+	cqf({ cmd = props.args, auto_jump = false })
 65+end, { nargs = "+", desc = "convert system command to quickfix" })
 66+
 67+vim.api.nvim_create_user_command("Qfa", function(props)
 68+	cqf({ cmd = props.args, auto_jump = true })
 69+end, { nargs = "+", desc = "convert system command to quickfix and auto jump when only 1 result" })
 70 
 71 local function cqf_fmt(line)
 72 	local path, rest = line:match("(%S+)%s*(.*)")
 73@@ -249,3 +240,52 @@ function cqf(args)
 74 	})
 75 	vim.cmd("copen")
 76 end
 77+
 78+-- Converts the buffer list into a quickfix list.
 79+-- It also sorts buffers by recently used.
 80+function bqf()
 81+	local mru = {} -- file → 1-based index in v:oldfiles
 82+	for i, f in ipairs(vim.v.oldfiles) do
 83+		mru[vim.fn.fnamemodify(f, ":p")] = i
 84+	end
 85+
 86+	local entries = {}
 87+	for _, buf in ipairs(vim.api.nvim_list_bufs()) do
 88+		local name = vim.api.nvim_buf_get_name(buf)
 89+		if name ~= "" and vim.bo[buf].buflisted then
 90+			-- use the MRU index (or a high number if never opened)
 91+			local key = mru[vim.fn.fnamemodify(name, ":p")] or 999999
 92+			table.insert(entries, { key = key, buf = buf, name = name })
 93+		end
 94+	end
 95+
 96+	-- sort by MRU index (smaller = more recent)
 97+	table.sort(entries, function(a, b)
 98+		return a.key < b.key
 99+	end)
100+
101+	local qf = vim.tbl_map(function(e)
102+		return {
103+			bufnr = e.buf,
104+			lnum = 1,
105+			col = 1,
106+			text = vim.fn.fnamemodify(e.name, ":t"),
107+			valid = 1,
108+		}
109+	end, entries)
110+
111+	vim.fn.setqflist(qf)
112+	vim.cmd("copen")
113+end
114+
115+-- A simple toggle for quickfix.
116+-- Designed to be set to a shortcut.
117+function toggle_qf()
118+	for _, win in ipairs(vim.fn.getwininfo()) do
119+		if win.quickfix == 1 then
120+			vim.cmd("cclose")
121+			return
122+		end
123+	end
124+	vim.cmd("copen")
125+end