openingh.nvim icon indicating copy to clipboard operation
openingh.nvim copied to clipboard

It's possible to copy the permalink directly?

Open guilhermeprokisch opened this issue 2 years ago • 5 comments

I wondered if copying the permalink directly from the plugin is too complicated since the OpenInGHLines already highlights the correct lines.

guilhermeprokisch avatar Oct 13 '23 12:10 guilhermeprokisch

I think this should be possible to implement using the following lua code:

vim.fn.setreg('+', url)

knpwrs avatar Jan 30 '24 16:01 knpwrs

Actually, reading the README, this appears to already be implemented:

All of the commands above optionally take a register, e.g. :OpenInGHFileLines+. In this case, the URL will not be opened in the browser, but put into the register given. This is especially useful if you're running neovim on a remote machine, but want to open the URL locally.

knpwrs avatar Jan 30 '24 17:01 knpwrs

The readme does seem to suggest it should work, but when I do that, it still opens the link in the browser?

samuelhwilliams avatar Apr 03 '24 15:04 samuelhwilliams

OpenInGHFileLines+ doesn't work for me neither. A GitHub search suggests that it should work though :shrug: .

rollf avatar Jun 17 '24 09:06 rollf

It isn't mentioned in the README.md but in the help doc the author writes:

To copy the URL into a register set vim.g.openingh_copy_to_register = true (helpdoc)

This needs to be set before the plugin is loaded, since it's used when the plugins' User Commands are being created (code), not when the user command is called (this is just how the nvim_create_user_command API works, ~although I feel like the plugin should just always set the reg option to true, since it can be opted out of when being called by just not passing a register~[1]*)

anyways, with that said, here's a working config you can use:

return {
  'Almo7aya/openingh.nvim',
  init = function()
    vim.g.openingh_copy_to_register = true
  end,
  config = function()
    -- ...

    vim.keymap.set('v', '<space>ygh', ':OpenInGHFileLines+<cr>', {
      silent = true,
      noremap = true,
      desc = 'Yank GitHub link to system clipboard',
    })
  end,
}

If you want to copy a permalink, you need to specify both the bang (!, to prioritize the commit sha, as described in the readme) and then pass the register as the first argument, i.e. :OpenInGHFileLines! + (note the space between ! and +)

e.g.

vim.keymap.set('v', '<space>ygh', ':OpenInGHFileLines! +<cr>', {
  silent = true,
  noremap = true,
  desc = 'Yank GitHub permalink to system clipboard',
})

[1]*: Actually this changes how the command interprets the first argument, and so if you frequently pass an explicit branch name to the commands then you might not want this to be set to true

rperryng avatar Jul 07 '24 18:07 rperryng