diff options
| author | Thomas Vanbesien <tvanbesi@proton.me> | 2026-06-07 14:20:06 +0200 |
|---|---|---|
| committer | Thomas Vanbesien <tvanbesi@proton.me> | 2026-06-07 17:26:19 +0200 |
| commit | 54a6c0a2bfe883f70704a3010a82c58c4c9e1ce6 (patch) | |
| tree | 334fbc8c4d7abd8d62542207a845df9630a5e1ba /.config/nvim | |
| parent | a189faf7411549d27ba9e7218a0c86347197c6cd (diff) | |
| download | dotfiles-54a6c0a2bfe883f70704a3010a82c58c4c9e1ce6.tar.gz dotfiles-54a6c0a2bfe883f70704a3010a82c58c4c9e1ce6.zip | |
feat(nvim): add transform_text plugin
With mappings to URL-encode/decode visual selection.
Diffstat (limited to '.config/nvim')
| -rw-r--r-- | .config/nvim/plugin/50-transform_text.lua | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/.config/nvim/plugin/50-transform_text.lua b/.config/nvim/plugin/50-transform_text.lua new file mode 100644 index 0000000..9ec74d5 --- /dev/null +++ b/.config/nvim/plugin/50-transform_text.lua @@ -0,0 +1,25 @@ +local function get_visual_selection_text() + return table.concat(vim.fn.getregion(vim.fn.getpos("v"), vim.fn.getpos("."), { type = vim.fn.mode() }), "\n") +end + +local function delete_visual_selection() + local mode = vim.fn.mode() + local eovisual_pos, cur_pos = vim.fn.getpos("v"), vim.fn.getpos(".") + local region = vim.fn.getregionpos(eovisual_pos, cur_pos, { type = mode })[1] + local start_row, start_col = unpack(region[1], 2, 3) + local end_row, end_col = mode == "v" and region[2][2] or math.max(eovisual_pos[2], cur_pos[2]), region[2][3] + vim.api.nvim_buf_set_text(0, start_row - 1, start_col - 1, end_row - 1, end_col, {}) +end + +local function transform_visual_selection(transform) + local selection = get_visual_selection_text() + delete_visual_selection() + vim.api.nvim_put({ transform(selection) }, "c", true, true) +end + +vim.keymap.set("x", "<Leader>tue", function() + transform_visual_selection(vim.uri_encode) +end, { desc = "URL-encode selection" }) +vim.keymap.set("x", "<Leader>tud", function() + transform_visual_selection(vim.uri_decode) +end, { desc = "URL-decode selection" }) |
