Skip to content

Instantly share code, notes, and snippets.

@eduardoarandah
Last active May 30, 2025 18:20
Show Gist options
  • Save eduardoarandah/d26e95bcad973080e315d26c542ded0d to your computer and use it in GitHub Desktop.
Save eduardoarandah/d26e95bcad973080e315d26c542ded0d to your computer and use it in GitHub Desktop.
Rept command, replace pattern with placeholders in a line with strings split by pipe '|'
-- ReplaceTemplate replaces each '&' in the given template string
-- with values from the current line, split by the '|' character.
--
-- a|b|c
-- usage: :Rept Hello & wonderful & world &
-- result: Hello a wonderful b world c
function ReplaceTemplate(template)
-- Get the current line where the cursor is
local line = vim.api.nvim_get_current_line()
-- Split the line by pipe '|'
local items = vim.split(line, "|", true)
-- Copy the template to start substitutions
local result = template
local index = 1
-- Replace each '@' in order with trimmed items from the split line
while result:find("&", 1, true) and index <= #items do
result = result:gsub("&", vim.trim(items[index]), 1)
index = index + 1
end
-- Replace the current line with the result
vim.api.nvim_set_current_line(result)
end
vim.api.nvim_create_user_command("Rept", function(opts)
ReplaceTemplate(opts.args)
end, { nargs = 1 })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment