2

I'm working on a plugin and I use visual selections all the time in my code. Stuff like normal! v{:call whatever()\<cr>.

The problem is that if the user does gv in his file it won't select his last visual selection, but the last visual selection used by my plugin.

So is there any way I can prevent that from happening?

7
  • 4
    gv uses the marks < and >. Presumably you could save them away and restore them in your code, but I'm not sure how you would do that without actually visiting the location '< and saving that location to a different mark....
    – Tumbler41
    Commented Aug 2, 2016 at 20:47
  • This isn't really a full answer, but there is a plugin for saving visual selections like marks or registers. It's called Visual Marks. Peering through the source code, or including it in your plugin might help you figure it out.
    – DJMcMayhem
    Commented Aug 2, 2016 at 20:53
  • 3
    Yes, you can save and restore the > and < marks using getpos()/setpos(). I do that for the NrrwRgn plugin. This needs patch 7.3.590 Commented Aug 2, 2016 at 21:05
  • 1
    v{:command could be expressed with .,'}command. Maybe you could use visual mode a little less.
    – romainl
    Commented Aug 2, 2016 at 21:06
  • 1
    On a side note: you might want to look at the textobj-user plugin and the text objects already defined with it. Commented Aug 3, 2016 at 5:54

1 Answer 1

6

Based on the comments by @Tumbler41 and @ChristianBrabant I managed to do it like this:

let l:lastSelectionStart = getpos("'<")
let l:lastSelectionEnd = getpos("'>")

" My code...

call setpos("'<", l:lastSelectionStart)
call setpos("'>", l:lastSelectionEnd)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.