Skip to content

Instantly share code, notes, and snippets.

@drocco007
Last active August 29, 2015 14:15
Show Gist options
  • Save drocco007/bc81c5e1b71d14495eca to your computer and use it in GitHub Desktop.
Save drocco007/bc81c5e1b71d14495eca to your computer and use it in GitHub Desktop.
Sublime Text 3 plugin to replace the default transpose command with something that behaves more like bash/emacs. Drop in to ~/.config/sublime-text-3/Packages/User to use.
import sublime, sublime_plugin
class Transpose(sublime_plugin.TextCommand):
"""Improve Sublime's transpose command.
Make Sublime's transpose command behave more like bash/emacs.
For each point:
* if the point is the end of a selection, do nothing
* if the point is at the start of the line, do nothing
* if the point is at the end of the line, swap the last two
characters of the line
* otherwise, swap the characters before and after the point
TODO:
* M-t to transpose words
* handle selections?
"""
def point_at_line_start(self, region):
return region.empty() and \
(self.view.classify(region.b) & sublime.CLASS_LINE_START)
def point_at_line_end(self, region):
return region.empty() and \
(self.view.classify(region.b) & sublime.CLASS_LINE_END)
def update_regions(self, region_updates):
selection = self.view.sel()
for old, new in region_updates:
selection.subtract(old)
selection.add(new)
def run(self, edit):
selection = self.view.sel()
region_updates = []
for region in selection:
if not region.empty() or self.point_at_line_start(region):
continue
elif self.point_at_line_end(region):
mutate = sublime.Region(region.b - 2, region.b)
else:
mutate = sublime.Region(region.b - 1, region.b + 1)
region_updates.append(
(region, sublime.Region(mutate.b, mutate.b))
)
self.view.replace(edit, mutate, self.view.substr(mutate)[::-1])
self.update_regions(region_updates)
@cmoon2000
Copy link

Hi! I want transpose features like Emacs in Sublime. I follow you how to drop file into Packages/User. But it doesn't work. Still typing Ctrl+t acts like default (between 2 character). Can you make it work on me?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment