nvim-surround icon indicating copy to clipboard operation
nvim-surround copied to clipboard

Add support for entering new surrounding in insert mode instead of prompt

Open tom-anders opened this issue 1 year ago • 6 comments

Checklist

  • [x] Have you read through :h nvim-surround to see if there might be any relevant information there?

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

When using something like csf to change a function call, there is a prompt for entering the new function name. This means I can't take advantage of LSP autocompletion when typing the new name.

Describe the solution you'd like

Have an option to enter insert mode for typing in the new name instead of the prompt.

tom-anders avatar Aug 25 '22 08:08 tom-anders

Hi there, adding this in would require a non-trivial refactor and I probably won't do it anytime soon (although it is quite an interesting idea). However, a workaround you could try is to hit the key combination csf<CR>i. That way it should just delete the function name (but leaving the parentheses), and then you can use LSP auto-completion to your heart's content. For your convenience, here's a keymap that you can use for the time being, although note that it's not part of the official API and could break in subsequent updates:

vim.keymap.set("n", "csf", function()
    vim.schedule(function()
        require("nvim-surround").change_surround({
            del_char = "f",
            add_delimiters = function()
                return { { "" }, { "" } }
            end,
            curpos = require("nvim-surround.buffer").get_curpos(),
        })
    end)
    return "i"
end, { silent = true, expr = true })

I'll be leaving this issue open as the above is just a workaround; please let me know if it helped!

kylechui avatar Aug 26 '22 14:08 kylechui

Sounds cool, thanks for the workaround!

tom-anders avatar Aug 26 '22 15:08 tom-anders

this would be a useful feature if only to insert xml-style tags (when you're not in xml/html/etc). So, something like <C-s>t in insert mode would spawn some sort of input() (like s<motion>t in normal mode) and then typing "code" would give you

<code>|</code>

where | is the cursor of course ("on" the '<' of "</code>" but in insert mode).

cough cough I'm writing documentation, and I am very tired of typing <code>bla bla bla then remembering to write </code> at the end.

VioletJewel avatar Dec 10 '23 16:12 VioletJewel

As of right now, using the default bindings in insert mode, <C-g>stcode<CR> does the behavior that you want. However, if you find yourself doing a lot of surrounds involving <code> tags, I would recommend making a custom surround.

kylechui avatar Dec 10 '23 19:12 kylechui

I am so sorry. I was using a different plugin, and I really thought I had come to the right repo. Thanks & sorry

VioletJewel avatar Dec 11 '23 00:12 VioletJewel

No worries! Maybe now you'll have some incentive to switch to nvim-surround :wink:

kylechui avatar Dec 11 '23 00:12 kylechui