Copy lines with file path and line number 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.
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'
},
-- whether to trim lines or not
trim_lines = true,
context_format = '# %s:%s', -- Default format for context: "# Source file: filepath:line"
})
end
}
{
'zhisme/copy_with_context.nvim',
config = function()
require('copy_with_context').setup({
-- Customize mappings
mappings = {
relative = '<leader>cy',
absolute = '<leader>cY'
},
-- whether to trim lines or not
trim_lines = true,
context_format = '# %s:%s', -- Default format for context: "# Source file: filepath:line"
})
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
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'
},
-- whether to trim lines or not
trim_lines = true,
context_format = '# %s:%s', -- Default format for context: "# Source file: filepath:line"
-- context_format = '# Source file: %s:%s',
-- Other format for context: "# Source file: /path/to/file:123"
})
Want to contribute to copy_with_context.nvim
? Here's how to set up your local development environment:
git clone https://github.com/yourusername/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'
},
-- whether to trim lines or not
trim_lines = true,
context_format = '# %s:%s', -- Default format for context: "# Source file: filepath:line"
-- context_format = '# Source file: %s:%s',
-- Other format for context: "# Source file: /path/to/file:123"
})
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'
},
-- whether to trim lines or not
trim_lines = true,
context_format = '# %s:%s', -- Default format for context: "# Source file: filepath:line"
-- context_format = '# Source file: %s:%s',
-- Other format for context: "# Source file: /path/to/file:123"
}
}
Then restart Neovim or run :Lazy
sync to load the local version
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.