Sublime Forum

Jump to previous cursor position shortcut

#1

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!

1 Like

#2

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

CTRL + SHIFT + P

F4 (next occurrence in project)
SHIFT + SHIFT (previous occurrence in project)

0 Likes

#3

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.

[quote=“guillermooo”]Search in project might be what you’re looking for?:

CTRL + SHIFT + P

F4 (next occurrence in project)
SHIFT + SHIFT (previous occurrence in project)[/quote]

0 Likes

#4

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:

- 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.

0 Likes

Navigate Backward/Forward
#5

Try Ctrl+U (softUndo)
It’s not exactly what you’re looking for, since it tracks any kind of changes (not just cursor position)

0 Likes

#6

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!

0 Likes

#7

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

0 Likes

#8

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

0 Likes

#9

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.

0 Likes

#10

Too late, I did one this morning :smile:
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:

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:

<binding key="ctrl+alt+left" command="cursorHistory undo"/> <binding key="ctrl+alt+right" command="cursorHistory redo"/>

0 Likes

#11

Just had a little try and couldn’t hack this to work… anyone know how/got a solution?

cheers…

0 Likes

#12

this works great, for navigation history:

viewtopic.php?f=5&t=2738&p=12698&hilit=command_history#p12698

0 Likes

#13

Guys, was an official plugin ever released? I could really do with this functionality

0 Likes

#14

It’s been long time, but now it is possible:

  • to jump back: Alt + -
  • to jump forward: Alt + Shift + -

or through Goto menu.

(SublimeText 3, build 3114)

2 Likes

#15

thanks for this!!

1 Like