How could I change each task like so below, from any checkbox to any checkbox with hotkeys set for each type (x, -, /, and blank [ ]) with the cursor anywhere on the line?
A long time ago when I was learning Lua I came up with this custom command, it may not be the most sophisticated code but it works and I haven’t touched it in a year. I does not include [-] and [/] but you can use this as a reference.
local function isMdItemChecked()
local line = vim.fn.getline(".")
return string.match(line, "^%s*%-%s%[%s*[xX]%s*%]")
end
local function isMdItemUnchecked()
local line = vim.fn.getline(".")
return string.match(line, "^%s*%-%s%[%s%]")
end
local function isMdItem()
return isMdItemUnchecked() or isMdItemChecked()
end
local function mdCompleteItem()
-- Execute the series of keys: 02f\srx
vim.api.nvim_feedkeys("0f[lrx2wv", "n", true)
vim.api.nvim_feedkeys("$gsa~", "v", true)
vim.api.nvim_feedkeys("v", "n", true)
vim.api.nvim_feedkeys("i~gsa~", "v", true)
end
local function mdUncompleteItem()
local line = vim.fn.getline(".")
local modified_line = string.gsub(line, "~~", "")
modified_line = string.gsub(modified_line, "%-%s*%[%s*[xX]%s*%]", "- [ ]")
-- Update the current line with the modified one
vim.fn.setline(".", modified_line)
end
function MdToggleItem()
if not isMdItem() then
-- Send an info message to the user
vim.api.nvim_out_write("Not a markdown list item\n")
return
end
if isMdItemChecked() then
mdUncompleteItem()
else
mdCompleteItem()
end
end
vim.cmd("command! -nargs=0 MdToggleItem lua MdToggleItem()")