<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Scripts to align text]]></title><description><![CDATA[<p dir="auto">This pair of scripts line up multiple lines of text on a given character (or characters), inserting padding as needed so the character is in the same column in all lines.</p>
<p dir="auto">These serve as alternatives to the TextFX Edit: “Line up multiple lines” options.  <a class="plugin-mentions-user plugin-mentions-a" href="/user/peterjones" aria-label="Profile: peterjones">@<bdi>peterjones</bdi></a> The FAQ on “How do I Replicate the Features of TextFX?” may offer these as another alternative.</p>
<p dir="auto">These require PythonScript v3.0.x (currently v3.0.16).</p>
<p dir="auto">At present, the alignment character is taken as the character at the top of the current selection. You can uncomment some code to change this policy to instead take the alignment character from within the selection at whichever end has the cursor. Either way, if that character is white space, the user is prompted to type the character (or characters). If you really wish to align on a white space character, you can just click OK at the prompt.</p>
<p dir="auto">When prompted to type the alignment character, the user may enter a sequence of characters, e.g., “–&gt;”, in which case the alignment is on the instances of that entire character sequence. For example, if the user enters “–&gt;” at the prompt, then instances of the “-” character get aligned only if they’re followed immediately by the characters “-&gt;”, while instances of, say, “-1” and "- " remain unaltered.</p>
<p dir="auto">If there is no current selection, then aligns all lines in the editor. If there is a current selection, then aligns only the lines that are at least partially included in the selection, and the selection is changed to the entire block of newly-padded lines.</p>
<p dir="auto">The default is to align all instances of the specific character, but I include a second stript align_text_1.py to override that default and align only the first instance of the character in each line. If desired, you can create additional, similar scripts to align the first 2 instances, or 3, etc.</p>
<p dir="auto">In my case, I used the Shortcut Mapper to assign keys</p>
<ul>
<li>align_text.py       Ctrl+=</li>
<li>align_text_1.py   Ctrl+Shift+=</li>
</ul>
<p dir="auto"><strong>--------------------------------------------<br />
align_text.py:<br />
--------------------------------------------</strong></p>
<pre><code>#------------------------------------------------------------------------
# If the character specified in the current selection is a white space,
# then prompt the user to enter the alignment character (or characters),
# using this character as the initial default.
#------------------------------------------------------------------------
default_align_char = ','

def align_selected_text(max_align_char_count = None):
    """Insert padding into the lines in the selection, as needed, to align up to max_align_char_count instances of a specific character or string of characters

    The default is to align all instances of the specific character.

    At present, the alignment character is taken as the character at the top of
    the current selection. You can uncomment some code below to change this
    policy to instead take the alignment character from within the selection at
    whichever end has the cursor. Either way, if that character is white space,
    the user is prompted to type the character (or characters). If you really
        wish to align on a white space character, you can just click OK at the
        prompt.

    When prompted to type the alignment character, the user may enter a
    sequence of characters, e.g., "--&gt;", in which case the alignment is on the
    instances of that entire character sequence. For example, if the user
    enters "--&gt;" at the prompt, then instances of the "-" character get
    aligned only if they're followed immediately by the characters "-&gt;", while
    instances of, say, "-1" and "- " remain unaltered.

    If there is no current selection, then aligns all lines in the editor.
    If there is a current selection, then aligns only the lines that are
    at least partially included in the selection, and the selection is
    changed to the entire block of newly-padded lines.

    Parameters
    ----------
      max_align_char_count : positive integer, optional
          The maximum number of instances to align of the specific
          character.  For example, set to 1 to align only the first
          instance of the character on each line. The default is to
          align all instances of the specific character.
    """
    from Npp import editor

    #----------------------------------------------------------------------------
    # For the alignment character, take the character just inside the bounds of
    # the selection block (at either the start or the end, as determined below).
    #----------------------------------------------------------------------------
    editor.targetFromSelection()
    selected_text = editor.getTargetText()

    # Use this code to get the align_char unconditionally from the start
    # of the selection.
    align_char = selected_text[0]

    # Optionally use this code to get the align_char from within the selection
    # at whichever end has the cursor.
    # (startByte, endByte) = editor.getUserCharSelection()
    # if startByte == editor.getCurrentPos():
    #     align_char = selected_text[0]
    # else:
    #     align_char = selected_text[-1]

    # If the character from the selection seems implausible as the
    # align_char, then prompt the user for it.
    if align_char.isspace():
        from Npp import notepad
        global default_align_char
        align_char = notepad.prompt('Align character:',
                                    'Enter Alignment Character',
                                    default_align_char)
        if align_char is not None:
            default_align_char = align_char

    #----------------------------------------------------------------------------
    #%% Get the lines of text within the selected alignment block
    #----------------------------------------------------------------------------
    (startLine, endLine) = editor.getUserLineSelection()
    startPos = editor.positionFromLine(startLine)
    endPos = editor.getLineEndPosition(endLine)
    text_lines = editor.getTextRange(startPos, endPos).splitlines(True)

    #----------------------------------------------------------------------------
    # Remember whether there is a user-selected block, so we can restore a
    # corresponding selection after aligning the text.
    #----------------------------------------------------------------------------
    restore_selection = editor.getSelectionStart() != editor.getSelectionEnd()

    #----------------------------------------------------------------------------
    # Align all instances of align_char within the lines of text
    #----------------------------------------------------------------------------
    if align_char is not None:
        # Enable the following to save the align_char, however it was determined,
        # to be the default_align_char when prompting for it next time.
        # default_align_char = align_char

        if max_align_char_count is None:
            align_char_count = max(line.count(align_char) for line in text_lines)
        else:
            align_char_count = max_align_char_count
        start = 0
        for instance in range(align_char_count):
            # Set the target column using the index of the align_char, ignoring
            # immediately preceding space, or the length of the line
            tgt_char_col = max(len(line[:line.find(align_char, start)].rstrip())
                               for line in text_lines)

            for (idx,line) in enumerate(text_lines):
                align_char_col = line.find(align_char, start)
                if align_char_col &gt;= 0:
                    text_lines[idx] = line[:align_char_col].rstrip().ljust(tgt_char_col) \
                                    + line[align_char_col:]
            start += tgt_char_col + len(align_char)

        editor.setTarget(startPos, endPos)
        editor.replaceTarget(''.join(text_lines))

        if restore_selection:
            startPos = editor.positionFromLine(startLine)
            endPos = editor.getLineEndPosition(endLine)
            editor.setSelectionStart(startPos)
            editor.setSelectionEnd(endPos)


if __name__ == '__main__':
    align_selected_text()
</code></pre>
<p dir="auto"><strong>--------------------------------------------<br />
align_text_1.py:<br />
--------------------------------------------</strong></p>
<pre><code>from align_text import align_selected_text

align_selected_text(max_align_char_count = 1)
</code></pre>
]]></description><link>https://community.notepad-plus-plus.org/topic/24680/scripts-to-align-text</link><generator>RSS for Node</generator><lastBuildDate>Sat, 13 Jun 2026 18:13:49 GMT</lastBuildDate><atom:link href="https://community.notepad-plus-plus.org/topic/24680.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 07 Jul 2023 16:31:08 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Scripts to align text on Sun, 17 May 2026 18:51:00 GMT]]></title><description><![CDATA[<p dir="auto">I’ve added an option to pad to the right side of the selected character. This preserves the character’s position and instead aligns the non-space text to the right of the character. The default pads to the left side, so the character itself gets aligned.</p>
<p dir="auto">A complementary set of scripts, <strong>align_text_right.py</strong> and <strong>align_text_right_1.py</strong>,  use this new option.  Here’s the complete set of scripts.</p>
<h1>align_text.py:</h1>
<pre><code>#------------------------------------------------------------------------
# If the character specified in the current selection is a white space,
# then prompt the user to enter the alignment character (or characters),
# using this character as the initial default.
#------------------------------------------------------------------------
default_align_char = ','

