Last active
May 30, 2025 18:20
-
-
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 '|'
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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