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.
    • Chandler NewbyC
      Chandler Newby
      last edited by

      I know I can use multi-edit in the following ways:

      • Select something, then hold CTRL and select something else.
      • Hold ALT+SHIFT and use the arrow keys to select multiple lines.
      • Hold ALT and click and drag the mouse to select a block.

      But what I really, really want to be able to do is:

      • Select some text, then hit a keyboard shortcut to find the next copy of that text, and select it along with the first.

      Sublime and Atom do this with CTRL+D. That same shortcut probably won’t work, but the feature would be wonderful.

      Claudia FrankC donhoD 2 Replies Last reply Reply Quote 2
      • Claudia FrankC
        Claudia Frank @Chandler Newby
        last edited by

        Hello @Chandler-Newby,

        I do have a python script which more or less (because I don’t know what sublime does) does what you want.
        It has 3 modes,

        • replace
        • insert before
        • insert afterwards

        To make this work you need to install the python script plugin (please use msi instead of plugin manager),
        create a new script and put the following into it. Assign a shortcut.

        # this script implements the multi cursor edit functionality
        editor.beginUndoAction()
        
        _text = editor.getSelText()
            
        if len(_text) != 0:
                
            _currentPosition = editor.getCurrentPos()
            _current_line = editor.lineFromPosition(_currentPosition)
            _end_Position = editor.getLength()
        
            _word_end_pos = 0
            _current_word_start_pos = editor.getLineSelStartPosition(_current_line)
            _current_word_end_pos = editor.getLineSelEndPosition(_current_line)
            
            incorrect_option = True
            while incorrect_option:
                result = notepad.prompt(' 0 = replace,  1 = before, 2 = afterwards', 'Choose the desired option', '0')
                if result in ['0','1','2']: 
                    incorrect_option = False
        
            if result == '0':
                # replace whole string version
                editor.setEmptySelection(_currentPosition)
                while 1:
                    
                    try:   
                        _word_start_pos, _word_end_pos = editor.findText(0, _word_end_pos, _end_Position, _text)
                        
                        if _word_end_pos != _currentPosition and _word_start_pos != _currentPosition:
                            editor.addSelection(_word_start_pos, _word_end_pos)
                            
                    except TypeError, err:
                        break
        
            elif result == '1':
                # insert before selected string version
                editor.setEmptySelection(_current_word_start_pos)
                while 1:
                    
                    try:   
                        _word_start_pos, _word_end_pos = editor.findText(0, _word_end_pos, _end_Position, _text)
                        
                        if _word_end_pos != _currentPosition and _word_start_pos != _currentPosition:
                            editor.addSelection(_word_start_pos, _word_start_pos)
                        else:
                            _current_word_start_pos, _current_word_end_pos = _word_start_pos, _word_start_pos
                            
                    except TypeError, err:
                        break
        
            elif result == '2':
                # insert after selected string version
                editor.setEmptySelection(_current_word_end_pos)
                while 1:
                    
                    try:   
                        _word_start_pos, _word_end_pos = editor.findText(0, _word_end_pos, _end_Position, _text)
                        
                        if _word_end_pos != _currentPosition and _word_start_pos != _currentPosition:
                            editor.addSelection(_word_end_pos, _word_end_pos)
                        else:
                            _current_word_start_pos, _current_word_end_pos = _word_end_pos, _word_end_pos
                            
                    except TypeError, err:
                        break
               
            # now add the current selection
            editor.addSelection(_current_word_start_pos, _current_word_end_pos)
            
        editor.endUndoAction()
        

        If you want to modify something in the script, let me know.

        Cheers
        Claudia

        ng khanhN 1 Reply Last reply Reply Quote 1
        • dailD
          dail
          last edited by

          Just FYI if/when Notepad++ updates to a newer version of Scintilla this capability will be built-in by default.

          Claudia FrankC 1 Reply Last reply Reply Quote 0
          • Claudia FrankC
            Claudia Frank @dail
            last edited by

            @dail

            I would vote for an update ;-)

            Cheers
            Claudia

            1 Reply Last reply Reply Quote 1
            • dailD
              dail
              last edited by

              Me too. Unfortunately it isn’t updated very often :(

              Vasile CarausV 1 Reply Last reply Reply Quote 0
              • Claudia FrankC
                Claudia Frank
                last edited by

                Well I can understand as it is a critical component which can break npp I guess.
                But if there is something I can do, like comparing source code to see which functions
                have been modified etc., I would do it.

                Cheers
                Claudia

                1 Reply Last reply Reply Quote 0
                • Chandler NewbyC
                  Chandler Newby
                  last edited by

                  Thanks for the script @Claudia-Frank! It works well.

                  It sounds like an updated Scintilla is what we need. Is there any timeline on how often the dev updates the Scintilla version used?

                  1 Reply Last reply Reply Quote 0
                  • dailD
                    dail
                    last edited by

                    The new version of Scintilla doesn’t really introduce anything that can’t be done currently with a plugin, it just makes it more convenient. Updates vary between a few months to a few years

                    I understand the caution updating Scintilla but it still seems like there are many advantages that could be taken with updating it more regularly. Don always takes care of updating it himself.

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

                      Hello Chandler Newby and All,

                      Presently, with current versions of N++, to get the SAME result, you need to perform the five steps below :

                      • Place the cursor, JUST BEFORE the text to search for :

                      • Perform the menu option Edit - Begin/End Select

                      • Select your text ( either a character, a word or a several words string )

                      • Perform the menu option Search - Select and Find Next or use the CTRL + F3 shortcut

                      IMPORTANT : you may run this command several times to get the Nth occurrence of your selected text

                      • Perform, again, the menu option Edit - Begin/End Select

                      => All the area, between the FIRST occurrence of the selected text, included, till the LAST occurrence found, included, is, then, selected

                      Et voilà !

                      Best Regards,

                      guy038

                      1 Reply Last reply Reply Quote 0
                      • ng khanhN
                        ng khanh @Claudia Frank
                        last edited by

                        @Claudia-Frank : Thanks for your script. How need to change if i want to set the range of find and modfication : from line 10 to line 100, it would be best if we can select instead of line number input

                        Scott SumnerS 1 Reply Last reply Reply Quote 0
                        • Scott SumnerS
                          Scott Sumner @ng khanh
                          last edited by

                          @ng-khanh

                          The biggest issue with your desired change is how to specify the range of lines you want to limit it to functioning over. As you said, entering actual line numbers is not ideal (although that would work, and could easily be set in the “prompt” box along with the 0, 1, 2 choice).

                          How about the placement of two bookmarks, one on the first line and one on the last line? This presumes that you currently aren’t using bookmarks for something else when you want to invoke this functionality.

                          1 Reply Last reply Reply Quote 0
                          • Claudia FrankC
                            Claudia Frank
                            last edited by Claudia Frank

                            Bookmarks is nice because than it can be used with F2 to jump to.
                            Another, possible way, could be that only the visible area gets changed or
                            if you select the first and the last (by holding ctrl) and everything in between,
                            together with the selected once, get replaced.
                            Or we try to extend it so it does all of this.
                            So with a prompt like

                            0 = replace,  1 = before, 2 = afterwards
                            a = all, b = bookmarks, s = selected, v = visible,
                            

                            So 0a would mean replace all occurrences and 1b would
                            be insert before selected word within bookmared lines etc…

                            Cheers
                            Claudia

                            1 Reply Last reply Reply Quote 0
                            • Claudia FrankC
                              Claudia Frank
                              last edited by Claudia Frank

                              Below the modified script.
                              Some words to explain its functionality.
                              If you run the script you will be asked for some options,
                              namely the area which should be used and the mode what to do.

                              Mode is either

                              • 0=replace
                              • 1=insert before
                              • 2=insert after

                              Area is either

                              • a=all lines
                              • b=position enclosed by two bookmarks
                              • s=position enclosed by two selected words
                              • v=lines in the view which are currently visible

                              So if one defines 0a it means every occurrence of the selected word gets overwritten with the current word
                              If one chooses 1b then only the selected words within the bookmarks, which are needed to be set first, get modified by
                              inserting the typed chars before the words.
                              2v would mean add additional chars to the end of the selected words, but only for those who are currently visible.
                              Well, this is not exactly true, it means that text gets added to every selected item from first visible line
                              including the last visible line.
                              Example:
                              Line 1-20 is visible
                              Line 21-30 is hidden
                              Line 31-40 is visible
                              Line 41-… exist but is out of view aka non-visible

                              If one selects area v then the words in the lines 21-30 get considered(modified/replaced) too.

                              Area s can only be used if settings->preferences->editing->Multi-Editing has been enabled.

                              Last but not least, there is a variable find_flag which is set to WHOLEWORD per default.
                              If one once to have no restriction set it to 0 or any other value stated in the comment.

                              That’s it.

                              Cheers
                              Claudia

                              # 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()
                              
                              _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: 
                                      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)
                              
                              editor.endUndoAction()
                              
                              1 Reply Last reply Reply Quote 0
                              • guy038G
                                guy038
                                last edited by

                                Hi, Claudia,

                                Until your recent script, I didn’t interfere, in that discussion, because I thought that a simple S/R, in regex mode, was enough to get the same behaviour. Indeed :

                                • For Replacement : SEARCH = "Searched word" and REPLACEMENT = "Replacement word"

                                • For Insertion before : SEARCH = "Searched word" and REPLACEMENT = "Added word${0}"

                                • For Insertion after : SEARCH = "Searched word" and REPLACEMENT = "${0}Added word"


                                But, your last script, with the different area options, shows, clearly, the advantages of a script ;-)) Your script should satisfy everyone !

                                However, I just noticed a minor bug : if you start the script by mistake, and that you want to stop it, the Cancel button does NOT work. So, the only choice is the Yes button !

                                Cheers,

                                guy038

                                BTW, if your file is quite important, it can take some seconds, before all the matches are highlighted !!

                                Claudia FrankC 1 Reply Last reply Reply Quote 1
                                • Vasile CarausV
                                  Vasile Caraus @dail
                                  last edited by

                                  @dail

                                  agree

                                  1 Reply Last reply Reply Quote 0
                                  • Claudia FrankC
                                    Claudia Frank @guy038
                                    last edited by

                                    @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 Reply Last reply Reply Quote 0
                                    • Scott SumnerS
                                      Scott Sumner @Claudia Frank
                                      last edited by

                                      @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 Reply Last reply Reply Quote 1
                                      • Claudia FrankC
                                        Claudia Frank @Scott Sumner
                                        last edited by

                                        @Scott-Sumner

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

                                        Cheers
                                        Claudia

                                        1 Reply Last reply Reply Quote 0
                                        • Claudia FrankC
                                          Claudia Frank
                                          last edited by

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

                                          Cheers
                                          Claudia

                                          1 Reply Last reply Reply Quote 1
                                          • Claudia FrankC
                                            Claudia Frank
                                            last edited by

                                            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 Reply Last reply Reply Quote 2
                                            • First post
                                              Last post
                                            The Community of users of the Notepad++ text editor.
                                            Powered by NodeBB | Contributors