Community
    • Login

    Multi selection and multi edit

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    multiline
    64 Posts 18 Posters 214.2k 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.
    • dinkumoilD
      dinkumoil @bitagoras
      last edited by dinkumoil

      @bitagoras

      About to one year ago we had a conversation about this topic here in the forum, have a look at this thread.

      The essentials of the discussion:

      1. You can already use these features using a scripting plugin.

      In the following I provide the code for the NppExec plugin.

      Command SCI_MULTIPLESELECTADDNEXT (with the help of @Ekopalypse):

      sci_sendmsg SCI_SETSEARCHFLAGS SCFIND_WHOLEWORD
      sci_sendmsg 2690  // SCI_TARGETWHOLEDOCUMENT
      sci_sendmsg 2688  // SCI_MULTIPLESELECTADDNEXT
      

      Command SCI_MULTIPLESELECTADDEACH (generously provided by @Ekopalypse):

      sci_sendmsg SCI_SETSEARCHFLAGS SCFIND_WHOLEWORD
      sci_sendmsg 2690  // SCI_TARGETWHOLEDOCUMENT
      
      sci_sendmsg SCI_GETSELECTIONEMPTY
      
      if $(MSG_RESULT) == 1 then
        sci_sendmsg 2688  // SCI_MULTIPLESELECTADDNEXT
       
      sci_sendmsg 2689  // SCI_MULTIPLESELECTADDEACH
      
      1. As these commands are not supported in Notepad++ versions older than v7.7, it is possible to emulate them using a scripting plugin. In the following I provide the Lua code (can be used in conjunction with the LuaScript plugin) to do this:

      Command SCI_MULTIPLESELECTADDNEXT (provided by @dail):

      -- =============================================================================
      -- Add menu entry to select next occurence of word under or next to cursor or
      -- already selected word
      -- =============================================================================
      
      npp.AddShortcut("Selection Add Next", "", function()
        -- From SciTEBase.cxx
        local flags = SCFIND_WHOLEWORD -- can use 0
      
        editor:SetTargetRange(0, editor.TextLength)
        editor.SearchFlags = flags
      
        -- From Editor.cxx
        if editor.SelectionEmpty or not editor.MultipleSelection then
          local startWord = editor:WordStartPosition(editor.CurrentPos, true)
          local endWord   = editor:WordEndPosition(startWord, true)
      
          editor:SetSelection(startWord, endWord)
        else
          local i = editor.MainSelection
          local s = editor:textrange(editor.SelectionNStart[i], editor.SelectionNEnd[i])
          local searchRanges = {{editor.SelectionNEnd[i], editor.TargetEnd}, {editor.TargetStart, editor.SelectionNStart[i]}}
      
          for _, range in pairs(searchRanges) do
            editor:SetTargetRange(range[1], range[2])
      
            if editor:SearchInTarget(s) ~= -1 then
              editor:AddSelection(editor.TargetStart, editor.TargetEnd)
      
              -- To scroll main selection in sight
              editor:ScrollRange(editor.TargetStart, editor.TargetEnd)
      
              break
            end
          end
        end
      
        -- To turn on Notepad++ multi select markers
        editor:LineScroll(0, 1)
        editor:LineScroll(0, -1)
      end)
      

      Command SCI_MULTIPLESELECTADDEACH (devolped by myself by extending above script):

      -- =============================================================================
      -- Add menu entry to select all occurences of word under or next to cursor or
      -- already selected word
      -- =============================================================================
      
      npp.AddShortcut("Selection Add All", "", function()
        local flags     = SCFIND_WHOLEWORD -- can use 0
        local startWord = -1
        local endWord   = -1
        local s         = ""
      
        editor.SearchFlags = flags
      
        if editor.SelectionEmpty or not editor.MultipleSelection then
          startWord = editor:WordStartPosition(editor.CurrentPos, true)
          endWord   = editor:WordEndPosition(startWord, true)
      
          editor:SetSelection(startWord, endWord)
        else
          local i   = editor.MainSelection
      
          startWord = editor.SelectionNStart[i]
          endWord   = editor.SelectionNEnd[i]
        end
      
        s = editor:textrange(startWord, endWord)
      
        while true do
          editor:SetTargetRange(0, editor.TextLength)
      
          local i            = editor.MainSelection
          local searchRanges = {{editor.SelectionNEnd[i], editor.TargetEnd}, {editor.TargetStart, editor.SelectionNStart[i]}}
          local itemFound    = false
      
          for _, range in pairs(searchRanges) do
            editor:SetTargetRange(range[1], range[2])
      
            if editor:SearchInTarget(s) ~= -1 then
              editor:AddSelection(editor.TargetStart, editor.TargetEnd)
              itemFound = true
              break
            end
          end
      
          if editor.TargetStart == startWord and
             editor.TargetEnd   == endWord   or
             not itemFound                   then
            break
          end
        end
      
        -- To turn on Notepad++ multi select markers
        editor:LineScroll(0, 1)
        editor:LineScroll(0, -1)
      end)
      
      bitagorasB 1 Reply Last reply Reply Quote 4
      • bitagorasB
        bitagoras @dinkumoil
        last edited by

        @dinkumoil : Ok thank you, I was not aware. But I still would appreaciate if the two functions would be added to list of scintilla comands at the keyboard shortcuts. Should not be to difficult.

        EkopalypseE 1 Reply Last reply Reply Quote 1
        • EkopalypseE
          Ekopalypse @bitagoras
          last edited by

          @bitagoras
          I just have updated the FR with my test.
          A simple keyboard binding does not work but the solution is
          similar easy.

          1 Reply Last reply Reply Quote 0
          • bitagorasB
            bitagoras
            last edited by

            Here is my entire solution (copy from this Github Issue) with NppExec which gives Notepad++ all the multi editing functionality from VSCode. The only difference is that there are two different shortcuts each for addNext and for skip.

            addNext (Ctrl-D):

            NPP_CONSOLE 0
            sci_sendmsg 2690  // SCI_TARGETWHOLEDOCUMENT
            sci_sendmsg SCI_SETSEARCHFLAGS 0
            sci_sendmsg 2688  // SCI_MULTIPLESELECTADDNEXT
            

            multiSelect_D

            addNextWholeWord (Shift-Ctrl-D):

            NPP_CONSOLE 0
            sci_sendmsg 2690  // SCI_TARGETWHOLEDOCUMENT
            sci_sendmsg SCI_SETSEARCHFLAGS SCFIND_WHOLEWORD
            sci_sendmsg 2688  // SCI_MULTIPLESELECTADDNEXT
            

            multiSelect_DW

            addEach (Shift-Ctrl-L):

            NPP_CONSOLE 0
            sci_sendmsg 2690  // SCI_TARGETWHOLEDOCUMENT
            sci_sendmsg SCI_GETSELECTIONEMPTY
            if $(MSG_RESULT) == 1 then
              sci_sendmsg SCI_SETSEARCHFLAGS SCFIND_WHOLEWORD
            else
              sci_sendmsg SCI_SETSEARCHFLAGS 0
            endif
            sci_sendmsg 2689  // SCI_MULTIPLESELECTADDEACH
            sci_sendmsg 2689  // SCI_MULTIPLESELECTADDEACH
            

            multiSelect_EACH

            undoAddNext (Ctrl-U):

            NPP_CONSOLE 0
            sci_sendmsg SCI_GETSELECTIONS
            set n ~ $(MSG_RESULT) - 1
            sci_sendmsg SCI_DROPSELECTIONN $(n)
            

            multiSelect_DU

            skip (Alt-Ctrl-D):

            NPP_CONSOLE 0
            sci_sendmsg SCI_SETSEARCHFLAGS SCFIND_NONE
            sci_sendmsg 2688  // SCI_MULTIPLESELECTADDNEXT
            sci_sendmsg SCI_GETSELECTIONS
            set n ~ $(MSG_RESULT) - 2
            sci_sendmsg SCI_DROPSELECTIONN $(n)
            

            multiSelect_DS

            skipWholeWord (Alt-Shift-Ctrl-D):

            NPP_CONSOLE 0
            sci_sendmsg SCI_SETSEARCHFLAGS SCFIND_NONE
            sci_sendmsg 2688  // SCI_MULTIPLESELECTADDNEXT
            sci_sendmsg SCI_GETSELECTIONS
            set n ~ $(MSG_RESULT) - 2
            sci_sendmsg SCI_DROPSELECTIONN $(n)
            
            Don BhaiD bitagorasB 2 Replies Last reply Reply Quote 6
            • michaelfernandez98M
              michaelfernandez98
              last edited by

              Hello
              I’m new to this but I’m interested in having the functionality to select similar strings in a document and be able to do multiple editing
              how can i add this functionality to notepad ++?

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

                @michaelfernandez98

                Perhaps the simplest way would be to add the NppExec plugin to your Notepad++ and used the scripts by @bitagoras directly above?

                1 Reply Last reply Reply Quote 2
                • michaelfernandez98M
                  michaelfernandez98
                  last edited by

                  yes i was watching
                  I already installed the NppExec script to the notepad++
                  but now the problem is how do I add the scripts by @bitagoras to the notepad ++?

                  Alan KilbornA PeterJonesP 2 Replies Last reply Reply Quote 0
                  • Alan KilbornA
                    Alan Kilborn @michaelfernandez98
                    last edited by

                    @michaelfernandez98

                    It feels a bit like spoon-feeding, but…

                    • Select Plugins from the N++ menus
                    • Select NppExec
                    • Select Execute (default key assignment is F6)
                    • Paste in ONE of the short “scripts” above in the Execute… box that appears
                    • Optionally (but recommended) test run it by pressing the OK button
                    • Press F6 again; “script” from before was retained and shows in the box
                    • Press Save… , give it a name (suggested above the “scripts” above), press OK
                    • Assign a keycombo (again; suggested ones are above the “scripts” above)
                    1 Reply Last reply Reply Quote 2
                    • PeterJonesP
                      PeterJones @michaelfernandez98
                      last edited by PeterJones

                      @michaelfernandez98 said:

                      but now the problem is how do I add the scripts by @bitagoras to the notepad ++?

                      @Alan-Kilborn replied:

                      …
                      Assign a keycombo (again; suggested ones are above the “scripts” above)

                      If saving and naming a script wasn’t obvious, then assigning a keycombo is going to be less intuitive. Just saying. ;-)

                      @michaelfernandez98,
                      Once you have one or more scripts named and saved as Alan described, then go to Plugins > NppExec > Advanced Options. In the lower left, use the Associated Script pulldown to select the script; click Add/Modify button to add it to the list of Menu Items in the upper left; note whether or not ☑ Place in the Macros Submenu is enabled (you will need this later). Repeat as necessary for all of the above scripts. When you hit OK to apply those changes, a popup will tell you that you should restart Notepad++: save any open files, then restart Notepad++.

                      After the restart, then either the Macros menu (if the checkbox was enabled) or the Plugins > NppExec submenu (if the checkbox was disabled) will list your new NppExec scripts.

                      To assign a keycombo to each, go to Macros > Modify Shortcut/Delete Macro (or, equivalently, Settings > Shortcut Mapper), and go to the Plugin Commands tab. Filter by NppExec and scroll down until you see the new script names. Click on the script you want a keycombo for, hit Modify, and pick the appropriate combination of Ctrl, Alt, Shift, and the selected key. Repeat the Modify procedure for all the scripts that you want a keycombo for.

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

                        @PeterJones

                        Thanks for filling in those details Peter!
                        As I was walking thru the steps on my setup, I didn’t do that part, so I’d forgotten that it was maybe not the most obvious thing.

                        Is there a way to delete scripts that just isn’t obvious to me at the moment?
                        For example, during my experimentation I named my script “junk”.
                        Now I want to get rid of it, but don’t see a way to in the NppExec UI.
                        Or do I have to resort to hand-editing the config file for the plugin?

                        1 Reply Last reply Reply Quote 2
                        • PeterJonesP
                          PeterJones
                          last edited by

                          @Alan-Kilborn said in Multi selection and multi edit:

                          Now I want to get rid of it, but don’t see a way to in the NppExec UI.

                          No obvious way, that’s for sure.

                          The … > Advanced Options > Delete button removes it from the menu, but not from the saved scripts.

                          Oh, right, there it is. F6, select junk, counter-intuitively hit Save…, then click the Delete button.

                          1 Reply Last reply Reply Quote 3
                          • michaelfernandez98M
                            michaelfernandez98
                            last edited by

                            I could already install the plugin and assign the keycombo to it
                            thanks @PeterJones @Alan-Kilborn
                            They were very helpful

                            1 Reply Last reply Reply Quote 3
                            • michaelfernandez98M
                              michaelfernandez98
                              last edited by

                              sorry if my english is bad
                              I speak Spanish

                              1 Reply Last reply Reply Quote 2
                              • Don BhaiD
                                Don Bhai @bitagoras
                                last edited by

                                @bitagoras
                                @Claudia-Frank
                                @dinkumoil
                                i got the plugin and could multi select finally, thanks
                                but i tried to replace 6 words by another corresponding 6 words
                                it worked in vscode that the first selected word replaced by the first word in clipboard and the second word replaced by second word and so on
                                but her in notepad++ every word replaced by the 6 words that in clipboard that makes me crazy
                                please, any one can help? any idea ?any script ?

                                Alan KilbornA astrosofistaA 2 Replies Last reply Reply Quote 0
                                • Alan KilbornA
                                  Alan Kilborn @Don Bhai
                                  last edited by

                                  @Don-Bhai

                                  I’d help with a PythonScript, but maybe you want an NppExec script?
                                  Can’t tell from the above…

                                  Don BhaiD 1 Reply Last reply Reply Quote 0
                                  • guy038G
                                    guy038
                                    last edited by guy038

                                    Hello, @don-bhai, @alan-kilborn and All,

                                    I may misunderstand what you want but are you looking for this kind of manipulations, below, which seems to work nicely ?

                                    I assume that the BetterMultiSelection plugin is installed, with its last 1.4 version ( IMPORTANT )

                                    I personally tested with N++ portable v7.9.2 (32 bits )


                                    So :

                                    • Paste the text, below, beginning with a blank line, in a new tab
                                    
                                    Little 123 test
                                    Little 456 test
                                    Little 789 test
                                    Little 012 test
                                    Little 345 test
                                    Little 678 test
                                    
                                    678
                                    345
                                    012
                                    789
                                    456
                                    123
                                    
                                    • Move the caret at beginning of line 9

                                    • Hold down the Alt and Shift keys and hit 5 times on the Down arrow key

                                    • Hit the Shift + End shortcut, once

                                    • Hit the Ctrl + C shortcut

                                    • Hit, 7 times, on the Up arrow key

                                    • Hit, 4 times, on the Right arrow key

                                    • Hold down the Shift key and hit 3 times on the Right arrow key

                                    • Hit the Ctrl + V shortcut

                                    • Hit the Esc key to cancel the multi-selection !

                                    As you can see, in the picture, below, each number, from line 27 to 32, have been pasted, accordingly, in lines 20 to 25 ;-))

                                    79fd1f93-28db-4867-829f-939b208e87ba-image.png

                                    Best Regards

                                    guy038

                                    Alan KilbornA Don BhaiD 2 Replies Last reply Reply Quote 2
                                    • Alan KilbornA
                                      Alan Kilborn @guy038
                                      last edited by Alan Kilborn

                                      @guy038

                                      I presume OP has this, where the inverted text is the product of “multi-select”:

                                      349843b9-20c3-4b6a-882d-13d61776696b-image.png

                                      and then has this in the clipboard:

                                      Peter
                                      Guy
                                      Claudia
                                      Scott
                                      Alan
                                      Terry
                                      Michael
                                      

                                      And wants to end up with this text after replacement:

                                      879ea221-1581-4cf5-89bf-b6d763b5f794-image.png

                                      Note that I styled the text in the cyan color myself, just so the replacements could be easier to see.

                                      Of course I could be totally wrong about what the OP has and wants. :-)

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

                                        Oops, it appears I’ve turned Guy into a philandering filmmaker, Scott into an infidel, and Michael into an encourager of adultery. Sorry, all! :-)

                                        1 Reply Last reply Reply Quote 2
                                        • Don BhaiD
                                          Don Bhai @Alan Kilborn
                                          last edited by

                                          @Alan-Kilborn
                                          i can install paython plugin if this is not available in NPPEXec script

                                          1 Reply Last reply Reply Quote 0
                                          • astrosofistaA
                                            astrosofista @Don Bhai
                                            last edited by

                                            @Don-Bhai said in Multi selection and multi edit:

                                            but her in notepad++ every word replaced by the 6 words that in clipboard that makes me crazy

                                            As I understand you comment, I think you missed an important condition to get what you want - in order to distribute replacements over a multiselection, you must copy the replacement strings as a rectangular selection, not a normal one.

                                            Hope this helps.

                                            Don BhaiD 1 Reply Last reply Reply Quote 2
                                            • First post
                                              Last post
                                            The Community of users of the Notepad++ text editor.
                                            Powered by NodeBB | Contributors