Skip to content
Merged
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
32 changes: 24 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ A Neovim plugin for easily copying file paths and GitHub URLs to clipboard.

- 📋 Copy current file relative path to clipboard
- 📍 Copy current file path with line number
- 🔗 Copy current file path as GitHub URL with line number
- 🔗 Copy current file path as GitHub URL (with branch or commit hash)
- 📍 Copy current file path as GitHub URL with line number (with branch or commit hash)
- ⚡ Simple and lightweight
- 🎨 Customizable keymaps

Expand Down Expand Up @@ -47,8 +48,11 @@ Default configuration:
require("cfp").setup({
keymaps = {
copy_path = "<leader>cp",
copy_path_line = "<leader>cl",
copy_path_url = "<leader>cu",
copy_path_line = "<leader>cP",
copy_branch_url = "<leader>cb",
copy_branch_url_line = "<leader>cB",
copy_hash_url = "<leader>ch",
copy_hash_url_line = "<leader>cH",
},
})
```
Expand All @@ -60,7 +64,10 @@ require("cfp").setup({
keymaps = {
copy_path = "<leader>cp",
copy_path_line = nil, -- disable this keymap
copy_path_url = "<leader>cu",
copy_branch_url = "<leader>cb",
copy_branch_url_line = nil, -- disable this keymap
copy_hash_url = "<leader>ch",
copy_hash_url_line = "<leader>cH",
},
})
```
Expand All @@ -73,21 +80,30 @@ require("cfp").setup({
|---------|-------------|
| `:CopyPath` | Copy current file relative path to clipboard |
| `:CopyPathLine` | Copy current file relative path with line number to clipboard |
| `:CopyPathURL` | Copy current file relative path with line number as GitHub URL to clipboard |
| `:CopyBranchURL` | Copy current file relative path as GitHub URL with branch to clipboard |
| `:CopyBranchURLLine` | Copy current file relative path with line number as GitHub URL with branch to clipboard |
| `:CopyHashURL` | Copy current file relative path as GitHub URL with commit hash to clipboard |
| `:CopyHashURLLine` | Copy current file relative path with line number as GitHub URL with commit hash to clipboard |

### Default Keymaps

| Keymap | Command | Description |
|--------|---------|-------------|
| `<leader>cp` | `:CopyPath` | Copy file path |
| `<leader>cl` | `:CopyPathLine` | Copy file path with line number |
| `<leader>cu` | `:CopyPathURL` | Copy file path as GitHub URL |
| `<leader>cP` | `:CopyPathLine` | Copy file path with line number |
| `<leader>cb` | `:CopyBranchURL` | Copy file path as GitHub URL with branch |
| `<leader>cB` | `:CopyBranchURLLine` | Copy file path as GitHub URL with branch and line number |
| `<leader>ch` | `:CopyHashURL` | Copy file path as GitHub URL with commit hash |
| `<leader>cH` | `:CopyHashURLLine` | Copy file path as GitHub URL with commit hash and line number |

## Example Output

- **CopyPath**: `lua/cfp/init.lua`
- **CopyPathLine**: `lua/cfp/init.lua:42`
- **CopyPathURL**: `https://github.com/tapihdev/cfp.nvim/blob/main/lua/cfp/init.lua#L42`
- **CopyBranchURL**: `https://github.com/tapihdev/cfp.nvim/blob/main/lua/cfp/init.lua`
- **CopyBranchURLLine**: `https://github.com/tapihdev/cfp.nvim/blob/main/lua/cfp/init.lua#L42`
- **CopyHashURL**: `https://github.com/tapihdev/cfp.nvim/blob/5e62bbb/lua/cfp/init.lua`
- **CopyHashURLLine**: `https://github.com/tapihdev/cfp.nvim/blob/5e62bbb/lua/cfp/init.lua#L42`

## License

Expand Down
11 changes: 6 additions & 5 deletions lua/cfp/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ local M = {}
local defaults = {
keymaps = {
copy_path = "<leader>cp",
copy_path_line = "<leader>cl",
copy_path_url = "<leader>cu",
copy_path_hash = "<leader>cw",
copy_path_with_hash = "<leader>cW",
copy_path_line = "<leader>cP",
copy_branch_url = "<leader>cb",
copy_branch_url_line = "<leader>cB",
copy_hash_url = "<leader>ch",
copy_hash_url_line = "<leader>cH",
},
}

Expand All @@ -20,4 +21,4 @@ function M.get()
return config
end

return M
return M
136 changes: 74 additions & 62 deletions lua/cfp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,54 @@ local function copy_to_clipboard(text)
vim.notify("Copied to clipboard: " .. text, vim.log.levels.INFO)
end

-- Copy relative path
-- Create GitHub URL for file
local function create_github_url(path, ref_type, include_line)
-- Get Git remote URL
local git_remote = vim.fn.system("git config --get remote.origin.url"):gsub("\n", "")
if vim.v.shell_error ~= 0 then
vim.notify("Not in a Git repository", vim.log.levels.WARN)
return nil
end

-- Convert SSH/HTTPS Git URL to GitHub URL
local github_url = git_remote
if github_url:match("^git@github.com:") then
github_url = github_url:gsub("^git@github.com:", "https://github.com/")
github_url = github_url:gsub("%.git$", "")
elseif github_url:match("^https://github.com/") then
github_url = github_url:gsub("%.git$", "")
else
vim.notify("Remote origin is not a GitHub repository", vim.log.levels.WARN)
return nil
end

-- Get reference (branch or commit hash)
local ref
if ref_type == "branch" then
ref = vim.fn.system("git rev-parse --abbrev-ref HEAD"):gsub("\n", "")
if vim.v.shell_error ~= 0 then
vim.notify("Failed to get current branch", vim.log.levels.WARN)
return nil
end
elseif ref_type == "hash" then
ref = vim.fn.system("git rev-parse HEAD"):gsub("\n", "")
if vim.v.shell_error ~= 0 then
vim.notify("Failed to get current commit hash", vim.log.levels.WARN)
return nil
end
end

-- Build URL
local url = github_url .. "/blob/" .. ref .. "/" .. path
if include_line then
local line_num = vim.api.nvim_win_get_cursor(0)[1]
url = url .. "#L" .. line_num
end

return url
end

-- Copy current file relative path to clipboard
function M.copy_path()
local path = get_relative_path()
if not path then
Expand All @@ -27,7 +74,7 @@ function M.copy_path()
copy_to_clipboard(path)
end

-- Copy relative path with line number
-- Copy current file relative path with line number to clipboard
function M.copy_path_line()
local path = get_relative_path()
if not path then
Expand All @@ -41,95 +88,60 @@ function M.copy_path_line()
copy_to_clipboard(path_with_line)
end

-- Copy relative path with line number as GitHub URL
function M.copy_path_url()
-- Copy current file relative path as GitHub URL with branch name to clipboard
function M.copy_branch_url()
local path = get_relative_path()
if not path then
vim.notify("No file path available", vim.log.levels.WARN)
return
end

-- Get Git remote URL
local git_remote = vim.fn.system("git config --get remote.origin.url"):gsub("\n", "")
if vim.v.shell_error ~= 0 then
vim.notify("Not in a Git repository", vim.log.levels.WARN)
return
end

-- Convert SSH/HTTPS Git URL to GitHub URL
local github_url = git_remote
if github_url:match("^git@github.com:") then
github_url = github_url:gsub("^git@github.com:", "https://github.com/")
github_url = github_url:gsub("%.git$", "")
elseif github_url:match("^https://github.com/") then
github_url = github_url:gsub("%.git$", "")
else
vim.notify("Remote origin is not a GitHub repository", vim.log.levels.WARN)
return
end

-- Get current branch
local branch = vim.fn.system("git rev-parse --abbrev-ref HEAD"):gsub("\n", "")
if vim.v.shell_error ~= 0 then
vim.notify("Failed to get current branch", vim.log.levels.WARN)
return
local url = create_github_url(path, "branch", false)
if url then
copy_to_clipboard(url)
end

local line_num = vim.api.nvim_win_get_cursor(0)[1]
local github_file_url = github_url .. "/blob/" .. branch .. "/" .. path .. "#L" .. line_num

copy_to_clipboard(github_file_url)
end

-- Copy relative path with line number as GitHub URL with commit hash
function M.copy_path_hash()
-- Copy current file relative path with line number as GitHub URL with branch name to clipboard
function M.copy_branch_url_line()
local path = get_relative_path()
if not path then
vim.notify("No file path available", vim.log.levels.WARN)
return
end

-- Get Git remote URL
local git_remote = vim.fn.system("git config --get remote.origin.url"):gsub("\n", "")
if vim.v.shell_error ~= 0 then
vim.notify("Not in a Git repository", vim.log.levels.WARN)
return
local url = create_github_url(path, "branch", true)
if url then
copy_to_clipboard(url)
end
end

-- Convert SSH/HTTPS Git URL to GitHub URL
local github_url = git_remote
if github_url:match("^git@github.com:") then
github_url = github_url:gsub("^git@github.com:", "https://github.com/")
github_url = github_url:gsub("%.git$", "")
elseif github_url:match("^https://github.com/") then
github_url = github_url:gsub("%.git$", "")
else
vim.notify("Remote origin is not a GitHub repository", vim.log.levels.WARN)
-- Copy current file relative path as GitHub URL with commit hash to clipboard
function M.copy_hash_url()
local path = get_relative_path()
if not path then
vim.notify("No file path available", vim.log.levels.WARN)
return
end

-- Get current commit hash
local commit_hash = vim.fn.system("git rev-parse HEAD"):gsub("\n", "")
if vim.v.shell_error ~= 0 then
vim.notify("Failed to get current commit hash", vim.log.levels.WARN)
return
local url = create_github_url(path, "hash", false)
if url then
copy_to_clipboard(url)
end

local line_num = vim.api.nvim_win_get_cursor(0)[1]
local github_file_url = github_url .. "/blob/" .. commit_hash .. "/" .. path .. "#L" .. line_num

copy_to_clipboard(github_file_url)
end

-- Copy relative path (for <leader>cW)
function M.copy_path_with_hash()
-- Copy current file relative path with line number as GitHub URL with commit hash to clipboard
function M.copy_hash_url_line()
local path = get_relative_path()
if not path then
vim.notify("No file path available", vim.log.levels.WARN)
return
end

copy_to_clipboard(path)
local url = create_github_url(path, "hash", true)
if url then
copy_to_clipboard(url)
end
end

return M
Expand Down
30 changes: 18 additions & 12 deletions lua/cfp/keymaps.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,39 @@ M.setup = function()

if keymaps.copy_path then
vim.keymap.set("n", keymaps.copy_path, cfp.copy_path, {
desc = "Copy file path"
desc = "Copy current file relative path to clipboard"
})
end

if keymaps.copy_path_line then
vim.keymap.set("n", keymaps.copy_path_line, cfp.copy_path_line, {
desc = "Copy file path with line number"
desc = "Copy current file relative path with line number to clipboard"
})
end

if keymaps.copy_path_url then
vim.keymap.set("n", keymaps.copy_path_url, cfp.copy_path_url, {
desc = "Copy file path as GitHub URL"
if keymaps.copy_branch_url then
vim.keymap.set("n", keymaps.copy_branch_url, cfp.copy_branch_url, {
desc = "Copy current file relative path as GitHub URL with branch name to clipboard"
})
end

if keymaps.copy_path_hash then
vim.keymap.set("n", keymaps.copy_path_hash, cfp.copy_path_hash, {
desc = "Copy file path as GitHub URL with commit hash"
if keymaps.copy_branch_url_line then
vim.keymap.set("n", keymaps.copy_branch_url_line, cfp.copy_branch_url_line, {
desc = "Copy current file relative path with line number as GitHub URL with branch name to clipboard",
})
end

if keymaps.copy_path_with_hash then
vim.keymap.set("n", keymaps.copy_path_with_hash, cfp.copy_path_with_hash, {
desc = "Copy file path"
if keymaps.copy_hash_url then
vim.keymap.set("n", keymaps.copy_hash_url, cfp.copy_hash_url, {
desc = "Copy current file relative path as GitHub URL with commit hash to clipboard"
})
end

if keymaps.copy_hash_url_line then
vim.keymap.set("n", keymaps.copy_hash_url_line, cfp.copy_hash_url_line, {
desc = "Copy current file relative path with line number as GitHub URL with commit hash to clipboard"
})
end
end

return M
return M
16 changes: 14 additions & 2 deletions plugin/cfp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,19 @@ vim.api.nvim_create_user_command("CopyPathLine", function() require("cfp").copy_
desc = "Copy current file relative path with line number to clipboard",
})

vim.api.nvim_create_user_command("CopyPathURL", function() require("cfp").copy_path_url() end, {
desc = "Copy current file relative path with line number as GitHub URL to clipboard",
vim.api.nvim_create_user_command("CopyBranchURL", function() require("cfp").copy_path_url() end, {
desc = "Copy current file relative path as GitHub URL with branch name to clipboard",
})

vim.api.nvim_create_user_command("CopyBranchURLLine", function() require("cfp").copy_path_url() end, {
desc = "Copy current file relative path with line number as GitHub URL with branch name to clipboard",
})

vim.api.nvim_create_user_command("CopyHashURL", function() require("cfp").copy_path_hash() end, {
desc = "Copy current file relative path as GitHub URL with commit hash to clipboard",
})

vim.api.nvim_create_user_command("CopyHashURLLine", function() require("cfp").copy_path_with_hash() end, {
desc = "Copy current file relative path with line number as GitHub URL with commit hash to clipboard",
})