Fix: Yank notifications all over copy to clipboard notifcations

This commit is contained in:
psychhim
2025-10-09 04:12:59 +05:30
parent 009b3aa8c0
commit 5fdc7ee5b6
3 changed files with 25 additions and 14 deletions

View File

@@ -1,17 +1,22 @@
local M = {}
function M.trim_clipboard()
-- Get the content of the '+' register
local content = vim.fn.getreg '+'
-- Remove single trailing newline if present
-- Get content from default register
local content = vim.fn.getreg '"'
if not content or content == '' then
return
end
-- Remove trailing newline
content = content:gsub('\n$', '')
-- Update '+' and '*' registers
-- Copy to system clipboard
vim.fn.setreg('+', content)
vim.fn.setreg('*', content)
-- Count the number of lines copied
local line_count = select(2, content:gsub('\n', '')) + 1 -- Number of newlines + 1
-- Notification with line count
-- Count lines
local line_count = select(2, content:gsub('\n', '')) + 1
local plural = line_count == 1 and '' or 's'
vim.notify(string.format('Copied %d line%s to clipboard', line_count, plural), vim.log.levels.INFO)
-- Delay clipboard notification slightly to appear after yank notification
vim.defer_fn(function()
vim.notify(string.format('Copied %d line%s to clipboard', line_count, plural), vim.log.levels.INFO, { title = 'Clipboard' })
end, 50) -- 50ms delay
end
return M

View File

@@ -161,7 +161,10 @@ vim.keymap.set('n', 'Y', function()
end)
end)
-- Visual mode: copy selection, trim trailing newline if needed
vim.keymap.set('x', 'Y', [["+y<esc>:lua require("copy_to_clipboard_fix").trim_clipboard()<CR>]])
vim.keymap.set('x', 'Y', function()
vim.cmd 'normal! y'
require('copy_to_clipboard_fix').trim_clipboard()
end, { noremap = true, silent = true })
-- [[ Paste from clipboard with line count ]]
local function paste_from_clipboard()

View File

@@ -1,21 +1,24 @@
local M = {}
vim.api.nvim_create_autocmd('TextYankPost', {
pattern = '*',
callback = function()
local content = vim.fn.getreg '"'
callback = function(ev)
local regname = ev.regname or '"'
-- Ignore clipboard yanks
if regname == '+' or regname == '*' then
return
end
local content = vim.fn.getreg(regname)
if not content or content == '' then
return
end
-- Split by newline
local lines = vim.split(content, '\n', true)
-- Remove trailing empty line if present
if lines[#lines] == '' then
table.remove(lines, #lines)
end
local line_count = #lines
vim.schedule(function()
local plural = line_count == 1 and '' or 's'
vim.notify(string.format('Yanked %d line%s', line_count, plural), vim.log.levels.INFO)
vim.notify(string.format('Yanked %d line%s', line_count, plural), vim.log.levels.INFO, { title = 'Yank' })
end)
end,
})