ThePrimeagen/init.lua

all the simple ways of "keep pasting" (without cursor movement, word replace, strcpy, strcpy + truncate until EOL)

matu3ba opened this issue · 0 comments

-- my_utils.lua
local M = {}

-- starting at cursor: overwrite text to right with register content
-- keep_suffix defines, if remaining line suffix should be kept
M.pasteOverwriteFromRegister = function(register, keep_suffix)
  local line_content = vim.api.nvim_get_current_line()
  local reg_content = vim.fn.getreg(register)
  local tup_rowcol = vim.api.nvim_win_get_cursor(0) -- [1],[2] = y,x = row,col
  local col_nr = tup_rowcol[2] -- 0 indexed => use +1
  local col = col_nr + 1
  local reg_len = string.len(reg_content)
  local line_len = string.len(line_content)
  local prefix = string.sub(line_content, 1, col-1) -- until before cursor
  local suffix = string.sub(line_content, col+reg_len, line_len) -- starting at cursor
  if keep_suffix == true then
    vim.api.nvim_set_current_line(prefix .. reg_content .. suffix)
  else
    vim.api.nvim_set_current_line(prefix .. reg_content)
  end
end

return M
-- my_keymaps.lua
-- suggested keymapings
local opts = { noremap = true, silent = true }
local map = vim.api.nvim_set_keymap

map('n', '<leader>p', [[<cmd> lua require("my_utils").pasteOverwriteFromRegister('+', true)<CR>]], opts)
map('n', '<leader>P', [[<cmd> lua require("my_utils").pasteOverwriteFromRegister('+', false)<CR>]], opts) -- replacement for broken [[v$P]]
map('n', '<C-p>', 'p`[', opts) -- ] paste without cursor movement
map('n', ',', [[viwP]], opts) -- keep pasting over the same thing for current word, simple instead of broken for EOL [["_diwP]]

Opinions?