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.
- Using vim-plug:
call plug#begin()
" Other plugins...
Plug 'zhisme/copy_with_context.nvim'
call plug#end()
- Using packer.nvim:
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 = false,
context_format = '# %s:%s', -- Default format for context: "# Source file: filepath:line"
})
end
}
- Using lazy.nvim
{
'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 = false,
context_format = '# %s:%s', -- Default format for context: "# Source file: filepath:line"
})
end
},
- Copy current line with relative path:
- Press
<leader>cy
in normal mode. - Plugin copies line under cursor with relative path into your unnamed register.
- Paste somewhere
- Press
Output example:
<% posts.each do |post| %>
# app/views/widgets/show.html.erb:4
- Copy current line with absolute path:
- Press
<leader>cY
in normal mode. - Plugin copies line under cursor with absolute path into your unnamed register.
- Paste somewhere
- Press
Output example:
<% posts.each do |post| %>
# /Users/zh/dev/project_name/app/views/widgets/show.html.erb:4
- Copy visual selection with relative path:
- Select lines in visual mode.
- Press
<leader>cY
. - Plugin copies the selected lines with relative path into your unnamed register.
- Paste somewhere
Output example:
<% posts.each do |post| %>
<%= post.title %>
<% end %>
# app/views/widgets/show.html.erb:4-6
- Copy visual selection with absolute path:
- Select lines in visual mode.
- Press
<leader>cY
. - Plugin copies the selected lines with absolute path into your unnamed register.
- Paste somewhere
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 = false,
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:
- Neovim (version 0.7.0 or higher)
- Lua (5.1 or higher)
- Cargo (Rust build tool for running stylua) Install
- Fork the repository
- Clone your fork:
git clone https://github.com/zhisme/copy_with_context.nvim
cd copy_with_context.nvim
- Install dependencies with Makefile.
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 = false,
context_format = '# %s:%s', -- Default format for context: "# 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 = false,
context_format = '# %s:%s', -- Default format for context: "# 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.