Home Download Buy Blog Forum Support

Jump to previous cursor position shortcut

Jump to previous cursor position shortcut

Postby canstein on Thu Dec 16, 2010 12:29 am

Updated: Clarified my question here and in my below resposne.

I'm very interested in sublime as a text editor. I have a question about a feature I lean heavily on in my work flow using visual studio. Is there an equivalent to the cursor position history browsing feature described below in sublime?

ctrl + -
jump to previous cursor (file) position
ctrl + -
jump to next cursor (file) position

If there is I may be sold!
Last edited by canstein on Thu Dec 16, 2010 5:17 pm, edited 2 times in total.
canstein
 
Posts: 6
Joined: Thu Dec 16, 2010 12:25 am

Re: Jump to previous position shortcut

Postby guillermooo on Thu Dec 16, 2010 7:25 am

Search in project might be what you're looking for?:

CTRL + SHIFT + P
<enter search term>
F4 (next occurrence in project)
SHIFT + SHIFT (previous occurrence in project)
guillermooo
 
Posts: 729
Joined: Thu Jul 23, 2009 9:06 am

Re: Jump to previous position shortcut

Postby jbjornson on Thu Dec 16, 2010 8:32 am

I think guillermooo meant SHIFT + F4, not "SHIFT + SHIFT " for previous occurrence in project.

I have found that Sublime Text does things differently than what you are used to and comfortable with. Once you understand how it works and get used to it, it is hard to go back to another editor. At first I thought that the ST implementation of projects was terrible (being used to a project tree like most other IDEs), but now that I understand projects in ST, I find other implementations very limiting and frustrating. Search within projects is a very cool and powerful feature and works really fast.

guillermooo wrote:Search in project might be what you're looking for?:

CTRL + SHIFT + P
<enter search term>
F4 (next occurrence in project)
SHIFT + SHIFT (previous occurrence in project)
jbjornson
 
Posts: 353
Joined: Tue Oct 27, 2009 4:25 pm
Location: Zurich, Switzerland

Re: Jump to previous position shortcut

Postby canstein on Thu Dec 16, 2010 3:28 pm

Thanks a bunch for the responses. I'm familiar with search in project but it's not exactly what I'm looking for. In visual studio you can track back to all the places your cursor has been (I should have used cursor in my first question) by hitting ctrl + - and ctrl + shift + -

Here's an example:
Code: Select all
- Cursor is on line 5
- Hit ctrl + f and search for a string
- Press enter to find the first instance of that string
   - Cursor jumps to line 13
- Press enter again to find the second instance of that string
   - Cursor jumps to line 21
- Hit escape to take focus off the find window
- Click on line 34 to edit something
   - Cursor jumps to line 34
- Press ctrl + -
   - Cursor jumps back to line 21
- Press ctrl + -
   - Cursor jumps back to line 13
- Press ctrl + -
   - Cursor jumps back to line 5

In Visual studio this works across files as well. It's super useful for the kind of editing I'm doing right now. Hopefully this walk through is clearer.
canstein
 
Posts: 6
Joined: Thu Dec 16, 2010 12:25 am

Re: Jump to previous position shortcut

Postby gpfsmurf on Thu Dec 16, 2010 5:17 pm

Try Ctrl+U (softUndo)
It's not exactly what you're looking for, since it tracks any kind of changes (not just cursor position)
gpfsmurf
 
Posts: 211
Joined: Mon Jun 23, 2008 6:31 pm

Re: Jump to previous cursor position shortcut

Postby canstein on Thu Dec 16, 2010 5:29 pm

I got really excited for a second there. If I haven't made any changes soft undo actually works the way I'd want but as soon as I've made changes it undoes them. My workflow has me jumping to the top of a file regularly to add things and then jumping back to my previous position quickly. This would undo the addition I just made. Thanks though!
canstein
 
Posts: 6
Joined: Thu Dec 16, 2010 12:25 am

Re: Jump to previous cursor position shortcut

Postby gpfsmurf on Thu Dec 16, 2010 5:46 pm