from enum import Enum
class PaddingSide(Enum):
    LEFT = 0
    RIGHT = 1


def align_selected_text(max_align_char_count = None,
                        padding_side = PaddingSide.LEFT):
    """Insert padding into the lines in the selection, as needed, to align up to max_align_char_count instances of a specific character or string of characters

    The default is to align all instances of the specific character.

    At present, the alignment character is taken as the character at the top of
    the current selection. You can uncomment some code below to change this
    policy to instead take the alignment character from within the selection at
    whichever end has the cursor. Either way, if that character is white space,
    the user is prompted to type the character (or characters). If you really
    wish to align on a white space character, you can just click OK at the
    prompt.

    When prompted to type the alignment character, the user may enter a
    sequence of characters, e.g., "--&gt;", in which case the alignment is on the
    instances of that entire character sequence. For example, if the user
    enters "--&gt;" at the prompt, then instances of the "-" character get
    aligned only if they're followed immediately by the characters "-&gt;", while
    instances of, say, "-1" and "- " remain unaltered.

    If there is no current selection, then aligns all lines in the editor.
    If there is a current selection, then aligns only the lines that are
    at least partially included in the selection, and the selection is
    changed to the entire block of newly-padded lines.

    Parameters
    ----------
      max_align_char_count : positive integer, optional
          The maximum number of instances to align of the specific
          character.  For example, set to 1 to align only the first
          instance of the character on each line. The default is to
          align all instances of the specific character.
      padding_side : instance of PaddingSide, optional
          Indicates which side of the character gets the padding. The 
          default is PaddingSide.LEFT, so the character itself gets aligned.
          Set to PaddingSide.RIGHT to preserve the character's position and
          instead align the non-space text to the right of the character.
    """
    from Npp import editor

    #----------------------------------------------------------------------------
    # For the alignment character, take the character just inside the bounds of
    # the selection block (at either the start or the end, as determined below).
    #----------------------------------------------------------------------------
    editor.targetFromSelection()
    selected_text = editor.getTargetText()

    #----------------------------------------------------------------------------
    # Use this code to get the align_char unconditionally from the start
    # of the selection.  If there is no selection, use the character at the
    # current cursor position; if that is unavailable, fall through to the
    # prompt below by treating the alignment character as whitespace.
    #----------------------------------------------------------------------------
    if selected_text:
        align_char = selected_text[0]
    else:
        current_pos = editor.getCurrentPos()
        align_char = editor.getTextRangeFull(current_pos, current_pos + 1)
        if not align_char:
            align_char = ' '

    #----------------------------------------------------------------------------
    # Optionally use this code to get the align_char from within the selection
    # at whichever end has the cursor.
    #----------------------------------------------------------------------------
    # if selected_text:
    #     (startByte, endByte) = editor.getUserCharSelection()
    #     if startByte == editor.getCurrentPos():
    #         align_char = selected_text[0]
    #     else:
    #         align_char = selected_text[-1]
    # else:
    #     current_pos = editor.getCurrentPos()
    #     align_char = editor.getTextRangeFull(current_pos, current_pos + 1)
    #     if not align_char:
    #         align_char = ' '

    # If the character from the selection seems implausible as the
    # align_char, then prompt the user for it.
    if align_char.isspace():
        from Npp import notepad
        global default_align_char
        align_char = notepad.prompt('Align character:',
                                    'Enter Alignment Character',
                                    default_align_char)
        if align_char is not None:
            default_align_char = align_char

    #----------------------------------------------------------------------------
    #%% Get the lines of text within the selected alignment block
    #----------------------------------------------------------------------------
    (startLine, endLine) = editor.getUserLineSelection()
    startPos = editor.positionFromLine(startLine)
    endPos = editor.getLineEndPosition(endLine)
    text_lines = editor.getTextRangeFull(startPos, endPos).splitlines(True)

    #----------------------------------------------------------------------------
    # Remember whether there is a user-selected block, so we can restore a
    # corresponding selection after aligning the text.
    #----------------------------------------------------------------------------
    restore_selection = editor.getSelectionStart() != editor.getSelectionEnd()

    #----------------------------------------------------------------------------
    # Align all instances of align_char within the lines of text
    #----------------------------------------------------------------------------
    if align_char is not None:
        # Enable the following to save the align_char, however it was determined,
        # to be the default_align_char when prompting for it next time.
        # default_align_char = align_char

        if max_align_char_count is None:
            align_char_count = max(line.count(align_char) for line in text_lines)
        else:
            align_char_count = max_align_char_count

        start = 0
        for instance in range(align_char_count):
            # Set the target column using the index of the align_char, ignoring
            # immediately preceding space, or the length of the line
            align_char_cols = [line.find(align_char, start) for line in text_lines]

            # Only lines that contain this instance participate in this pass.
            # A failed find() returns -1, and using that as a slice index would
            # make lines without this instance incorrectly affect the target.
            lines_with_instance = [(line, col)
                                   for (line, col) in zip(text_lines, align_char_cols)
                                   if col &gt;= 0]
            if not lines_with_instance:
                break

            if padding_side == PaddingSide.LEFT:
                # Align the alignment character itself, removing any spaces
                # immediately to its left before inserting the required padding.
                target_col = max(len(line[:col].rstrip())
                                 for (line, col) in lines_with_instance)

                for (idx, line) in enumerate(text_lines):
                    align_char_col = align_char_cols[idx]
                    if align_char_col &gt;= 0:
                        text_lines[idx] = line[:align_char_col].rstrip().ljust(target_col) \
                                        + line[align_char_col:]

                start = target_col + len(align_char)

            elif padding_side == PaddingSide.RIGHT:
                # Align the non-space text after the alignment character.
                # Preserve the alignment character itself, but
                # replace any existing spaces after it with the required
                # padding.
                suffix_starts = []
                for (line, col) in lines_with_instance:
                    suffix_start = col + len(align_char)
                    while suffix_start &lt; len(line) and line[suffix_start] == ' ':
                        suffix_start += 1
                    suffix_starts.append(suffix_start)

                target_col = max(suffix_starts)

                for (idx, line) in enumerate(text_lines):
                    align_char_col = align_char_cols[idx]
                    if align_char_col &gt;= 0:
                        suffix_start = align_char_col + len(align_char)
                        while suffix_start &lt; len(line) and line[suffix_start] == ' ':
                            suffix_start += 1
                        text_lines[idx] = line[:align_char_col + len(align_char)].ljust(target_col) \
                                        + line[suffix_start:]

                start = target_col

            else:
                raise ValueError('Unsupported padding_side: {}'.format(padding_side))

        editor.setTarget(startPos, endPos)
        editor.replaceTarget(''.join(text_lines))

        if restore_selection:
            startPos = editor.positionFromLine(startLine)
            endPos = editor.getLineEndPosition(endLine)
            editor.setSelectionStart(startPos)
            editor.setSelectionEnd(endPos)


