馃 my neovim config:)
1if vim.g.loaded_grep then
2 return
3end
4
5vim.g.loaded_grep = true
6
7local function grep(props)
8 local grepcmd, n = vim.o.grepprg:gsub("%$%*", props.args)
9 if n == 0 then
10 grepcmd = grepcmd .. " " .. props.args
11 end
12 vim.api.nvim_exec_autocmds("QuickFixCmdPre", {
13 pattern = "grep",
14 modeline = false,
15 })
16 local fn = function(o)
17 local src = o.stderr
18 if o.code == 0 then
19 src = o.stdout
20 end
21 src = src
22 local lines = vim.split(src, "\n", { trimempty = true })
23 vim.schedule(function()
24 vim.fn.setqflist({}, " ", {
25 title = grepcmd,
26 lines = lines,
27 efm = vim.o.grepformat,
28 nr = "$",
29 })
30 vim.api.nvim_exec_autocmds("QuickFixCmdPost", {
31 pattern = "grep",
32 modeline = false,
33 })
34 end)
35 end
36 vim.system({ vim.o.shell, "-c", grepcmd }, {
37 text = true,
38 }, fn)
39end
40
41vim.api.nvim_create_user_command("Grep", grep, { nargs = "+", complete = "file_in_path", force = true })
42
43vim.api.nvim_create_autocmd("QuickFixCmdPost", {
44 group = vim.api.nvim_create_augroup("grep", { clear = true }),
45 nested = true,
46 callback = function()
47 local list = vim.fn.getqflist()
48 vim.cmd([[ cclose|botright ]] .. " " .. (math.min(10, #list)) .. "cwindow")
49 end,
50})
51
52vim.keymap.set("n", "<leader>/", ":Grep ", { silent = false })
53vim.keymap.set("x", "<leader>/", function()
54 local lines = vim.fn.getregion(vim.fn.getpos("v"), vim.fn.getpos("."), { type = vim.api.nvim_get_mode().mode })
55 if #lines ~= 1 then
56 return ":Grep "
57 end
58 local line = string.gsub(lines[1], "<", [[<lt>]])
59 return ":Grep " .. line
60end, { silent = false, expr = true })
61
62-- find --
63
64vim.o.findfunc = "v:lua.vim.g.findfunc"
65---@type fun(cmdarg: string, cmdcomplete: boolean): string[]
66vim.g.findfunc = function(cmdarg, _)
67 if not vim.g.findprg then
68 return {}
69 end
70 local farg = string.format("'%s'", cmdarg)
71 local findcmd, n = vim.g.findprg:gsub("%$%*", farg)
72 if n == 0 then
73 findcmd = findcmd .. " " .. farg
74 end
75 local fn = function(o)
76 local src = o.stderr
77 if o.code == 0 then
78 src = o.stdout
79 end
80 src = src
81 local lines = vim.split(src, "\n", { trimempty = true })
82 return lines
83 end
84 return fn(vim
85 .system({ vim.o.shell, "-c", findcmd }, {
86 text = true,
87 })
88 :wait())
89end