Skip to content

Example mappings

Zachary Thomas edited this page Jan 21, 2023 · 7 revisions

Sorting

local function sort(lines, opts)
  -- We don't care about anything non alphanumeric here
  local sort_without_leading_space = function(a, b)
    -- true = a then b
    -- false = b then a
    local pattern = [[^%W*]]
    return string.gsub(a, pattern, "") < string.gsub(b, pattern, "")
  end
  if #lines == 1 then
    -- If only looking at 1 line, sort that line split by some char gotten from imput
    local delimeter = utils.get_input("Delimeter: ")
    local split = vim.split(lines[1], delimeter, { trimempty = true })
    -- Remember! `table.sort` mutates the table itself
    table.sort(split, sort_without_leading_space)
    return { utils.join(split, delimeter) }
  else
    -- If there are many lines, sort the lines themselves
    table.sort(lines, sort_without_leading_space)
    return lines
  end
end

require("yop").op_map({ "n", "v" }, "gs", sort)

Searching with Telescope

local function search(lines)
  -- Multiple lines can't be searched for
  if #lines > 1 then
    return
  end
  require("telescope.builtin").grep_string({ search = lines[1] })
end

require("yop").op_map({ "n", "v" }, "<leader>s", search)

Adding a Markdown style header

local function heading(lines)
	if lines[1]:sub(1, 1) == "#" then
		lines[1] = "#" .. lines[1]
	else
		lines[1] = "# " .. lines[1]
	end
	return lines
end

require("yop").op_map("n", "<leader>#", heading, { linewise = true })

Search help docs with a mapping

local function help(lines)
	if #lines > 1 then
		return
	end
	vim.cmd(":h " .. lines[1])
end

require("yop").op_map({ "n", "v" }, "K", help)
Clone this wiki locally