I think it's possible to do a plugin for this, using the onSelectionModified callback.

In fact I think I'm going to make one, if I have time this afternoon
gpfsmurf
 
Posts: 211
Joined: Mon Jun 23, 2008 6:31 pm

Re: Jump to previous cursor position shortcut

Postby canstein on Thu Dec 16, 2010 7:26 pm

That would be awesome! I believe this thread is kind of what I'm talking about:

viewtopic.php?f=4&t=1136&p=5043&hilit=cursor+history#p5043
canstein
 
Posts: 6
Joined: Thu Dec 16, 2010 12:25 am

Re: Jump to previous cursor position shortcut

Postby canstein on Mon Dec 20, 2010 6:59 pm

Don't worry about writing a plugin gpfsmurf. A good friend of mine (who talked to me into trying sublime text) wrote a cursor history plugin over the weekend. He's going to post it after he's had the chance to clean the code up a bit.
canstein
 
Posts: 6
Joined: Thu Dec 16, 2010 12:25 am

Re: Jump to previous cursor position shortcut

Postby gpfsmurf on Tue Dec 21, 2010 3:59 pm

Too late, I did one this morning :)
Quick and (very) dirty, but it works...
I won't go further since your friend's will probably be better.

Some ideas: it could be useful to group some of the cursor movements (so that pressing the right arrow 10 times will only be recorded as 1 command), and also to have a history of the where modifications were made, rather than just cursor movements.
And the 'cursor redo stack' doesn't need to be cleared each time there's a modification

EDIT: this is per-view... but it's easy to adapt for multiple files/views.

Here you go:

Code: Select all
import sublime, sublimeplugin

from collections import defaultdict

class CursorHistory(sublimeplugin.TextCommand):

   _undo = defaultdict(list)
   _redo = defaultdict(list)
   _lastSize = defaultdict(int)
   isUndoing = False

   # the following properties are to make it easier to migrate to sublime text X
   # (per-view command instances)

   @property
   def undo(self): return self._undo[sublime.activeWindow().activeView().id()]

   @undo.setter
   def undo(self, value): self._undo[sublime.activeWindow().activeView().id()] = value

   @property
   def redo(self): return self._redo[sublime.activeWindow().activeView().id()]

   @redo.setter
   def redo(self, value): self._redo[sublime.activeWindow().activeView().id()] = value

   @property
   def lastSize(self): return self._lastSize[sublime.activeWindow().activeView().id()]

   @lastSize.setter
   def lastSize(self, value): self._lastSize[sublime.activeWindow().activeView().id()] = value


   def onSelectionModified(self, view):
      if self.isUndoing:
         self.isUndoing = False
         return
      
      self.redo = []

      if self.lastSize == view.size():
         # cursor movement
         self.undo.append(tuple(view.sel()))
      else:
         # modification
         self.lastSize = view.size()
      

   def run(self, view, args):
      if len(args) > 0 and args[0] in ['redo', 'up']:
         try:
            prev = self.redo.pop()
            if prev:
               if prev[0] == view.sel()[0]:
                  self.undo.append(prev)
                  prev = self.redo.pop()
               self.undo.append(prev)
               self.isUndoing = True
               view.sel().clear()
               view.sel().add(prev[0])
         except IndexError:
            pass
      else:
         try:
            prev = self.undo.pop()
            if prev:
               if prev[0] == view.sel()[0]:
                  self.redo.append(prev)
                  prev = self.undo.pop()
               self.redo.append(prev)
               self.isUndoing = True
               view.sel().clear()
               view.sel().add(prev[0])
         except IndexError:
            pass
      


And add this to the keymap:
Code: Select all
<binding key="ctrl+alt+left" command="cursorHistory undo"/>
   <binding key="ctrl+alt+right" command="cursorHistory redo"/>
gpfsmurf
 
Posts: 211
Joined: Mon Jun 23, 2008 6:31 pm

Next

Return to Technical Support

Who is online

Users browsing this forum: No registered users and 17 guests