From 9ad2658f5e92ece67cb0d8ba8909830ab67155d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Wiberg?= Date: Thu, 16 Apr 2026 08:16:24 +0200 Subject: [PATCH] feat: add file URL commands (no line range) Add GetGithubBranchFileUrl and GetGithubFilePermalink commands for copying GitHub URLs for the current file without a line range. Useful for sharing file links in normal mode. --- README.md | 27 ++++++++++++++++++++++----- lua/getgithublink/init.lua | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 6af23cf..2e256ac 100644 --- a/README.md +++ b/README.md @@ -6,11 +6,11 @@ https://github.com/user-attachments/assets/dbfdcd9f-bf11-46da-903f-3f46d9893d4e ## Features -- Generate GitHub URLs for selected code +- Generate GitHub URLs for selected lines (visual mode) +- Generate GitHub URLs for the current file (normal mode) - Support for both branch-based URLs and permalinks (commit-based) - Automatic clipboard copying - Supports both SSH and HTTPS git remote URLs -- Visual mode commands ## Installation @@ -27,23 +27,40 @@ Using [lazy.nvim](https://github.com/folke/lazy.nvim): ## Usage -The plugin provides two main commands: +The plugin provides four commands: + +### Line selection (visual mode) - `:GetGithubUrl` - Generate a branch-based URL for the selected lines - `:GetGithubPermalink` - Generate a permanent (commit-based) URL for the selected lines +### File URL (normal mode) + +- `:GetGithubBranchFileUrl` - Generate a branch-based URL for the current file +- `:GetGithubFilePermalink` - Generate a permanent (commit-based) URL for the current file + ### Examples -Branch-based URL: +Branch-based URL (lines): ``` https://github.com/username/repo/blob/main/file.txt#L1-L5 ``` -Permalink: +Permalink (lines): ``` https://github.com/username/repo/blob/a1b2c3d4e5f6g7h8i9j0/file.txt#L1-L5 ``` +Branch-based file URL: +``` +https://github.com/username/repo/blob/main/file.txt +``` + +File permalink: +``` +https://github.com/username/repo/blob/a1b2c3d4e5f6g7h8i9j0/file.txt +``` + ## Configuration The plugin works out of the box with default settings. diff --git a/lua/getgithublink/init.lua b/lua/getgithublink/init.lua index aacc7f8..015f4c7 100644 --- a/lua/getgithublink/init.lua +++ b/lua/getgithublink/init.lua @@ -74,6 +74,27 @@ function M.get_github_url(use_permalink) print("GitHub URL copied to clipboard: " .. github_url) end +function M.get_github_file_url(use_permalink) + local repo_url = get_git_remote_url() + local relative_path = get_relative_path() + local ref = use_permalink and get_current_commit() or get_current_branch() + + if not repo_url or not relative_path or not ref then + print("Error: Could not get repository information") + return + end + + local github_url = string.format( + "%s/blob/%s/%s", + repo_url, + ref, + relative_path + ) + + vim.fn.setreg('+', github_url) + print("GitHub URL copied to clipboard: " .. github_url) +end + function M.setup() vim.api.nvim_create_user_command('GetGithubUrl', function() M.get_github_url(false) @@ -89,6 +110,18 @@ function M.setup() desc = 'Get GitHub permalink for selected lines' }) + vim.api.nvim_create_user_command('GetGithubBranchFileUrl', function() + M.get_github_file_url(false) + end, { + desc = 'Get GitHub file URL using current branch' + }) + + vim.api.nvim_create_user_command('GetGithubFilePermalink', function() + M.get_github_file_url(true) + end, { + desc = 'Get GitHub file permalink using commit hash' + }) + end return M