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

Compatibility with vim-yoink or other yank-related plugins #88

Closed
kiyoon opened this issue Jan 4, 2023 · 8 comments
Closed

Compatibility with vim-yoink or other yank-related plugins #88

kiyoon opened this issue Jan 4, 2023 · 8 comments
Labels
help wanted Extra attention is needed question Further information is requested

Comments

@kiyoon
Copy link

kiyoon commented Jan 4, 2023

Hi, thanks for the wonderful plugin.

I may not be aware of the details of this implementation so my apologies if this is impossible.

There are some plugins that overrides y and p to perform custom yanking, like
vim-cutlass and vim-yoink.
I found a problem that in this way you can't use these plugins together.

I saw that there is an autocmd for text yanking.
Here's the minimal example:

vim.api.nvim_create_autocmd({ "TextYankPost" }, {
	callback = function()
		vim.highlight.on_yank({ higroup = "Visual", timeout = 200 })
	end,
})

Perhaps you can use such autocmds to perform buffer syncing rather than remapping y key, and if all plugin developers do that it should be possible to override multiple functionalities related to yanks.

What do you think?

EDIT:

Sorry, it seems like you're already doing this. I don't know why vim-yoink stopped working with this plugin installed, then. Any idea to make both work together?

@kiyoon kiyoon changed the title Autocmds rather than overriding key bindings? Compatibility with vim-yoink or other yank-related plugins Jan 4, 2023
@aserowy aserowy added help wanted Extra attention is needed question Further information is requested labels Jan 14, 2023
@aserowy
Copy link
Owner

aserowy commented Jan 14, 2023

Heho,

unfortunately the plugin needs todo both because the sync from tmux has to run right before the paste takes place. There are no events from tmux buffer to know when new values arrive. If you find another solution im happy to try it.

I'll leave this issue open in case someone can contribute to this behaviour.

Kind regards
Alexander

@kiyoon
Copy link
Author

kiyoon commented Jan 15, 2023

Heho,

unfortunately the plugin needs todo both because the sync from tmux has to run right before the paste takes place. There are no events from tmux buffer to know when new values arrive. If you find another solution im happy to try it.

I'll leave this issue open in case someone can contribute to this behaviour.

Kind regards Alexander

I see, the problem was in pasting. It's a pity neovim doesn't have TextPastePre or something like that.. Thanks for your awesome work though!

@kiyoon
Copy link
Author

kiyoon commented Feb 9, 2023

Hi, is it maybe possible to add a configuration that allows custom paste function?

I'm trying to integrate yanky.nvim with this plugin together, and I think it can be possible if yanky paste function is called after sync_register.

I think it will be sick if it allows that. Thanks!

@kiyoon
Copy link
Author

kiyoon commented Feb 11, 2023

Finally made it work. I just had to remap all put keys so it uses all plugins together.

The configuration below integrates tmux.nvim with yanky.nvim and which-key.nvim so we get the benefits of all yank-related plugins.

From the dotfiles here

require("tmux").setup {
  copy_sync = {
    enable = true,
    sync_clipboard = false,
    sync_registers = true,
  },
  resize = {
    enable_default_keybindings = false,
  },
}

-- since we want to use sync_registers with yanky.nvim, we need to
-- configure keybindings manually.
local yanky = require "yanky"
yanky.setup {
  highlight = {
    on_put = true,
    on_yank = true,
    timer = 300,
  },
}
vim.keymap.set("n", "p", function()
  if vim.env.TMUX then
    require("tmux.copy").sync_registers()
  end
  yanky.put("p", false)
end)
vim.keymap.set("x", "p", function()
  if vim.env.TMUX then
    require("tmux.copy").sync_registers()
  end
  yanky.put("p", true)
end)
vim.keymap.set("n", "P", function()
  if vim.env.TMUX then
    require("tmux.copy").sync_registers()
  end
  yanky.put("P", false)
end)
vim.keymap.set("x", "P", function()
  if vim.env.TMUX then
    require("tmux.copy").sync_registers()
  end
  yanky.put("P", true)
end)

