-- just custom, self-written experimental stuff for nvim. vim.g.clipboard = 'osc52' local augroup = vim.api.nvim_create_augroup local autocmd = vim.api.nvim_create_autocmd autocmd({'BufRead', 'BufNewFile'}, { pattern = '{neomutt-*,*.eml}', command = "setlocal tw=72 colorcolumn=72 fo=awq comments+=nb:> spell".. " | match ErrorMsg '\\s\\+$'" }) -- buflist to quickfix list vim.keymap.set('n', 'bl', function() local qf_list = {} for _, buf in ipairs(vim.fn.getbufinfo()) do if buf.listed == 1 then table.insert(qf_list, { filename = buf.name ~= '' and buf.name or '[No Name]', text = ':' .. buf.bufnr }) end end vim.fn.setqflist(qf_list, 'r') vim.cmd('copen') end, {}) -- mom, can we have telescope? no, we have telescope at home. telescope at home: local function run_search(cmd) local output = vim.fn.systemlist(cmd) vim.cmd("enew") vim.api.nvim_buf_set_lines(0, 0, -1, false, output) vim.bo.buftype = "nofile" vim.bo.bufhidden = "wipe" vim.bo.swapfile = false end vim.api.nvim_create_user_command("FileSearch", function(opts) local path = opts.bang and vim.fn.expand("%:p:h") or vim.fn.getcwd() local excludes = "-path '*.egg-info*' -prune -o -path '*.git*' -prune -o -path '*__pycache__*' -prune -o" if not opts.bang then excludes = excludes .. " -path '*.venv*' -prune -o" excludes = excludes .. " -path '" .. vim.fn.getcwd() .. "/target*'" .. " -prune -o" end run_search("find " .. vim.fn.shellescape(path) .. " " .. excludes .. " " .. " -name " .. "'*" .. opts.args .. "*' -print") end, { nargs = "+", bang = true }) vim.api.nvim_create_user_command("GrepTextSearch", function(opts) local path = opts.bang and vim.fn.expand("%:p:h") or vim.fn.getcwd() local excludes = "--exclude-dir='*target*' --exclude-dir=.git --exclude-dir='*.egg-info' --exclude-dir='__pycache__'" if not opts.bang then -- cwd search should only look at project files. excludes = excludes .. " --exclude-dir=.venv" end run_search("grep -IEnr " .. excludes .. " '" .. opts.args .. "' " .. path) end, { nargs = "+", bang = true }) vim.api.nvim_create_user_command("TextSearch", function(opts) local path = opts.bang and vim.fn.expand("%:p:h") or vim.fn.getcwd() local excludes = "--glob '!**/target/**' --glob '!.git/**' --glob '!**/*.egg-info/**' --glob '!**/__pycache__/**'" if not opts.bang then excludes = excludes .. " --glob '!**/.venv/**'" else excludes = excludes .. " --no-ignore " end local cmd = "rg --vimgrep -i -n " .. excludes .. " '" .. opts.args .. "' " .. path run_search(cmd) end, { nargs = "+", bang = true }) vim.keymap.set("n", "q", scratch_to_quickfix) vim.keymap.set("n", "sf", function() vim.ui.input({ prompt = "> " }, function(name) if name then vim.cmd("FileSearch " .. name) end end) end) vim.keymap.set("n", "lf", function() vim.ui.input({ prompt = "> " }, function(name) if name then vim.cmd("FileSearch! " .. name) end end) end) vim.keymap.set("n", "sg", function() vim.ui.input({ prompt = "> " }, function(pattern) if pattern then vim.cmd("TextSearch " .. pattern) end end) end) vim.keymap.set("n", "lg", function() vim.ui.input({ prompt = "> " }, function(pattern) if pattern then vim.cmd("TextSearch! " .. pattern) end end) end) vim.keymap.set("n", "/", function() vim.ui.input({ prompt = "> " }, function(pattern) if not pattern or pattern == "" then return end run_search("grep -n '" .. pattern .. "' " .. vim.fn.shellescape(vim.api.nvim_buf_get_name(0))) end) end) ---- end of custom telescope --- in case I ever want to go back to telescope local pluginpath = vim.fn.stdpath("data") .. "/site/pack/plugins/start/" if not vim.loop.fs_stat(pluginpath) then vim.fn.system({ "git", "clone", "https://github.com/yobibyte/telescope.nvim", "--branch=0.1.x", pluginpath .. "telescope.nvim", }) vim.opt.rtp:append(pluginpath .. "telescope.nvim") vim.fn.system({ "git", "clone", "https://github.com/yobibyte/plenary.nvim", pluginpath .. "plenary.nvim", }) end vim.keymap.set("n", "sf", function() local config = { previewer = false, layout_strategy = "center", layout_config = { height = 0.4, }, } if vim.bo.filetype == "netrw" then config.cwd = vim.fn.expand("%:p:h") config.no_ignore = true end require("telescope.builtin").find_files(config) end) vim.keymap.set("n", "sg", function() local config = {} if vim.bo.filetype == "netrw" then config.search_dirs = {vim.b.netrw_curdir} config.additional_args = function() return { "--hidden", "--no-ignore" } end end require("telescope.builtin").live_grep(config) end) vim.keymap.set("n", "o", function() vim.cmd.edit(vim.fn.fnameescape(vim.fn.trim(vim.fn.getreg("+")))) end) vim.keymap.set("n", "", ":move .+1") vim.keymap.set("n", "", ":move .-2") vim.keymap.set("v", "", ":move '>+1gv") vim.keymap.set("v", "", ":move '<-2gv") vim.keymap.set("n", "gc", function() extc("git diff --name-only --diff-filter=U") qf() end) vim.keymap.set({ "n", "x" }, "/", function() local cs = vim.bo.commentstring:match("^(.*)%%s") if not cs or cs == "" then return end local s_row, e_row = vim.fn.line("."), vim.fn.line(".") if vim.fn.mode() ~= "n" then s_row, e_row = vim.fn.line("v"), vim.fn.line(".") if s_row > e_row then s_row, e_row = e_row, s_row end end local prefix = vim.trim(cs) local lines = {} local all_commented = true for i = s_row, e_row do local line = vim.fn.getline(i) local uncommented = line:gsub("^%s*" .. vim.pesc(prefix) .. "%s?", "", 1) if uncommented == line then all_commented = false end table.insert(lines, { i, line, uncommented }) end for _, entry in ipairs(lines) do local i, line, uncommented = unpack(entry) if all_commented then vim.fn.setline(i, uncommented) else vim.fn.setline(i, prefix .. " " .. line) end end end, { desc = "Toggle comment using commentstring" }) local ex = { ".git", "*.egg-info", "__pycache__", "wandb", "target", ".venv" } if vim.fn.executable("rg") then local exc = table.concat( vim.tbl_map(function(dir) return "--glob='!" .. dir .. "/**'" end, ex), " ") vim.opt.grepprg = "rg --vimgrep --no-ignore-parent -in " .. exc .. " -- $*" else local exc = table.concat(vim.tbl_map(function(e) return "--exclude-dir='" .. e .. "'" end, ex), " ") vim.opt.grepprg = "grep -IEnr " .. exc .. " $* " .. vim.fn.getcwd() end vim.opt.grepprg = "rg --vimgrep --no-ignore-parent --glob='!.git/**' --glob='!*.egg-info/**' --glob='!__pycache__/**' --glob='!wandb/**' --glob='!target/**' --glob='!.venv/**' -- $*" vim.keymap.set("n", "c", function() vim.ui.input({}, function(c) if c and c~="" then vim.cmd("noswapfile vnew | setlocal buftype=nofile bufhidden=wipe") vim.api.nvim_buf_set_lines(0, 0, -1, false, vim.fn.systemlist(c)) end end) end) function _G.f(cmd_arg) return vim.fn.systemlist("rg --files | rg " .. cmd_arg) end vim.o.findfunc = 'v:lua.f' vim.cmd("syntax off | highlight Normal guifg=#ffaf00 guibg=#282828") vim.cmd("syntax off | highlight Normal guibg=#3b224c guifg=#a4a0e8") require('vim._extui').enable({ enable = true, -- Whether to enable or disable the UI. msg = { -- Options related to the message module. ---@type 'cmd'|'msg' Where to place regular messages, either in the ---cmdline or in a separate ephemeral message window. target = 'msg', timeout = 4000, -- Time a message is visible in the message window. }, }) vim.keymap.set('n', 'y', "let @+ = expand('%:p')") vim.keymap.set("n", "c", function() vim.cmd("nos ene | setl bt=nofile bh=wipe") vim.cmd("r !" .. vim.fn.input("")) vim.cmd("1d") end)