if __name__ == '__main__':
    align_selected_text()
</code></pre>
<h1>align_text_1.py:</h1>
<pre><code>from align_text import align_selected_text

align_selected_text(max_align_char_count = 1)
</code></pre>
<h1>align_text_right.py:</h1>
<pre><code>from align_text import align_selected_text, PaddingSide

align_selected_text(padding_side = PaddingSide.RIGHT)
</code></pre>
<h1>align_text_right_1.py:</h1>
<pre><code>from align_text import align_selected_text, PaddingSide

align_selected_text(max_align_char_count = 1,
                    padding_side = PaddingSide.RIGHT)
</code></pre>
]]></description><link>https://community.notepad-plus-plus.org/post/105471</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/105471</guid><dc:creator><![CDATA[sound-fx]]></dc:creator><pubDate>Sun, 17 May 2026 18:51:00 GMT</pubDate></item><item><title><![CDATA[Reply to Scripts to align text on Tue, 18 Nov 2025 16:01:54 GMT]]></title><description><![CDATA[<p dir="auto">The following code supports PythonScript 3.0.23 as well as earlier versions of PythonScript 3.x.</p>
<pre><code>#------------------------------------------------------------------------
# If the character specified in the current selection is a white space,
# then prompt the user to enter the alignment character (or characters),
# using this character as the initial default.
#------------------------------------------------------------------------
default_align_char = ','