local status, which_key = pcall(require, "which-key")
if status then
  vim.keymap.set("n", [["]], function()
    if vim.env.TMUX then
      require("tmux.copy").sync_registers()
    end
    which_key.show('"', { mode = "n", auto = true })
  end)
  vim.keymap.set("x", [["]], function()
    if vim.env.TMUX then
      require("tmux.copy").sync_registers()
    end
    which_key.show('"', { mode = "v", auto = true })
  end)
end
vim.keymap.set("n", "<c-n>", "<Plug>(YankyCycleForward)")
vim.keymap.set("n", "<c-p>", "<Plug>(YankyCycleBackward)")
vim.keymap.set({ "n", "x" }, "y", "<Plug>(YankyYank)")
vim.keymap.set("n", "=p", "<Plug>(YankyPutAfterFilter)")
vim.keymap.set("n", "=P", "<Plug>(YankyPutBeforeFilter)")
require("telescope").load_extension "yank_history"

@kiyoon kiyoon closed this as completed Feb 11, 2023
@aserowy aserowy pinned this issue Mar 11, 2023
@aserowy
Copy link
Owner

aserowy commented Mar 11, 2023

nice! i pinned your solution to let others find it.

@catgoose
Copy link

catgoose commented Mar 14, 2023

I solved my problem with sharing clipboard by using the ojroques/nvim-osc52 plugin:

Using lazy.nvim.
This is my yanky.lua:

local opts = {
	ring = {
		history_length = 1000,
		storage = "shada",
		sync_with_numbered_registers = true,
	},
	system_clipboard = {
		sync_with_ring = true,
	},
	highlight = {
		on_put = true,
		on_yank = true,
		timer = 500,
	},
	preserve_cursor_position = {
		enabled = true,
	},
}

return {
	"gbprod/yanky.nvim",
	opts = opts,
	keys = {
		{ "y", "<Plug>(YankyYank)", mode = { "n", "x" } },
		{ "p", "<Plug>(YankyPutAfter)", mode = { "n", "x" } },
		{ "P", "<Plug>(YankyPutBefore)", mode = { "n", "x" } },
		{ "gP", "<Plug>(YankyGPutBefore)", mode = { "n", "x" } },
		{ "gp", "<Plug>(YankyGPutAfter)", mode = { "n", "x" } },
		{ "<c-p>", "<Plug>(YankyCycleForward)" },
		{ "<c-n>", "<Plug>(YankyCycleBackward)" },
		{ "<leader>pp", "<cmd>YankyRingHistory<cr>" },
	},
	event = "BufReadPre",
}

tmux.lua:

local opts = {
	copy_sync = {
		enable = false,
	},
	navigation = {
		cycle_navigation = true,
		enable_default_keybindings = true,
		persist_zoom = true,
	},
	resize = {
		enable_default_keybindings = false,
		resize_step_x = 1,
		resize_step_y = 1,
	},
}

return {
	"aserowy/tmux.nvim",
	event = "BufReadPre",
	opts = opts,
}

osc52.lua:

local api, v = vim.api, vim.v

local opts = {
	silent = true,
}

return {
	"ojroques/nvim-osc52",
	event = "BufReadPre",
	opts = opts,
	init = function()
		local au_copy = function()
			if v.event.operator == "y" and v.event.regname == "+" then
				require("osc52").copy_register("+")
			end
			if v.event.operator == "y" and v.event.regname == "" then
				require("osc52").copy_register("")
			end
		end
		api.nvim_create_autocmd("TextYankPost", { callback = au_copy })
	end,
}

I don't have to use special callbacks and my clipboard is shared between the host OS over ssh, neovim, and tmux.

@moetayuko
Copy link
Contributor

After recent refactoring of which-key.nvim 3.x, the which-key.nvim integration provided by @kiyoon is no longer applicable due to a bugfix.

Fortunately, folke provided a much easier approach at folke/which-key.nvim#743 (comment) which directly injects sync_registers() into the corresponding lua method and does not require messing with keymaps.

Similarly, I re-implemented yanky.nvim integration to eliminate the need to set keymaps. Below is my config for LazyVim:

return {
  {
    "aserowy/tmux.nvim",
    event = "VeryLazy",
    opts = {
      copy_sync = {
        enable = true,
        sync_registers_keymap_put = false, -- remove this line if you don't use yanky.nvim
        sync_registers_keymap_reg = false,
      },
    },
    config = function(_, opts)
      require("tmux").setup(opts)
      if vim.env.TMUX then
        LazyVim.on_load("which-key.nvim", function()
          local reg = require("which-key.plugins.registers")
          local expand = reg.expand
          function reg.expand()
            require("tmux.copy").sync_registers()
            return expand()
          end
        end)

        if LazyVim.has("yanky.nvim") then
          LazyVim.on_load("yanky.nvim", function()
            local yanky = require("yanky")
            local put = yanky.put
            function yanky.put(type, is_visual, callback)
              require("tmux.copy").sync_registers()
              return put(type, is_visual, callback)
            end
          end)
        end
      end
    end,
  },
}

@aserowy
Copy link
Owner

aserowy commented Jul 31, 2024

Thus, ill let it pinned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants