Community
    • 登入

    Multi selection and multi edit

    已排程 已置頂 已鎖定 已移動 Help wanted · · · – – – · · ·
    multiline
    64 貼文 18 Posters 231.7k 瀏覽
    正在載入更多貼文
    • 從舊到新
    • 從新到舊
    • 最多點贊
    回覆
    • 在新貼文中回覆
    登入後回覆
    此主題已被刪除。只有擁有主題管理權限的使用者可以查看。
    • Vasile CarausV
      Vasile Caraus @dail
      最後由 編輯

      @dail

      agree

      1 條回覆 最後回覆 回覆 引用 0
      • Claudia FrankC
        Claudia Frank @guy038
        最後由 編輯

        @guy038

        there is no need for a stop routine as it is a per one time execution script - no callbacks.
        You run the script and all it does is to select the different instances of the selected word.
        Once everything has been selected it finishes instantly.

        You are right, it hasn’t optimized for speed but I think it doesn’t make sense to edit
        a huge file and use this little helper to replace all occurrences of a string. Npps
        builtin feature is much faster in this way.

        Cheers
        Claudia

        Scott SumnerS 1 條回覆 最後回覆 回覆 引用 0
        • Scott SumnerS
          Scott Sumner @Claudia Frank
          最後由 編輯

          @Claudia-Frank

          I think what @guy038 meant was, what if you start the script running, get the prompt dialog box on the screen, and then change your mind and decide you don’t want to do what you started after all.

          Claudia FrankC 1 條回覆 最後回覆 回覆 引用 1
          • Claudia FrankC
            Claudia Frank @Scott Sumner
            最後由 編輯

            @Scott-Sumner

            ahh - ok - yes correct - but just press ok and move cursor.
            Nothing changed.

            Cheers
            Claudia

            1 條回覆 最後回覆 回覆 引用 0
            • Claudia FrankC
              Claudia Frank
              最後由 編輯

              finally I understand - the cancel button !! phew – need a cup of coffee!

              Cheers
              Claudia

              1 條回覆 最後回覆 回覆 引用 1
              • Claudia FrankC
                Claudia Frank
                最後由 編輯

                cancel button fix

                # this script implements the enhanced multi cursor edit functionality
                
                def default_positions():
                    return 0, editor.getLength()
                
                def get_pos_of_bookmarks():
                    npp_bookmark_marker_id_number = 24
                    npp_bookmark_marker_mask = 1 << npp_bookmark_marker_id_number
                    _start_position, _end_position = default_positions()
                    
                    line_nbr = editor.markerNext(_start_position, npp_bookmark_marker_mask)
                    if line_nbr != -1:
                        _start_position = editor.positionFromLine(line_nbr)
                        line_nbr = editor.markerNext(line_nbr + 1, npp_bookmark_marker_mask)
                        if line_nbr != -1:
                            _end_position = editor.getLineEndPosition(line_nbr)
                    return _start_position, _end_position
                
                def get_pos_of_visible_lines():
                    first_visible_line = editor.getFirstVisibleLine()
                    _start_position = editor.positionFromLine(first_visible_line)
                    lines_visible = editor.linesOnScreen()
                    last_visible_line = editor.docLineFromVisible(first_visible_line+lines_visible)
                    _end_position = editor.getLineEndPosition(last_visible_line)
                    return _start_position, _end_position
                
                def get_pos_of_selections():
                    _start_position, _end_position = default_positions()
                    if editor.getSelections() == 2:
                        _start_position = editor.getSelectionNStart(0)
                        _end_position = editor.getSelectionNEnd(1)
                    return _start_position, _end_position
                
                
                area_dict = {'a':default_positions,
                             'b':get_pos_of_bookmarks,
                             's':get_pos_of_selections,
                             'v':get_pos_of_visible_lines}
                    
                editor.beginUndoAction()
                
                def Main():
                    _text = editor.getTextRange(editor.getSelectionNStart(0), editor.getSelectionNEnd(0))
                    if len(_text) != 0:
                
                        _current_position = editor.getCurrentPos()
                        _current_line = editor.lineFromPosition(_current_position)
                        _current_word_start_pos = editor.getLineSelStartPosition(_current_line)
                        _current_word_end_pos = editor.getLineSelEndPosition(_current_line)
                
                        find_flag = 2 # 0=DEFAULT, 2=WHOLEWORD 4=MATCHCASE 6=WHOLEWORD | MATCHCASE
                        mode_options = ' 0=replace,  1=before,  2=afterwards\n'
                        area_options = ' a=all, b=bookmarks, s=selected, v=visible'
                        expected_results = [x+y for x in ['0','1','2'] for y in ['a','b','s','v']]
                           
                        result = notepad.prompt(mode_options + area_options, 'Choose the desired option', '0a')
                        while result not in expected_results: 
                            if result is None:
                                return
                            result = notepad.prompt(mode_options + area_options, 'Choose the desired option', '0a')
                
                        chosen_mode, chosen_area = result
                        area_start_position, area_end_position = area_dict[chosen_area]()
                        
                        if chosen_mode == '0': # replace whole string version
                            editor.setEmptySelection(_current_position)       
                            position_tuple = editor.findText(find_flag, area_start_position, area_end_position, _text)
                            
                            while position_tuple is not None:
                                if _current_position not in position_tuple:
                                    editor.addSelection(*position_tuple)
                                position_tuple = editor.findText(find_flag, position_tuple[1], area_end_position, _text)
                                    
                
                        elif chosen_mode == '1': # insert before selected string version
                            editor.setEmptySelection(_current_word_start_pos)
                            position_tuple = editor.findText(find_flag, area_start_position, area_end_position, _text)
                            
                            while position_tuple is not None: 
                                startpos, endpos = position_tuple
                                if startpos != _current_position and endpos != _current_position:
                                    editor.addSelection(startpos, startpos)
                                else:
                                    _current_word_start_pos, _current_word_end_pos = startpos, startpos
                                position_tuple = editor.findText(find_flag, endpos, area_end_position, _text)
                
                
                        elif chosen_mode == '2': # insert after selected string version
                            editor.setEmptySelection(_current_word_end_pos)
                            position_tuple = editor.findText(find_flag, area_start_position, area_end_position, _text)
                            
                            while position_tuple is not None: 
                                startpos, endpos = position_tuple
                                if startpos != _current_position and endpos != _current_position:
                                    editor.addSelection(endpos, endpos)
                                else:
                                    _current_word_start_pos, _current_word_end_pos = endpos, endpos
                                position_tuple = editor.findText(find_flag, endpos, area_end_position, _text)
                
                                
                        # now add the current selection
                        editor.addSelection(_current_word_start_pos, _current_word_end_pos)
                
                Main()
                editor.endUndoAction()
                

                Cheers
                Claudia

                1 條回覆 最後回覆 回覆 引用 2
                • kpymK
                  kpym
                  最後由 編輯

                  I want to advocate too for CTRL+D sublime-like build-in shortcut.
                  This is the most useful way to use multi-cursors :

                  • add next identical
                  • skip next identical
                  • select all identicals
                  1 條回覆 最後回覆 回覆 引用 2
                  • Alan KilbornA
                    Alan Kilborn
                    最後由 編輯

                    @kpym

                    So maybe on the surface this seems different, but this is really just a Find and Replace operation, for which Notepad++ already has a good solution for. Here’s a comparision:

                    • your ctrl+d <–> N++'s press ctrl+h, fill in replace field
                    • your add/skip next identical <–> N++ either “Find Next” or “Replace”
                    • your select all identicals <–> N++ “Replace All”
                    • your “whatever action you would do on what is multiselected…” <–> N++'s nothing to do, it’s already done!
                    1 條回覆 最後回覆 回覆 引用 1
                    • bitagorasB
                      bitagoras
                      最後由 編輯

                      @Alan Kilborn
                      What you irgnore in your comment is the fact that Ctrl+D function is mainly used to quickly clone the cursor at positions of identical strings and not neccessarily to replace the selected. Once you have multiple cursors, you can move them and do much more things than just renaming. I uploaded a gif animation that shows what I mean:

                      https://gifyu.com/image/TaVW

                      It’s really magic what you can to with it and safes you a lot of time with a minimum of commands to remember. It is very intuitive and in most of the cases faster than search / replace or macro recording. Sometimes I use VS Code in parallel with Notepad++ only because of this function.

                      What is missing to make full use of this kind of multi editing:

                      • Ctrl+D (or another shortcut) to clone the cursor at similar occurences of the current word or selection.
                      • Multiple cursors should stay persistent until escape is pressed (as in the plugin “better multiselection”).
                      • Ctrl+mouseclick should not only add but also remove one cursor (when Ctrl+D has produced an unwanted cursor).
                      • Every cursor should have an indiviual clipboard.
                      Scott SumnerS 1 條回覆 最後回覆 回覆 引用 1
                      • Scott SumnerS
                        Scott Sumner @bitagoras
                        最後由 編輯

                        @bitagoras

                        This kind of functionality really has nothing to do with Notepad++ itself, but rather is Scintilla-related. Scintilla in Notepad++ is known to be very outdated. Would upgrading Scintilla to the latest version provide such functionality? I don’t know, but since remaining current with Scintilla doesn’t seem to be a priority of Notepad++ developers, it probably isn’t worth much discussion here. :-(

                        1 條回覆 最後回覆 回覆 引用 1
                        • bitagorasB
                          bitagoras
                          最後由 編輯

                          I think with a little bit of programming it could be possible with scintilla but it is not a standard feature that can just be activated.

                          But you say the Notepad++ developers plan to replace Scintilla? Do you know any details? I haven’t seen any discussion here on this. I wonder, if this wouldn’t be just another new editor but not Notepad++ any more.

                          Scott SumnerS 1 條回覆 最後回覆 回覆 引用 0
                          • Scott SumnerS
                            Scott Sumner @bitagoras
                            最後由 編輯

                            @bitagoras said:

                            but it is not a standard feature that can just be activated

                            Maybe not, I didn’t investigate–but I seem to recall that some of the “fancier” multi-selection stuff is available in newer Scintilla versions…but I don’t know if it takes it to the level of your examples. I really don’t care…until Notepad++ updates to something newer, and THEN I’ll take the time to explore what new features it provides.

                            you say the Notepad++ developers plan to replace Scintilla?

                            I most certainly did NOT say that! :-)

                            1 條回覆 最後回覆 回覆 引用 0
                            • guy038G
                              guy038
                              最後由 編輯

                              Hello, @bitagoras, @scott-sumner and All,

                              @bitagoras, why don’t you use the excellent @Dail plugin BetterMultiselection version v1.3 ? See, below :

                              https://github.com/dail8859/BetterMultiSelection/

                              https://github.com/dail8859/BetterMultiSelection/releases

                              https://notepad-plus-plus.org/community/topic/13653/new-plugin-bettermultiselection

                              Note that, if you have the last v7.6 version of N++, you’ll have to :

                              • Create the new directory BetterMultiSelection, under your active plugins folder

                              • Place the BetterMultiSelection.dll inside the BetterMultiSelection folder

                              Now, assuming the test test, below :

                              abcde fghijk lmno pqr..stuv wxyz 012345789
                              abcde fghijk lmno pqr..stuv wxyz 012345789
                              abcde fghijk lmno pqr..stuv wxyz 012345789
                              abcde fghijk lmno pqr><stuv wxyz 012345789
                              abcde fghijk lmno pqr><stuv wxyz 012345789
                              abcde fghijk lmno pqr><stuv wxyz 012345789
                              abcde fghijk lmno pqr..stuv wxyz 012345789
                              abcde fghijk lmno pqr..stuv wxyz 012345789
                              abcde fghijk lmno pqr..stuv wxyz 012345789
                              
                              • Create a column-mode selection, on the 3 middle lines, between the angle brackets ><

                              • And, now have fun, playing around, with any of the following keys or shortcuts, below ;-))

                                • Home , End , Left , Right , Ctrl + Left , Ctrl + Right

                                • Shift + Home, Shift + End , Shift + Left, Shift + Right , Ctrl + Shift + Left , Ctrl + Shift + Right

                                • Backspace , Delete , Ctrl + Backspace , Ctrl + Delete

                                • Up, Down , Shift + Up, Shift + Down

                                • Enter , Tab , Shift + Tab

                              Just a pleasure, isn’t it ? Many thanks, again, @dail, for this very useful plugin, while waiting for this native behavior in N++, after a future update of Scintilla !

                              Though I suppose,it will not happen soon, as I guess that @don-ho is quite busy with the new plugin’s organization :-)

                              Best Regards,

                              P.S. :

                              You may be interested, too, by this other @dail useful plugin SurroundSelection.dll. See, below :

                              https://github.com/dail8859/SurroundSelection/

                              https://github.com/dail8859/SurroundSelection/releases

                              https://notepad-plus-plus.org/community/topic/13610/new-plugin-surroundselection

                              1 條回覆 最後回覆 回覆 引用 2
                              • bitagorasB
                                bitagoras
                                最後由 編輯

                                @Scott-Sumner

                                I most certainly did NOT say that! :-)

                                Sorry that I misunderstood you sentence. What you say is that it is a priority to upgrade to a new Scintilla version rather than tinker with the current version. I agree.

                                @guy038,

                                Yes BetterMultiSelection works very well. But the mulit clipboard and the Ctrl+D would make it perfekt.

                                Here the example again. I just figured out how to embedd the GIF.

                                Scott SumnerS 1 條回覆 最後回覆 回覆 引用 2
                                • Scott SumnerS
                                  Scott Sumner @bitagoras
                                  最後由 編輯

                                  @bitagoras said:

                                  What you say is that it is a priority to upgrade to a new Scintilla version rather than tinker with the current version

                                  I didn’t say THAT either (I don’t know WHAT if any Scintilla-based decisions are being made, or how they are made), but I agree as well. :-)

                                  I just figured out how to embedd the GIF.

                                  I don’t see the embedded GIF. But it is OK since it is available via the earlier link you posted.

                                  bitagorasB 1 條回覆 最後回覆 回覆 引用 0
                                  • bitagorasB
                                    bitagoras @Scott Sumner
                                    最後由 編輯

                                    @Scott-Sumner

                                    ok but I think I got your point ;-)

                                    Probably this GIF is only visible from my IP.

                                    1 條回覆 最後回覆 回覆 引用 0
                                    • PeterJonesP
                                      PeterJones
                                      最後由 編輯

                                      I can see the animated gif. It may be that Scott’s connection is being filtered from seeing s2.gifyu.com – I know that my employer blocks many file-sharing sites; fortunately, imgur is not one of them (amazingly enough), so I can upload to and see embedded images from imgur, which is quite helpful here.

                                      1 條回覆 最後回覆 回覆 引用 2
                                      • guy038G
                                        guy038
                                        最後由 guy038 編輯

                                        Hi, @bitagoras, and All,

                                        I found out a work-around to simulate the behavior of your animated gif picture !

                                        I suppose 3 hypotheses :

                                        • The Multi-Editing feature is enabled ( Settings > Preferences… > Editing > Multi-Editing settings )

                                        • The BetterMultiSelection plugin is installed and enabled

                                        • The Ctrl+ D shortcut is assigned to the Scintilla function SCI_SELECTION DUPLICATE ( and not to SCI_LINEDUPLICATE ! )


                                        So, assuming this sample test, below , in a new tab :

                                        var1, var2, object1, object2, object3, flag2, flag3
                                        
                                        • Place the cursor right before the string var1 ( without the Ctrl key pressed )

                                        • Holding down a Ctrl key, left click, right in front of all the other strings, in order to get a total of 7 cursors !

                                        Now :

                                        • Backspace * 2

                                        • Enter

                                        You should get a 7- lines selection, below :

                                        var1
                                        var2
                                        object1
                                        object2
                                        object3
                                        flag2
                                        flag3
                                        

                                        Note that the SP notation, below, represents a hit on the Space bar

                                        • Type obj.

                                        • Shift + End

                                        • Ctrl + D

                                        • Right

                                        • Type SP=SParg["

                                        • Shift + End

                                        • Ctrl + D

                                        • Right

                                        • Type "]SP#SPDefinitionSPofSP

                                        • Esc

                                        You obtain the following text :

                                        obj.var1 = arg["var1"] # Definition of var1
                                        obj.var2 = arg["var2"] # Definition of var2
                                        obj.object1 = arg["object1"] # Definition of object1
                                        obj.object2 = arg["object2"] # Definition of object2
                                        obj.object3 = arg["object3"] # Definition of object3
                                        obj.flag2 = arg["flag2"] # Definition of flag2
                                        obj.flag3 = arg["flag3"] # Definition of flag3
                                        

                                        Now, regarding the second block, we start, again, with the following text :

                                        var1, var2, object1, object2, object3, flag2, flag3
                                        
                                        • Place the cursor right before the string var1 ( without the Ctrl key pressed )

                                        • Holding down a Ctrl key, left click, right in front of all the other strings, in order to get a total of 7 cursors

                                        Then :

                                        • Backspace * 2

                                        • Enter

                                        Again , you’ll obtain the 7- lines selection, below :

                                        var1
                                        var2
                                        object1
                                        object2
                                        object3
                                        flag2
                                        flag3
                                        

                                        Now :

                                        • Tab

                                        • Type "

                                        • Shift + End

                                        • Ctrl + D

                                        • Right

                                        • Type ":SPobj.

                                        • End

                                        • Type ,

                                        • Esc

                                        And… you get the expected text, with a leading tabulation :

                                        	"var1": obj.var1,
                                        	"var2": obj.var2,
                                        	"object1": obj.object1,
                                        	"object2": obj.object2,
                                        	"object3": obj.object3,
                                        	"flag2": obj.flag2,
                                        	"flag3": obj.flag3,
                                        

                                        Cheers,

                                        guy038

                                        1 條回覆 最後回覆 回覆 引用 2
                                        • cipher-1024C
                                          cipher-1024
                                          最後由 編輯

                                          I don’t know HOW I missed @dali 's plugin for this long! I have to admit whenever I saw someone doing multiline work I was extremely jealous and tempted to try the VS Code waters. @guy038, what is your CTRL-D mapped to? CTRL-D is mapped to “Duplicate Current Line” for me. When I follow your instructions, I just get duplicate lines. If I try to copy and paste, the paste is just all of the selections globbed together. I can’t seem to duplicate just the selection (like it does in the gif on github). This is what I get when I follow your instructions:

                                          	"var1": obj.,
                                          	"var1
                                          	"var2": obj.,
                                          	"var2
                                          	"object1": obj.,
                                          	"object1
                                          	"object2": obj.,
                                          	"object2
                                          	"object3": obj.,
                                          	"object3
                                          	"flag2": obj.,
                                          	"flag2
                                          	"flag3": obj.,
                                          	"flag3
                                          
                                          1 條回覆 最後回覆 回覆 引用 2
                                          • cipher-1024C
                                            cipher-1024
                                            最後由 cipher-1024 編輯

                                            Ohh you rascals! I went into the shortcut mapper and under the Scintilla commands tab there was an unassigned “SCI_SELECTIONDUPLICATE”. I assigned that to Ctrl + D et voila! (as Guy would say)!

                                            Christmas came early this year! Now I’m not the only kid on the block that can’t mulitedit! Thank you @dail and @guy038 for bringing this plugin to my attention!

                                            1 條回覆 最後回覆 回覆 引用 2
                                            • 第一個貼文
                                              最後的貼文
                                            The Community of users of the Notepad++ text editor.
                                            Powered by NodeBB | Contributors