from enum import Enum
class PaddingSide(Enum):
    LEFT = 0
    RIGHT = 1


def align_selected_text(max_align_char_count = None,
                        padding_side = PaddingSide.LEFT):
    """Insert padding into the lines in the selection, as needed, to align up to max_align_char_count instances of a specific character or string of characters

    The default is to align all instances of the specific character.

    At present, the alignment character is taken as the character at the top of
    the current selection. You can uncomment some code below to change this
    policy to instead take the alignment character from within the selection at
    whichever end has the cursor. Either way, if that character is white space,
    the user is prompted to type the character (or characters). If you really
    wish to align on a white space character, you can just click OK at the
    prompt.

    When prompted to type the alignment character, the user may enter a
    sequence of characters, e.g., "--&gt;", in which case the alignment is on the
    instances of that entire character sequence. For example, if the user
    enters "--&gt;" at the prompt, then instances of the "-" character get
    aligned only if they're followed immediately by the characters "-&gt;", while
    instances of, say, "-1" and "- " remain unaltered.

    If there is no current selection, then aligns all lines in the editor.
    If there is a current selection, then aligns only the lines that are
    at least partially included in the selection, and the selection is
    changed to the entire block of newly-padded lines.

    Parameters
    ----------
      max_align_char_count : positive integer, optional
          The maximum number of instances to align of the specific
          character.  For example, set to 1 to align only the first
          instance of the character on each line. The default is to
          align all instances of the specific character.
    """
    from Npp import editor

    #----------------------------------------------------------------------------
    # For the alignment character, take the character just inside the bounds of
    # the selection block (at either the start or the end, as determined below).
    #----------------------------------------------------------------------------
    editor.targetFromSelection()
    selected_text = editor.getTargetText()

    # Use this code to get the align_char unconditionally from the start
    # of the selection.
    align_char = selected_text[0]

    # Optionally use this code to get the align_char from within the selection
    # at whichever end has the cursor.
    # (startByte, endByte) = editor.getUserCharSelection()
    # if startByte == editor.getCurrentPos():
    #     align_char = selected_text[0]
    # else:
    #     align_char = selected_text[-1]

    # If the character from the selection seems implausible as the
    # align_char, then prompt the user for it.
    if align_char.isspace():
        from Npp import notepad
        global default_align_char
        align_char = notepad.prompt('Align character:',
                                    'Enter Alignment Character',
                                    default_align_char)
        if align_char is not None:
            default_align_char = align_char

    #----------------------------------------------------------------------------
    #%% Get the lines of text within the selected alignment block
    #----------------------------------------------------------------------------
    (startLine, endLine) = editor.getUserLineSelection()
    startPos = editor.positionFromLine(startLine)
    endPos = editor.getLineEndPosition(endLine)
    text_lines = editor.getTextRangeFull(startPos, endPos).splitlines(True)

    #----------------------------------------------------------------------------
    # Remember whether there is a user-selected block, so we can restore a
    # corresponding selection after aligning the text.
    #----------------------------------------------------------------------------
    restore_selection = editor.getSelectionStart() != editor.getSelectionEnd()

    #----------------------------------------------------------------------------
    # Align all instances of align_char within the lines of text
    #----------------------------------------------------------------------------
    if align_char is not None:
        # Enable the following to save the align_char, however it was determined,
        # to be the default_align_char when prompting for it next time.
        # default_align_char = align_char

        padding_side_offset = padding_side.value * len(align_char)

        if max_align_char_count is None:
            align_char_count = max(line.count(align_char) for line in text_lines)
        else:
            align_char_count = max_align_char_count
        start = 0
        for instance in range(align_char_count):
            # Set the target column using the index of the align_char, ignoring
            # immediately preceding space, or the length of the line
            tgt_char_col = max(len(line[:line.find(align_char, start)].rstrip())
                               for line in text_lines)

            for (idx,line) in enumerate(text_lines):
                align_char_col = line.find(align_char, start)
                if align_char_col &gt;= 0:
                    text_lines[idx] = line[:align_char_col+padding_side_offset].rstrip().ljust(tgt_char_col) \
                                    + line[align_char_col+padding_side_offset:]
            start = tgt_char_col + len(align_char)

        editor.setTarget(startPos, endPos)
        editor.replaceTarget(''.join(text_lines))

        if restore_selection:
            startPos = editor.positionFromLine(startLine)
            endPos = editor.getLineEndPosition(endLine)
            editor.setSelectionStart(startPos)
            editor.setSelectionEnd(endPos)


