-- ToggleComment vim.api.nvim_create_user_command("ToggleComment", function() local comments = { bash = "#", c = "//", cpp = "//", editorconfig = "#", fish = "#", gdscript = "#", gdshader = "//", gitignore = "#", glsl = "//", go = "//", hyprlang = "#", javascript = "//", jsonc = "//", lua = "--", rust = "//", sh = "#", tmux = "#", toml = "#", typescript = "//", yaml = "#", zig = "//", } local comment = comments[vim.bo.filetype] if comment == nil then return end local is_visual = vim.fn.mode():match("[vV]") local start_line local end_line if is_visual then start_line = vim.fn.line("v") end_line = vim.fn.line(".") else start_line = vim.fn.line(".") end_line = start_line end if start_line == 0 or end_line == 0 then vim.notify("No visual selection found. Please make a selection first.", vim.log.levels.WARN) return end if start_line > end_line then start_line, end_line = end_line, start_line end local lines = vim.api.nvim_buf_get_lines(0, start_line - 1, end_line, false) for i, line in ipairs(lines) do local indent, rest = line:match("^(%s*)(.*)") if vim.startswith(rest, comment) then rest = rest:sub(#comment + 1):gsub("^%s*", "") else rest = comment .. " " .. rest end lines[i] = indent .. rest end vim.api.nvim_buf_set_lines(0, start_line - 1, end_line, false, lines) end, {}) -- ToggleWord vim.api.nvim_create_user_command("ToggleWord", function() local inverse = { ["bottom"] = "top", ["horizontal"] = "vertical", ["left"] = "right", ["on"] = "off", ["true"] = "false", ["True"] = "False", ["TRUE"] = "FALSE", ["yes"] = "no", ["Yes"] = "No", ["YES"] = "NO", ["y"] = "n", ["1"] = "0", ["<"] = ">", ["("] = ")", ["["] = "]", ["{"] = "}", ['"'] = "'", ['""'] = "''", ["+"] = "-", ["==="] = "!==", ["=="] = "!=", } for key, value in pairs(inverse) do inverse[value] = key end vim.cmd("normal! yiw") local yanked = vim.fn.getreg('"') local flipped = inverse[yanked] if flipped == nil then return end vim.cmd("normal! ciw" .. flipped) end, {})