Community
    • Login

    remove Space & Break line while Copying a line in notepad++

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    14 Posts 5 Posters 3.9k Views
    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.
    • Alan KilbornA
      Alan Kilborn
      last edited by

      it copies an extra space

      I can confirm that you get the line break copied after triple click, but no extra space.

      If the original line ends with a space character, triple-clicking would get this space in the selection.


      How to just copy it without spaces and break line?

      Steps:

      • do the triple click
      • press Shift+leftarrow
      • press Ctrl+c
      SultanS 1 Reply Last reply Reply Quote 1
      • SultanS
        Sultan @Alan Kilborn
        last edited by

        @Alan-Kilborn this is too tough to copy
        I just want to highlight and copy real quick.
        How about deleting the space character at the end of the line??

        Alan KilbornA 1 Reply Last reply Reply Quote 0
        • Alan KilbornA
          Alan Kilborn @Sultan
          last edited by PeterJones

          @Sultan said in remove Space & Break line while Copying a line in notepad++:

          this is too tough to copy

          If you stick to keyboard actions, you can make a MACRO of the whole sequence:

          • press Home twice
          • press Shift+End
          • press Ctrl+c

          Then assign the macro to a keycombo.
          Can’t be any quicker than that.

          1 Reply Last reply Reply Quote 3
          • Alan KilbornA
            Alan Kilborn
            last edited by

            So the OP’s desire to select to end of line content, but not any trailing whitespace, got me thinking about how using the Shift+End keycombo works in comparison to Shift+Home.

            Shift+End will select to the end of data (be it whitespace or not), just before the end-of-line character(s) on a line. Shift+Home, however, selects from current position to the first non-whitespace on a line. If, after doing Shift+Home the first time, the user presses it again, THEN it will extend the selection to column 1.

            But really, why this difference? Why shouldn’t Shift+End first select to the last non-whitespace character on a line, then a subsequent re-press of that keycombo selects the remaining (less line-ending char(s))? I mean, one could argue it either way, but I say “why not act similarly”?

            So I wrote a script to provide this functionality for Shift+End (and End alone as well). I call the script EndKeyReplacementFunctionality.py and here it is:

            # -*- coding: utf-8 -*-
            from __future__ import print_function
            
            # references:
            #  https://community.notepad-plus-plus.org/topic/23381
            
            from Npp import *
            from ctypes import (WinDLL)
            
            #-------------------------------------------------------------------------------
            
            class EKRF(object):
            
                def __init__(self):
                    VK_SHIFT = 0x10
                    is_selecting = (WinDLL('user32').GetAsyncKeyState(VK_SHIFT) & 0x8000) != 0
                    if editor.selectionIsRectangle():
                        editor.lineEndRectExt() if is_selecting else editor.lineEndDisplay()
                        return
                    if editor.getSelections() > 1:
                        editor.lineEndWrapExtend() if is_selecting else editor.lineEndWrap()
                        return
                    caret_pos = editor.getCurrentPos()
                    cur_line = editor.lineFromPosition(caret_pos)
                    cur_line_end_pos = editor.getLineEndPosition(cur_line)
                    match_pos_list = []
                    editor.research(r'\h+$', lambda m: match_pos_list.append(m.span(0)[0]), 0, editor.positionFromLine(cur_line), cur_line_end_pos, 1)
                    new_caret_pos = cur_line_end_pos if (len(match_pos_list) == 0 or caret_pos == match_pos_list[0]) else match_pos_list[0]
                    editor.setSelection(new_caret_pos, editor.getAnchor() if is_selecting else new_caret_pos)
            
            #-------------------------------------------------------------------------------
            
            if __name__ == '__main__': EKRF()
            

            To use the script, remap the shortcut for the Shift+End keycombo from its default function to run this script instead.

            To have the functionality for the End key alone requires a second script with a similar keycombo remapping. The scripts can be identical; what I did was just copy EndKeyReplacementFunctionality.py to EndKeyReplacementFunctionality2.py.

            Andi KiisselA 1 Reply Last reply Reply Quote 2
            • Alan KilbornA Alan Kilborn referenced this topic on
            • Andi KiisselA
              Andi Kiissel @Alan Kilborn
              last edited by

              @Alan-Kilborn
              Home and End different behavior with whitespaces disturbed me too. This script is very clever, thank you!
              I’d suggest to add editor.chooseCaretX() after editor.setSelection... to remember caret new horizontal position for next up/down moves.
              Remapping of End and Shift+End works even with conflicts with Scintilla default commands, but I don’t know, what exactly is going on in behind :) These keys can be disabled in Scintilla with SCI_CLEARCMDKEY, but they remain in N++ Shortcut Mapper as conflicts. May this be a problem somehow, I don’t know.
              Thanks again for this useful idea and script!

              Alan KilbornA 1 Reply Last reply Reply Quote 3
              • Alan KilbornA
                Alan Kilborn @Andi Kiissel
                last edited by Alan Kilborn

                @Andi-Kiissel said:

                I’d suggest to add editor.chooseCaretX() after editor.setSelection… to remember caret new horizontal position for next up/down moves

                Yes, that’s a very good idea.


                @Andi-Kiissel said :

                Remapping of End and Shift+End works even with conflicts with Scintilla default commands, but I don’t know, what exactly is going on in behind :) These keys can be disabled in Scintilla with SCI_CLEARCMDKEY, but they remain in N++ Shortcut Mapper as conflicts. May this be a problem somehow, I don’t know.

                I do know something about how keys are processed, but I think the easiest/safest thing if someone wants to use the script(s) here is to first go in and unmap End and Shift+End, then assign them to the script(s).

                Andi KiisselA 1 Reply Last reply Reply Quote 2
                • Andi KiisselA
                  Andi Kiissel @Alan Kilborn
                  last edited by

                  @Alan-Kilborn said:

                  I do know something about how keys are processed, but I think the easiest/safest thing if someone wants to use the script(s) here is to first go in and unmap End and Shift+End, then assign them to the script(s).

                  I didn’t realize until now that if Remove isn’t available and None with OK don’t work, then None with Apply do!
                  Thanks for emphasizing that it’s best to unmap those keys first!

                  Alan KilbornA PeterJonesP 2 Replies Last reply Reply Quote 1
                  • Alan KilbornA
                    Alan Kilborn @Andi Kiissel
                    last edited by

                    @Andi-Kiissel said in remove Space & Break line while Copying a line in notepad++:

                    I didn’t realize until now that if Remove isn’t available and None with OK don’t work, then None with Apply do!

                    See:

                    • https://github.com/notepad-plus-plus/notepad-plus-plus/issues/12040#:~:text=Remove button is disabled and user has to resort to the unintuitive method of picking None from the dropdown and clicking Apply

                    • https://github.com/notepad-plus-plus/notepad-plus-plus/issues/12034

                    Andi KiisselA 1 Reply Last reply Reply Quote 1
                    • PeterJonesP
                      PeterJones @Andi Kiissel
                      last edited by PeterJones

                      @Andi-Kiissel said in remove Space & Break line while Copying a line in notepad++:

                      I didn’t realize until now that if Remove isn’t available and None with OK don’t work, then None with Apply do!
                      Thanks for emphasizing that it’s best to unmap those keys first!

                      BTW: per Alan’s recent prompting, the documentation has been updated in the github copy of the online user manual to reflect that oddity. The next time the usermanual is pushed to npp-user-manual.org (probably after the next release of Notepad++ (v8.4.5 or v8.5 or whatever it’s called)), the updated instructions will be there.

                      1 Reply Last reply Reply Quote 2
                      • Andi KiisselA
                        Andi Kiissel @Alan Kilborn
                        last edited by

                        @Alan-Kilborn

                        I propose one more significant change to this script:
                        If the line is longer than width of the window and WordWrap=OFF then with editor.setSelection the caret (cursor) remains invisible (outside the window).
                        Better to use
                        editor.setSel(editor.getAnchor() if is_selecting else new_caret_pos, new_caret_pos) (NB! arguments swapped!)
                        which moves the view as needed.
                        With WordWrap=ON this script behaves differently than Scintilla End/Shift+End default, but that’s OK for me :)

                        Alan KilbornA 2 Replies Last reply Reply Quote 3
                        • Alan KilbornA
                          Alan Kilborn @Andi Kiissel
                          last edited by

                          @Andi-Kiissel said in remove Space & Break line while Copying a line in notepad++:

                          editor.setSel(editor.getAnchor() if is_selecting else new_caret_pos, new_caret_pos) (NB! arguments swapped!)

                          Another good suggestion. :-)

                          1 Reply Last reply Reply Quote 0
                          • Alan KilbornA
                            Alan Kilborn @Andi Kiissel
                            last edited by

                            @Andi-Kiissel said i:

                            With WordWrap=ON this script behaves differently than Scintilla End/Shift+End default, but that’s OK for me

                            I considered this case, but I didn’t see an easy way to handle it.
                            As line “breaks” caused by word-wrap are artificial, it would be extremely rare for me to want to move to one.
                            So wrap consideration got defeatured from the spec for this problem. :-)

                            1 Reply Last reply Reply Quote 2
                            • Alan KilbornA Alan Kilborn referenced this topic on
                            • First post
                              Last post
                            The Community of users of the Notepad++ text editor.
                            Powered by NodeBB | Contributors