XXiaoA/ns-textobject.nvim

github github
pluginediting-support
star 24
alert-circle 3
users 1
git-branch 3
CREATED

2022-10-15

UPDATED

2 months ago


https://user-images.githubusercontent.com/62557596/210149085-8cb8c3e0-dd57-40c6-aeec-5fbac8aa01d1.mp4

require("ns-textobject").setup({})

-- from https://github.com/kylechui/nvim-surround/discussions/53#discussioncomment-3134891
-- move the following callback into `~/.config/nvim/after/ftplugin/markdown.lua`
require("nvim-surround").buffer_setup({
    surrounds = {
        ["l"] = {
            add = function()
                local clipboard = vim.fn.getreg("+"):gsub("\n", "")
                return {
                    { "[" },
                    { "](" .. clipboard .. ")" },
                }
            end,
            find = "%b[]%b()",
            delete = "^(%[)().-(%]%b())()$",
            change = {
                target = "^()()%b[]%((.-)()%)$",
                replacement = function()
                    local clipboard = vim.fn.getreg("+"):gsub("\n", "")
                    return {
                        { "" },
                        { clipboard },
                    }
                end,
            },
        },
    }
})

Requirements

Installation

Install the plugin with your favourite package manager:

use({
    "XXiaoA/ns-textobject.nvim",
    after = "nvim-surround",
    config = function()
        require("ns-textobject").setup({
            -- your configuration here
            -- or just left empty to use defaluts
        })
    end
})

Usage

We will make the keymaps refer to your nvim-surround's aliases and surrounds automatically after calling setup. If you want to disable this feature, check the Configuration .

Then you're able to map manually like the following:

local nstextobject = require("ns-textobject")

vim.keymap.set({ "x", "o" }, "aq", function()
    -- First parameter means a alias or surround of nvim-surround
    -- The second one has two choice: `a` means around or `i` means inside
    nstextobject.create_textobj("q", "a")
end, { desc = "Around the quote" })
vim.keymap.set({ "x", "o" }, "iq", function()
    nstextobject.create_textobj("q", "i")
end, { desc = "Inside the quote" })

-- Or a simple way:
-- First parameter means a alias or surround of nvim-surround
-- The second one used to add the description for keymap
nstextobject.map_textobj("q", "quotes")

Configuration

{
    auto_mapping = {
        -- automatically mapping for nvim-surround's aliases
        aliases = true,
        -- for nvim-surround's surrounds
        surrounds = true,
    },
    disable_builtin_mapping = {
        enabled = true,
        -- list of char which shouldn't mapping by auto_mapping
        chars = { "b", "B", "t", "`", "'", '"', "{", "}", "(", ")", "[", "]", "<", ">" },
    },
}

TODO

  • new option to disable auto mapping for builtin textobject (ib, etc.)
  • support nvim-surround