ehuss / Sublime-Wrap-Plus

Enhanced "wrap lines" command for Sublime Text 2 or 3.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Wrap without loosing cursor position

ggutierrez opened this issue · comments

It will be very nice when wrapping a paragraph to not loose the current cursor position. That is, after the operation the cursor should remain in the position were it was when the operation was invoked.

For me it was enough to have the cursor at the end of the last paragraph (instead of below the last paragraph) so I helped myself by commenting line 595 in wrap_plus.py.

That's not really what you want, but for me the current cursor position is at the end of the last paragraph most of the time when I use wrap. Would be nice to see a config option for this too.

+1 on @andialbrecht's suggestion.

any progress on this issue? I also think the cursor should remain in the current position instead of jumping to the end of the paragraph.

My workaround is simple. Create a new plugin with the following implementations:

import sublime, sublime_plugin

class MyWrapCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        pos = self.view.sel()[0].begin()
        self.view.run_command('wrap_lines_plus')
        self.view.sel().clear()
        self.view.sel().add(sublime.Region(pos))

Then add the following to your key bindings file:

  { "keys": ["alt+q"], "command": "my_wrap" },

replace the key binding for something tat suits your needs.

+1 @ggutierrez
thank you very much, that works perfectly fine

I just fixed this on my fork. It took me a lot of work until get it working almost 99% correctly. There are just some rare edge cases: https://github.com/evandrocoan/WrapPlus

Just remember, it has a setting to control this behavior:

    // Control the cursor behavior while wrapping the text. It accepts the following:
    // "cursor_below", will move the cursor/caret to the end the the wrapped text
    // "cursor_stay", will `attempt` to keep the cursor/caret on its original position
    "WrapPlus.after_wrap": "cursor_stay",

Thanks, @ggutierrez, I changed that py code to:

import sublime, sublime_plugin

class WrapPlusCursorFix(sublime_plugin.TextCommand):
  def run(self, edit):
    pos = self.view.sel()[0].end()
    self.view.run_command('wrap_lines_plus')
    self.view.sel().clear()
    self.view.sel().add(sublime.Region(pos))

It places the cursor at the end of the selection instead of begin().