Skip to content

Feature Request: Duplicating Nodes #780

@adueck

Description

@adueck

Is your feature request related to a problem? Please describe.

This library answers a huge felt need wanting to work with elements of the syntax by selecting them, deleting, swapping them around, etc.

But one itch that I find is still unscratched is the ability to duplicate nodes.

I often find I am working with an array of objects in TypeScript (which would be treated as @parameter) and I want to quickly make a duplicate of a node and then modify it. For example, I might have an array like this:

const people = [
    {
        name: "Bill Smith",
        age: 25,
    },
    {
        name: "Frank Jones",
        age: 30,
    },
];

And I want to duplicate Frank Jones' record. To do that I can easily select and/or yank the record, and then paste it after. The problem with this is that I have to do a little dancing around the trailing comma and it takes a few steps. The same thing goes for inline arrays of function parameters etc.

Screen.Recording.2025-06-16.at.12.56.35.PM.mov

Of course there's other options to do this like using V to do line-wise visual mode etc, but it always requires a bit of thinking and a few keystrokes to see how to duplicate nodes in different situations.

Describe the solution you'd like

What I would love to do would be have an action like move or swap that would allow me to use a keybinding to quickly duplicate something like a @parameter or a @statement. The same could be done for any nodes that are in an array on the tree.

lua <<EOF
require'nvim-treesitter.configs'.setup {
  textobjects = {
    copy = {
      enable = true,
      copy_next = {
        ["<leader>c"] = "@parameter.inner",
      },
    },
  },
}
EOF

In a way this would be similar to how many editors allow you to quickly duplicate files and then make adjustments. It would be so nice to be able to just quickly duplicate a @function, a @statement, or a @parameter in our syntax tree and then tweak it, rather than worry about yanking and pasting.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions