Skip to content

XXiaoA/ns-textobject.nvim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ns-textobject.nvim

Awesome textobject plugin works with nvim-surround

Stars Issues Repo Size

demo.mp4
Click to show the configuration in demo.
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:

Click to show the code.
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:

Packer.nvim
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