Skip to content

Instantly share code, notes, and snippets.

@vitaLee
Created June 5, 2012 17:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vitaLee/2876253 to your computer and use it in GitHub Desktop.
Save vitaLee/2876253 to your computer and use it in GitHub Desktop.
SublimeText command for snapping displaced text to indentation
# sample shortcuts
{ "keys": ["ctrl+super+]"], "command": "snap_lines_to_indent_level", "args": { "snap_direction": 1 } },
{ "keys": ["ctrl+super+["], "command": "snap_lines_to_indent_level", "args": { "snap_direction": -1 } }
import sublime
import sublime_plugin
import re
import math
class SnapLinesToIndentLevel(sublime_plugin.TextCommand):
def run(self, edit, snap_direction=1):
view = self.view
tab_size = view.settings().get('tab_size')
use_spaces = view.settings().get('translate_tabs_to_spaces')
lines = []
for sel in view.sel():
lines += view.lines(sel)
lines.reverse()
for line_region in lines:
content = view.substr(line_region)
match = re.match("^\s*", content)
if not match:
continue
whitespace = match.group(0)
spaces = map(lambda c: 1 if c == ' ' else tab_size,
list(whitespace))
curr_indent = sum(spaces)
indent_offset = float(curr_indent) / tab_size
if indent_offset == 0:
continue
#backward
if snap_direction == -1:
indent_level = math.floor(indent_offset) * tab_size
else:
indent_level = math.ceil(indent_offset) * tab_size
indent_content = ''
if use_spaces:
indent_content = ' ' * int(indent_level)
else:
indent_content = '\t' * int(indent_level / tab_size)
replace_region = sublime.Region(line_region.a,
line_region.a + len(whitespace))
view.replace(edit, replace_region, indent_content)
@horak
Copy link

horak commented Jul 24, 2013

Thank you!

@claessen
Copy link

Very useful. Thanks!

@9840380
Copy link

9840380 commented Jan 15, 2021

not working now

putting py inside user folder
in key binding entered shortcuts

help!!!!!)))

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