if __name__ == '__main__':
    align_selected_text()
</code></pre>
]]></description><link>https://community.notepad-plus-plus.org/post/103825</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/103825</guid><dc:creator><![CDATA[sound-fx]]></dc:creator><pubDate>Tue, 18 Nov 2025 16:01:54 GMT</pubDate></item><item><title><![CDATA[Reply to Scripts to align text on Thu, 28 Dec 2023 05:36:47 GMT]]></title><description><![CDATA[<p dir="auto">For what it’s worth, the plugin <a href="https://github.com/Coises/ColumnsPlusPlus/releases" rel="nofollow ugc">Columns++</a> can also do this using the <a href="https://coises.github.io/ColumnsPlusPlus/help.htm#alignment" rel="nofollow ugc">Align…</a> menu option. Alignment can be on the first or last occurrence of a character or string, or on a regular expression.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/91466</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/91466</guid><dc:creator><![CDATA[Coises]]></dc:creator><pubDate>Thu, 28 Dec 2023 05:36:47 GMT</pubDate></item><item><title><![CDATA[Reply to Scripts to align text on Thu, 28 Dec 2023 05:24:10 GMT]]></title><description><![CDATA[<p dir="auto">The code below fixes a bug in the script originally posted.  I’d fixed this bug once before, and it embarrasses me that it somehow got reintroduced. &lt;sigh/&gt;</p>
<hr />
<h2>align_text.py:</h2>
<pre><code>#------------------------------------------------------------------------
# If the character specified in the current selection is a white space,
# then prompt the user to enter the alignment character (or characters),
# using this character as the initial default.
#------------------------------------------------------------------------
default_align_char = ','

