How can I jump to a random character in the front tab in Sublime Text 2? Is there a plug-in for that purpose?
If I had a text file like below open in the front tab and the cursor was at the beginning of the first line,
► put returns between paragraphs
► for linebreak add 2 spaces at end
► _italic_ or **bold**
► indent code by 4 spaces
► backtick escapes `like _so_`
► quote by placing > at start of line
► to make links
<http://foo.com>
[foo](http://foo.com)
<a href="http://foo.com">foo</a>
► basic HTML also allowed
I'd like to jump to somewhere really random in this text like to the second "i" of the line "► quote by placing > at start of line".
Some searching of PackageControl doesn't seem to turn up anything that looks like it would do this, but you can roll your own with some simple Python code.
The following is an example plugin (works for Sublime Text 2 and 3) that implements a command that will do this. To use this, select either Tools > New Plugin...
or Tools > Developer > New Plugin...
(depending on the version of Sublime Text you are using) and replace the default plugin with the code here, then save it as a python file.
import sublime, sublime_plugin
import random
class JumpToRandomPositionCommand(sublime_plugin.TextCommand):
"""
When invoked, randomly select a character in the current
file and jump the cursor to that position. Does nothing
if the current file is empty or if the current view does
not represent a file.
"""
def run(self, edit):
view = self.view
if view.size() > 0 and view.settings().get("is_widget", False) == False:
view.sel().clear()
pos = random.randrange(0, view.size())
view.sel().add(sublime.Region(pos, pos))
view.show(pos)
This implements a command named jump_to_random_position
which will randomly select a character in the file and jump the cursor to that position, making sure that the new position is visible on the screen.
This will un-select anything that might be selected and revert the view to single selection mode (if it was not). It also takes care to do nothing on an empty file or when the current view is a widget (e.g. the text entry in the Sublime console).
Depending on how often you need to do something like this, you could either create a key binding for the command:
{
"keys": ["ctrl+alt+shift+r"],
"command": "jump_to_random_position"
}
or you can manually invoke the command from the Sublime Text console using the following code (to open the console, choose View > Show Console
from the menu or press Ctrl+`:
view.run_command("jump_to_random_position")