aserowy / tmux.nvim

tmux integration for nvim features pane movement and resizing from within nvim.
MIT License
642 stars 40 forks source link

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

Closed kiyoon closed 1 year ago

kiyoon commented 1 year ago

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?

aserowy commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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"
aserowy commented 1 year ago

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

catgoose commented 1 year ago

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 commented 3 months ago

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 https://github.com/folke/which-key.nvim/issues/743#issuecomment-2234460129 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 commented 3 months ago

Thus, ill let it pinned.