Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
matu3ba opened this issue Jan 28, 2023 · 0 comments

Comments

@matu3ba
Copy link

matu3ba commented Jan 28, 2023

-- 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?

@matu3ba matu3ba changed the title all the simple ways of "keep pasting" (without cursor movement, word replace, memset, memset + trunace until EOL) all the simple ways of "keep pasting" (without cursor movement, word replace, memset, memset + truncate until EOL) Jan 28, 2023
@matu3ba matu3ba changed the title all the simple ways of "keep pasting" (without cursor movement, word replace, memset, memset + truncate until EOL) all the simple ways of "keep pasting" (without cursor movement, word replace, strcpy, strcpy + truncate until EOL) Jan 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant