zhisme/copy_with_context.nvim

github github
editing-support
stars 10
issues 4
subscribers 1
forks 0
CREATED

2025-03-18

UPDATED

5 days ago


copy_with_context.nvim

Neovim CI codecov Hits-of-Code GitHub Tag GitHub License

Copy lines with file path and line number metadata. Perfect for sharing code snippets with context.

Why?

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.

Installation

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
  },

Usage

  1. 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

Output example:

  <% posts.each do |post| %>
  # app/views/widgets/show.html.erb:4
  1. 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

Output example:

  <% posts.each do |post| %>
  # /Users/zh/dev/project_name/app/views/widgets/show.html.erb:4
  1. 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
  1. 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

Configuration

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"
})

Development

Want to contribute to copy_with_context.nvim? Here's how to set up your local development environment:

Prerequisites

  • Neovim (version 0.7.0 or higher)
  • Lua (5.1 or higher)
  • Cargo (Rust build tool for running stylua) Install

Setup

  1. Fork the repository
  2. Clone your fork:
git clone https://github.com/yourusername/copy_with_context.nvim
cd copy_with_context.nvim
  1. Install dependencies with Makefile.
make deps

Tests

Tests are written in test framework busted

How to run tests:

make test

Linting

Linting is done with luacheck

make lint

Formatting

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

Testing Your Changes Locally

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

Contributing

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.

License

The plugin is available as open source under the terms of the MIT License.