def align_selected_text(max_align_char_count = None):
    """Insert padding into the lines in the selection, as needed, to align up to max_align_char_count instances of a specific character or string of characters

    The default is to align all instances of the specific character.

    At present, the alignment character is taken as the character at the top of
    the current selection. You can uncomment some code below to change this
    policy to instead take the alignment character from within the selection at
    whichever end has the cursor. Either way, if that character is white space,
    the user is prompted to type the character (or characters). If you really
    wish to align on a white space character, you can just click OK at the
    prompt.

    When prompted to type the alignment character, the user may enter a
    sequence of characters, e.g., "--&gt;", in which case the alignment is on the
    instances of that entire character sequence. For example, if the user
    enters "--&gt;" at the prompt, then instances of the "-" character get
    aligned only if they're followed immediately by the characters "-&gt;", while
    instances of, say, "-1" and "- " remain unaltered.

    If there is no current selection, then aligns all lines in the editor.
    If there is a current selection, then aligns only the lines that are
    at least partially included in the selection, and the selection is
    changed to the entire block of newly-padded lines.

    Parameters
    ----------
      max_align_char_count : positive integer, optional
          The maximum number of instances to align of the specific
          character.  For example, set to 1 to align only the first
          instance of the character on each line. The default is to
          align all instances of the specific character.
    """
    from Npp import editor

    #----------------------------------------------------------------------------
    # For the alignment character, take the character just inside the bounds of
    # the selection block (at either the start or the end, as determined below).
    #----------------------------------------------------------------------------
    editor.targetFromSelection()
    selected_text = editor.getTargetText()

    # Use this code to get the align_char unconditionally from the start
    # of the selection.
    align_char = selected_text[0]

    # Optionally use this code to get the align_char from within the selection
    # at whichever end has the cursor.
    # (startByte, endByte) = editor.getUserCharSelection()
    # if startByte == editor.getCurrentPos():
    #     align_char = selected_text[0]
    # else:
    #     align_char = selected_text[-1]

    # If the character from the selection seems implausible as the
    # align_char, then prompt the user for it.
    if align_char.isspace():
        from Npp import notepad
        global default_align_char
        align_char = notepad.prompt('Align character:',
                                    'Enter Alignment Character',
                                    default_align_char)
        if align_char is not None:
            default_align_char = align_char

    #----------------------------------------------------------------------------
    #%% Get the lines of text within the selected alignment block
    #----------------------------------------------------------------------------
    (startLine, endLine) = editor.getUserLineSelection()
    startPos = editor.positionFromLine(startLine)
    endPos = editor.getLineEndPosition(endLine)
    text_lines = editor.getTextRange(startPos, endPos).splitlines(True)

    #----------------------------------------------------------------------------
    # Remember whether there is a user-selected block, so we can restore a
    # corresponding selection after aligning the text.
    #----------------------------------------------------------------------------
    restore_selection = editor.getSelectionStart() != editor.getSelectionEnd()

    #----------------------------------------------------------------------------
    # Align all instances of align_char within the lines of text
    #----------------------------------------------------------------------------
    if align_char is not None:
        # Enable the following to save the align_char, however it was determined,
        # to be the default_align_char when prompting for it next time.
        # default_align_char = align_char

        if max_align_char_count is None:
            align_char_count = max(line.count(align_char) for line in text_lines)
        else:
            align_char_count = max_align_char_count
        start = 0
        for instance in range(align_char_count):
            # Set the target column using the index of the align_char, ignoring
            # immediately preceding space, or the length of the line
            tgt_char_col = max(len(line[:line.find(align_char, start)].rstrip())
                               for line in text_lines)

            for (idx,line) in enumerate(text_lines):
                align_char_col = line.find(align_char, start)
                if align_char_col &gt;= 0:
                    text_lines[idx] = line[:align_char_col].rstrip().ljust(tgt_char_col) \
                                    + line[align_char_col:]
            start = tgt_char_col + len(align_char)

        editor.setTarget(startPos, endPos)
        editor.replaceTarget(''.join(text_lines))

        if restore_selection:
            startPos = editor.positionFromLine(startLine)
            endPos = editor.getLineEndPosition(endLine)
            editor.setSelectionStart(startPos)
            editor.setSelectionEnd(endPos)


if __name__ == '__main__':
    align_selected_text()
</code></pre>
]]></description><link>https://community.notepad-plus-plus.org/post/91465</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/91465</guid><dc:creator><![CDATA[sound-fx]]></dc:creator><pubDate>Thu, 28 Dec 2023 05:24:10 GMT</pubDate></item></channel></rss>