Copy lines with file path, line number, and repository URL metadata. Perfect for sharing code snippets with context.
When sharing code snippets, it's often useful to include the file path and line number for context. This plugin makes it easy to copy lines with this metadata. It is easier to understand the context of the code snippet when the file path and line number are included. Otherwise you have to do it manually. Copying snippet, then adding the line number (what if it is long config file? it is boring). We can automate it and do not waste our time.
Working with AI assistants on the web (ChatGPT, Claude, Gemini, etc.)? Including line numbers and file paths gives you significantly better results.
src/auth/login.ts:42 help AI understand your project structure❌ Without context:
Here's my login function:
def authenticate(user)
validate_credentials(user)
end
How do I add OAuth?
✅ With context (using this plugin):
Here's my login function:
def authenticate(user)
validate_credentials(user)
end
# app/controllers/auth_controller.rb:45-47
# https://github.com/user/repo/blob/abc123/app/controllers/auth_controller.rb#L45-L47
How do I add OAuth?
Result: The second prompt gives AI file location, line numbers, project structure insight, and a direct link to the code. AI provides OAuth integration that fits your exact architecture instead of generic advice.
call plug#begin()
" Other plugins...
Plug 'zhisme/copy_with_context.nvim'
call plug#end()
use {
'zhisme/copy_with_context.nvim',
config = function()
require('copy_with_context').setup({
-- Customize mappings
mappings = {
relative = '<leader>cy',
absolute = '<leader>cY',
remote = '<leader>cr',
},
formats = {
default = '# {filepath}:{line}', -- Used by relative and absolute mappings
remote = '# {remote_url}', -- Custom format for remote mapping
},
-- whether to trim lines or not
trim_lines = false,
})
end
}
{
'zhisme/copy_with_context.nvim',
config = function()
require('copy_with_context').setup({
-- Customize mappings
mappings = {
relative = '<leader>cy',
absolute = '<leader>cY',
remote = '<leader>cr',
},
formats = {
default = '# {filepath}:{line}', -- Used by relative and absolute mappings
remote = '# {remote_url}',
},
-- whether to trim lines or not
trim_lines = false,
})
end
},
<leader>cy in normal mode.Output example:
<% posts.each do |post| %>
# app/views/widgets/show.html.erb:4
<leader>cY in normal mode.Output example:
<% posts.each do |post| %>
# /Users/zh/dev/project_name/app/views/widgets/show.html.erb:4
<leader>cY.Output example:
<% posts.each do |post| %>
<%= post.title %>
<% end %>
# app/views/widgets/show.html.erb:4-6
<leader>cY.Output example:
<% posts.each do |post| %>
<%= post.title %>
<% end %>
# /Users/zh/dev/project_name/app/views/widgets/show.html.erb:4-6
<leader>cr in normal mode. <% posts.each do |post| %>
# https://github.com/user/repo/blob/abc123def/app/views/widgets/show.html.erb#L4
<leader>cr. <% posts.each do |post| %>
<%= post.title %>
<% end %>
# https://github.com/user/repo/blob/abc123def/app/views/widgets/show.html.erb#L4-L6
There is no need to call setup if you are ok with the defaults.
-- default options
require('copy_with_context').setup({
-- Customize mappings
mappings = {
relative = '<leader>cy',
absolute = '<leader>cY',
},
-- Define format strings for each mapping
formats = {
default = '# {filepath}:{line}', -- Used by relative and absolute mappings
},
-- whether to trim lines or not
trim_lines = false,
})
You can use the following variables in format strings:
{filepath} - The file path (relative or absolute depending on mapping){line} - Line number or range (e.g., "42" or "10-20"){linenumber} - Alias for {line}{remote_url} - Repository URL (GitHub, GitLab, Bitbucket)You can define unlimited custom mappings with their own format strings:
require('copy_with_context').setup({
mappings = {
relative = '<leader>cy',
absolute = '<leader>cY',
remote = '<leader>cr',
full = '<leader>cx', -- Custom mapping with everything
},
formats = {
default = '# {filepath}:{line}',
remote = '# {remote_url}',
full = '# {filepath}:{line}\n# {remote_url}',
},
})
Important: Every mapping name must have a matching format name. The special mappings relative and absolute use the default format.
In case it fails to find the format for a mapping, it will fail during config load time with an error message. Check your config if that happens, whether everything specified in mappings is also present in formats.
When you use {remote_url} in a format string, the plugin automatically generates permalink URLs for your code snippets. This feature works with:
The URLs always use the current commit SHA for stable permalinks. If you're not in a git repository or the repository provider is not recognized, the URL will simply be omitted (graceful degradation)
Want to contribute to copy_with_context.nvim? Here's how to set up your local development environment:
git clone https://github.com/zhisme/copy_with_context.nvim
cd copy_with_context.nvim
make deps
Tests are written in test framework busted
How to run tests:
make test
Linting is done with luacheck
make lint
Formatting is done with stylua
To automatically format the code, run:
make fmt
To check if the code is formatted correctly, run:
make fmt-check
To test the plugin in your Neovim environment while developing:
With packer.nvim:
use {
"~/path/to/copy_with_context.nvim",
config = function()
require('copy_with_context').setup({
-- Customize mappings
mappings = {
relative = '<leader>cy',
absolute = '<leader>cY',
remote = '<leader>cr',
},
formats = {
default = '# {filepath}:{line}',
remote = '# {remote_url}',
},
-- whether to trim lines or not
trim_lines = false,
})
end
}
Then run :PackerSync to load the local version
With lazy.nvim:
{
dir = "~/path/to/copy_with_context.nvim",
dev = true,
opts = {
mappings = {
relative = '<leader>cy',
absolute = '<leader>cY',
remote = '<leader>cr',
},
formats = {
default = '# {filepath}:{line}',
remote = '# {remote_url}',
},
-- whether to trim lines or not
trim_lines = false,
}
}
Then restart Neovim or run :Lazy sync to load the local version
For maintainers: see RELEASING.md for the complete release process.
The guide covers:
Bug reports and pull requests are welcome on GitHub at https://github.com/zhisme/copy_with_context.nvim. Ensure to test your solution and provide a clear description of the problem you are solving. Write new tests for your changes, and make sure the tests pass as well as linters.
The plugin is available as open source under the terms of the MIT License.