diff --git a/lua/copy_to_clipboard_fix.lua b/lua/copy_to_clipboard_fix.lua index 92d3668..9bcd412 100644 --- a/lua/copy_to_clipboard_fix.lua +++ b/lua/copy_to_clipboard_fix.lua @@ -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 diff --git a/lua/keymaps.lua b/lua/keymaps.lua index 92ee51d..cf5dbfb 100644 --- a/lua/keymaps.lua +++ b/lua/keymaps.lua @@ -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:lua require("copy_to_clipboard_fix").trim_clipboard()]]) +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() diff --git a/lua/yank_notification.lua b/lua/yank_notification.lua index 08ce1ee..2071657 100644 --- a/lua/yank_notification.lua +++ b/lua/yank_notification.lua @@ -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, })