Git-Link for NeoVim allows you to quickly navigate from a line of code in Neovim to the corresponding line in a browser, assuming your repository's remote is hosted on platforms like GitHub, GitLab, etc. This plugin is especially useful when you need to share a link to a specific line of code (or line range) with your colleagues.
- Get a sharable URL: Easily generate a link to the line of code under your cursor, ready to share. You can also share a link to a line range, if you select a visual block.
- Open in Browser: Instantly open the browser and navigate to the specific line of code under your cursor.
- Permalinks: Copy or open a SHA-pinned link when your current
HEADis present on the remote. - Configurable URL Rules: Support for custom Git hosting platforms through configurable URL rewriting rules.
This plugin does not set any default keymaps, however, in the following examples we set these:
<leader>gu: Copies the URL of the line of code under your cursor to your clipboard.<leader>go: Opens the browser and navigates directly to the line of code under your cursor.<leader>gp: Copies a permalink for the line of code under your cursor to your clipboard.<leader>gP: Opens a permalink for the line of code under your cursor in your browser.
Below are examples for different setups:
{
"juacker/git-link.nvim",
keys = {
{
"<leader>gu",
function() require("git-link.main").copy_line_url() end,
desc = "Copy code link to clipboard",
mode = { "n", "x" }
},
{
"<leader>go",
function() require("git-link.main").open_line_url() end,
desc = "Open code link in browser",
mode = { "n", "x" }
},
{
"<leader>gp",
function() require("git-link.main").copy_permalink() end,
desc = "Copy code permalink to clipboard",
mode = { "n", "x" }
},
{
"<leader>gP",
function() require("git-link.main").open_permalink() end,
desc = "Open code permalink in browser",
mode = { "n", "x" }
},
},
}use {
'juacker/git-link.nvim',
config = function()
-- Set up your keymaps
vim.keymap.set('n', '<leader>gu', function() require("git-link.main").copy_line_url() end)
vim.keymap.set('n', '<leader>go', function() require("git-link.main").open_line_url() end)
vim.keymap.set('n', '<leader>gp', function() require("git-link.main").copy_permalink() end)
vim.keymap.set('n', '<leader>gP', function() require("git-link.main").open_permalink() end)
vim.keymap.set('x', '<leader>gu', function() require("git-link.main").copy_line_url() end)
vim.keymap.set('x', '<leader>go', function() require("git-link.main").open_line_url() end)
vim.keymap.set('x', '<leader>gp', function() require("git-link.main").copy_permalink() end)
vim.keymap.set('x', '<leader>gP', function() require("git-link.main").open_permalink() end)
end
}-- In your init.lua
vim.keymap.set('n', '<leader>gu', function() require("git-link.main").copy_line_url() end)
vim.keymap.set('n', '<leader>go', function() require("git-link.main").open_line_url() end)
vim.keymap.set('n', '<leader>gp', function() require("git-link.main").copy_permalink() end)
vim.keymap.set('n', '<leader>gP', function() require("git-link.main").open_permalink() end)
vim.keymap.set('x', '<leader>gu', function() require("git-link.main").copy_line_url() end)
vim.keymap.set('x', '<leader>go', function() require("git-link.main").open_line_url() end)
vim.keymap.set('x', '<leader>gp', function() require("git-link.main").copy_permalink() end)
vim.keymap.set('x', '<leader>gP', function() require("git-link.main").open_permalink() end)copy_line_url() and open_line_url() use a branch name from origin. If your local branch has unpushed commits, the plugin chooses the closest branch on origin that shares history with HEAD, preferring the current branch's upstream, then origin/HEAD, then any other matching remote branch.
copy_permalink() and open_permalink() use the full HEAD SHA. They only work when HEAD is present on origin; otherwise the plugin reports an error instead of creating a link to a commit the remote may not resolve.
The plugin comes with built-in support for the following Git remote URL formats:
- HTTPS URLs (e.g.,
https://github.com/user/repo) - Git SSH URLs (e.g.,
git@github.com:user/repo) - SSH protocol URLs (e.g.,
ssh://git@github.com/user/repo)
These URLs are automatically converted to browser-friendly formats for GitHub, GitLab, and similar platforms.
You can add custom URL rules to support different Git hosting platforms. Each rule consists of:
pattern: A Lua pattern to match your Git remote URL formatreplace: A template to convert the URL to HTTPS formatformat_url: A function that generates the final browser URL using:base_url: The resulting URL after replacing the patternparams: A table containing:branch: Selected branch name, or the commit SHA for permalink callsref: Same value asbranchpermalink: Boolean indicating whether the caller requested a permalinkfile_path: Path to the filestart_line: Starting line numberend_line: Ending line number (same as start_line for single line links)
Custom rules are processed before default rules, allowing you to override the default behavior for specific repositories.
Here is an example of configuration with a custom URL rule:
{
"juacker/git-link.nvim",
opts = {
-- Optional: Add custom URL rules for your Git hosting platform
url_rules = {
-- Example rule for Gerrit (might need adjustment for your Gerrit instance)
{
pattern = "^ssh://([^:]+):29418/(.+)$",
replace = "https://%1/plugins/gitiles/%2",
format_url = function(base_url, params)
-- For single line
if params.start_line == params.end_line then
-- Generates Gerrit Gitiles URLs: https://host/plugins/gitiles/project/+/refs/heads/branch/path#number
return string.format("%s/+/refs/heads/%s/%s#%d", base_url, params.branch, params.file_path, params.start_line)
else
-- For line ranges - Gerrit uses comma to separate line ranges
return string.format("%s/+/refs/heads/%s/%s#%d,%d", base_url, params.branch, params.file_path, params.start_line, params.end_line)
end
end,
},
},
},
keys = {
{
"<leader>gu",
function() require("git-link.main").copy_line_url() end,
desc = "Copy code link to clipboard",
mode = { "n", "x" }
},
{
"<leader>go",
function() require("git-link.main").open_line_url() end,
desc = "Open code link in browser",
mode = { "n", "x" }
},
},
}- Place the cursor on the desired line of code or select the visual block you want to share.
- Use your configured keybinding to copy the link URL to your clipboard.
- Alternatively, use your configured keybinding to open the link directly in your browser.