XXiaoA/ns-textobject.nvim

github github
editing-support
stars 36
issues 3
subscribers 1
forks 3
CREATED

2022-10-15

UPDATED

10 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

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 .

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

And here comes some useful features with default configuration:

  • For function:
a = func(args)
-- if press dsf
a = args
-- if press daf
a =
-- if press dif
a = func()
  • For quotes
s = "this's a `string`"
-- press ciqworld (cursor inside "")
s = "word"
-- press ciqworld (cursor inside ``)
s = "this's a `world`"
  • And others easy-used textobjects without pressing extra keys or leaving main area of keyboard:
    • ia|aa: alias of i<|a< (for <sth.>)
    • ir|ar: alias of i[|a[ (for [sth.])

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

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