Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand Down
33 changes: 33 additions & 0 deletions lua/getgithublink/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down