omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Indent/outdent selected lines

    Pythonista
    1
    1
    1131
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Alex210
      Alex210 last edited by

      This will indent or outdent all selected lines:
      <code>
      #Indent/Outdent selected lines

      import editor
      import console

      indent_char = '\t'
      text = editor.get_text()
      selection = editor.get_line_selection()
      selected_text = text[selection[0]:selection[1]]

      indent = console.alert('indent or outdent?', '', '<<', '>>') == 2

      replacement = ''
      for line in selected_text.splitlines():
      if indent:
      replacement += indent_char + line + '\n'
      else:
      replacement += line[line.find(indent_char) + len(indent_char):] + '\n'

      editor.replace_text(selection[0], selection[1], replacement)
      editor.set_selection(selection[0], selection[0] + len(replacement) - 1)
      </code>

      Most of it is stolen from the (un)comment script. Change the indent_char variable to whatever your preference is, e.g. this could be 2 or 4 spaces. Outdent will only work if all lines use the same indent_char.

      1 Reply Last reply Reply Quote 0
      • First post
        Last post
      Powered by NodeBB Forums | Contributors