kylechui / nvim-surround

Add/change/delete surrounding delimiter pairs with ease. Written with :heart: in Lua.
MIT License
3.18k stars 62 forks source link

Return cursor to original position after surround operation #63

Closed benfrain closed 2 years ago

benfrain commented 2 years ago

Firstly, thanks for creating this plugin.

On first use I expected that my caret/cursor would be positioned back where it was when I executed the command.

For example, given this string, and my cursor/caret position indidated by the pipe |:

"kylech|ui/nvim-surround",

I run cs"' and end like this:

|'kylechui/nvim-surround',

Where I would expect this:

'kylech|ui/nvim-surround',

Is this something you feel may be a preferable outcome?

kylechui commented 2 years ago

I think this can be added as an option in the user setup table, e.g. move_cursor. Apologies in advance since I might not be able to work on this until I have a few other "larger" features fleshed out that will affect the design of the codebase. Another thing to consider would be "position relativity", e.g. consider the word feature with the cursor over the t in normal mode. If you type ysiw", would the cursor reside on a or t? Since inserting characters before the cursor would shift things forwards. In any case, thanks for submitting the feature request!

Edit: I am leaning towards "no relativity", e.g. the cursor would move to the a, as I think this is simpler.

kylechui commented 2 years ago

@benfrain I think I'll include this as a new key into the setup table (maybe move_cursor?), which takes a bool or string value:

Development will occur on the branch linked to this issue, cursor-movement-options; I'll tag you again when I have a working copy. To anybody reading this, please give me feedback on the interface I've suggested above, positive or negative :)

kylechui commented 2 years ago

@benfrain Should be working now, cursor remains fixed in place for all of add/visual/change/delete operations, and will go to nearest valid location if the original cursor position is now "out of bounds". For example:

require('nvim-surround').setup() -- Comment

If the cursor is on the last character of the word Comment, then typing dsb will delete the parentheses after setup (reverse jumping). This invalidates the cursor's original position as the line is now shorter, so the cursor will "snap" to the new location of the final character in the line (2 chars back).

benfrain commented 2 years ago

Thanks @kylechui look forward to giving it a whirl next week 👍

kevintraver commented 1 year ago

A use case for when the end option might be helpful:

When using CTRL-o in insert mode.

This would allow an easy way to surround the current word with fewer motions while in insert mode, and allow you to remain in insert mode when finished.

Currently this does not work because the cursor is in the left position when returning to insert mode.