kylechui / nvim-surround

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

Surrounding a visual selection does not leave the cursor in a consistent position at the end #216

Closed alexandreroberts closed 1 year ago

alexandreroberts commented 1 year ago

Checklist

Neovim Version

NVIM v0.8.3

Plugin Version

Tagged (Stable)

Minimal Configuration

" minconfig.vim

call plug#begin('~/.vim/plugged')
Plug 'kylechui/nvim-surround'
call plug#end()

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

Sample Buffer

This |is a| sentence.

(Where | indicates the boundaries of a visual selection.)

Keystroke Sequence

S]

Expected behavior

  1. Surround is a in square brackets
  2. leave the cursor on the opening bracket (or the closing bracket, but certainly one of the two brackets).

Actual behavior

Step 1 does indeed happen as expected. But then where the cursor ends up depends on how I made the visual selection. So if I start with the cursor on the a and then type vbS], then the cursor does indeed end up on the opening bracket. But if I start with the cursor on the i in is then type vwS], the cursor ends up on neither bracket but rather on the a (i.e., the place where the cursor was before nvim-surround was invoked).

I'm not sure this is strictly speaking a bug; perhaps it is even intentional? But to me the most natural behavior would be to consistently end on the opening bracket (or whatever other thing has surrounded the visual selection).

Additional context

I am especially interested in making the behavior consistent because I depended on this behavior while using vim-surround (from which I recently moved to nvim-surround) to write a number of mappings for wrapping visual selection in particular LaTeX commands I use frequently, e.g., vmap <leader>tq S}i\textquote<ESC> so that I can time ,tq to wrap a visual selection in \textquote{...}. Now with nvim-surround, this produces garbled output.

kylechui commented 1 year ago

Will work on a fix for this in the next few days; I spent a bit of time working on the same effect for normal-mode surrounds but visual seemed to just slip under my radar.

kylechui commented 1 year ago

I will note that I have something like this for your use case: https://github.com/kylechui/config.nvim/blob/main/after/ftplugin/tex.lua#L28

It just enables command surrounds

kylechui commented 1 year ago

Please update to the latest commit and see if the issue is resolved; let me know how it goes!

alexandreroberts commented 1 year ago

It works perfectly now. Thanks so much for the fix!

alexandreroberts commented 1 year ago

I will note that I have something like this for your use case: https://github.com/kylechui/config.nvim/blob/main/after/ftplugin/tex.lua#L28

It just enables command surrounds

Yes, I've been using this feature, it's great. But Sctextquote takes longer to write than ,tq. Is there some simple way to harness this c option to write a more intelligent mapping for ,tq (i.e., that has the same effect but simply inputs textquote to the c option directly rather than prompting the user)?

kylechui commented 1 year ago

Change the add key to be

add = { "\\textquote{", "}" }

See the help menu for more information.

alexandreroberts commented 1 year ago

Ah! That makes sense. Sorry for the elementary question; as you can tell, my understanding of the code is very limited, especially in lua. Hopefully I am learning...

kylechui commented 1 year ago

No worries, feel free to tag me here (or perhaps in a discussions post) if you have any more questions about the plugin or Lua in general!

alexandreroberts commented 1 year ago

Thank you, much appreciated!