Community
    • Login

    Automatic selection of ALL instances of a searched string, in one go

    Scheduled Pinned Locked Moved General Discussion
    37 Posts 7 Posters 4.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.
    • wonkawillyW
      wonkawilly @Alan Kilborn
      last edited by

      @Alan-Kilborn said in Automatic selection of ALL instances of a searched string, in one go:

      @wonkawilly said in Automatic selection of ALL instances of a searched string, in one go:

      Automatic selection of ALL instances of a searched string, in one go should be implemented into the Search Replace dialog of Npp

      And the reason for this is…?
      Please be specific and give tangible advantages for needing to do this.

      Very trivial Scenarios

      1. Suppose a user has a file that contains data that needs be to updated. That data is in columns: in this scenario usually it is used column / rectangular selection and, for example, column editor to add a progressive number. Easy and fast.

      2. But in the moment that the data that must be updated is no more in column the user can’t use rectangular selection so neither column editor at the moment. The user will need to do it manually or to look for a work around of some kind like selecting by hand each one of the instances with all the consequences in term of time spend and even the risk to forget someone of them not processed.

      So a feature that allows selecting all instances of a search string or a RegEx pattern will solve this issue in seconds instead of going for a workaround because Column Editor will work as well on selections that are spread around in the file even if not in column.

      Also in find and replace dialog there is the possibility to apply a style to all matches but the use of that feature is less important than selection of all instances of matches because just highlighting them with a nice color do not allow any edit per se.

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

        @wonkawilly

        Sorry, I didn’t follow any of that… at all. :-(

        wonkawillyW 1 Reply Last reply Reply Quote 0
        • wonkawillyW
          wonkawilly @Alan Kilborn
          last edited by

          @Alan-Kilborn said in Automatic selection of ALL instances of a searched string, in one go:

          @wonkawilly

          Sorry, I didn’t follow any of that… at all. :-(

          Please clarify, I am not getting what you mean, sorry

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

            @wonkawilly

            If I understand you correctly, you want something like this mock?

            79fc909f-2ff9-4d10-9404-38bbd110e81c-image.png

            A way to find and select each instance to work in multi-editor/instance mode, right?
            Yes, that would be beneficial, but I think it would also open a can of worms. I already see that some select 1_000_000 instances of a word.

            wonkawillyW Alan KilbornA 2 Replies Last reply Reply Quote 1
            • wonkawillyW
              wonkawilly @Ekopalypse
              last edited by wonkawilly

              @Ekopalypse
              Yes that would be beneficial indeed IMHO too.

              IMHO no need of the “only” word, just Select all will work fine

              Of course everything could have drawbacks when used improperly: even regular expressions if improperly used can open not just one but multiple cans or worms.

              Also into the dialog the button Count can help to prevent problems. And I suppose that the tools present into the tab Mark will help too for the same reason to check before act.

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

                @Ekopalypse said in Automatic selection of ALL instances of a searched string, in one go:

                If I understand you correctly, you want something like this mock?

                For the record, I understand exactly what @wonkawilly wants.
                But I wanted him to explain why he thinks having such a capability is so advantageous. I wanted him to do this on his own, so I wasn’t leading him, but he couldn’t figure out what I was asking for, so…

                What are you going to do when you have all these matches as selected text, that you can’t do already in the N++ search/replace/mark interface, without having text selected as a result of a search?

                Your choices are:

                • start typing (this will replace the selected text with what you type; hmmm, there’s already a Replace function that will do this)

                • copy (there’s already a Copy Marked Text function – ok, you have to mark text first)

                • delete (just a Replace with nothing, use that function)

                • cut (copy plus delete, a bit laborious with existing functionality, but is this a really common need?)

                • scroll around post-search to “eyeball” all the matches (just use Mark All; also, if text was selected and you do this, be careful that you don’t do a keyboard/mouse action that causes all of your selected text to become immediately unselected)

                In short, no big advantage that I see to having text selected as a result of a search. Sure, it is fine to want it, but perhaps there is a reason it doesn’t already exist.

                EkopalypseE 1 Reply Last reply Reply Quote 3
                • EkopalypseE
                  Ekopalypse @Alan Kilborn
                  last edited by

                  @Alan-Kilborn

                  It serves a similar but different purpose, in the end you have several cursors to work with. At the moment, the Find and Replace dialog cannot do this and the options mentioned are, for this case, also no options, because they leave only one cursor after the actions.

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

                    @Ekopalypse said in Automatic selection of ALL instances of a searched string, in one go:

                    in the end you have several cursors to work with

                    And the big advantage to that is…?

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

                      Hi, @alan-kilborn and All,

                      Alan, you asked @wonkawilly to describe the benefits of this potential new feature but, to be honest, I said exactly the same thing in my first post when I added, at the end :

                      So, I wonder if we should propose an enhancement of the Mark dialog, with a new box option Selection of the Occurrences or equivalent, using this mechanism ?

                      So, you would also be entitled to level the same criticism at me and ask me to justify the need for multiple cursors !

                      Thus, I read your last post carefully and, of course, your arguments seem relevant. However, I have an intuitive feeling that this new functionality would be very powerful, especially when you decide to take consecutive actions on this selection of cursors !

                      For example, considering the text :

                      This is a simple test
                        This is a simple test
                          This is a simple test
                            This is a simple test
                              This is a simple test
                                This is a simple test
                                  This is a simple test
                                    This is a simple test
                                      This is a simple test
                                        This is a simple test
                      
                      • Let’s say we’ve selected the ten words simple, using the latest version of the Mutliple-Selections.py script.

                      • First, we decide to delete the word simple : We could use the Replace dialog with the search of simple, Nothing as the replacement and with the In selection box ticked, to obtain the following text :

                      This is a  test
                        This is a  test
                          This is a  test
                            This is a  test
                              This is a  test
                                This is a  test
                                  This is a  test
                                    This is a  test
                                      This is a  test
                                        This is a  test
                      
                      • However, suppose we change our mind and we want to add 10 line-breaks, AFTER deleting those 10 simple words. Now, using the Replace dialogue is not so easy, because we have to search for the string a test and change it to the string a \r\n test, just for those ten lines ! If we had used the multiple cursors’ technique, we would only have had to press the Enter key :-)

                      And then, after all: if Neil Hodgson, and his team of devs, have added the SCI_MULTIPLESELECTADDNEXT and SCI_MULTIPLESELECTADDEACH commands, in the Scintilla project, there must be some point in using them !

                      Best Regards,

                      guy038

                      BTW, once I selected the ten lines of my test and used the Multiple_Selections.py script to select the ten words simple and deleted them with the Del key, I tried a Ctrl + Z action in the hope of get the words simple back. But no success ! I suppose that my last version should include a sequence editor.beginUndoAction() and editor.endUndoAction() …

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

                        @guy038

                        No worries; it was merely my attempt to point out that it probably wouldn’t be as useful as people think on first consideration.

                        Now consider the Select all only checkbox in the @Ekopalypse UI mockup. How is one to know which actions this applies to? Does one need it for Find Next? (no). Count? (maybe?). Find All in Current Document? (certainly). Find All in All Opened…? (probably?).

                        The historical way of options that apply to only certain actions is to put them in a box that groups those actions; here we see In selection as the example of that. How would something like that best be done here, or is the Select All Only checkbox left floating out on its own?

                        Would this apply to Find in Files as well, for documents that are already open?

                        Is there any effect for Mark? Maybe the new checkbox only appears on the marking tab, as this has some logical commonality.

                        It’s good to want things, but it is also good to consider why you want them, and then all the downstream concerns.

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

                          @guy038 said in Automatic selection of ALL instances of a searched string, in one go:

                          BTW, once I selected the ten lines of my test and used the Multiple_Selections.py script to select the ten words simple and deleted them with the Del key, I tried a Ctrl + Z action in the hope of get the words simple back. But no success ! I suppose that my last version should include a sequence editor.beginUndoAction() and editor.endUndoAction()

                          I didn’t try your script, but setting up a similar test, where Delete is done with multiple selections active and then doing Undo DID achieve the undo for me. I see no reason why it should be different with a script pre-selecting data that you manually press the Delete key on post-script.

                          In your script, editor.beginUndoAction() and editor.endUndoAction() aren’t going to do anything, because your script doesn’t change the text…so there’s nothing to undo.

                          Lycan ThropeL 1 Reply Last reply Reply Quote 3
                          • guy038G
                            guy038
                            last edited by

                            Hello, @alan-kilborn,

                            Of course, you’re right about it ! Don’t know which manipulation I did before ! I confirm that after deleting the 10 words simple of my test, with the script, adding a line-break then some dummy text, the Ctrl + Z actions did delete the added text as well as the line-breaks and get the words simple back ! The status of the file was also back to unchanged !

                            BR

                            guy038

                            1 Reply Last reply Reply Quote 3
                            • mkupperM
                              mkupper
                              last edited by mkupper

                              I have always thought of selections as being temporary and fragile things and don’t think I would be comfortable with constructing selections using a search expression. I like to be able to look things over before I perform some action… One click of a mouse or keyboard can wipe out a carefully constructed selection.

                              An alternative is to use marking and to add a Select Marked Text button. This button likely could replace the Copy Marked Text button on the Mark dialog box.

                              Once the marked text is selected then people could then copy it, replace it, delete it, convert to upper/lower case, etc.

                              At present, unmarking is moderately awkward but is possible. Ideally, the Mark dialog box was a little more consistent. The Mark All button marks using a combination of the search pattern plus the In selection checkbox. The Clear all marks button though ignores the search pattern field but does honor In selection.

                              That leads me to thinking about adding a Mark selected text button so that I can drag-select some text, mark it, drag-select something else and mark it without needing to use the search pattern field.

                              A potential downside to the idea is that because selecting and marking do not make changes to the text that the activity related to setting up the marked area(s) or selection(s) never show up in the undo/redo history. Someone could loose chunk of time.

                              Alan KilbornA 1 Reply Last reply Reply Quote 4
                              • Lycan ThropeL
                                Lycan Thrope @Alan Kilborn
                                last edited by

                                @Alan-Kilborn ,

                                I’m glad you did this, because I was thinking the same thing. I don’t know why his Undo didn’t work, perhaps because it was a script that did other things in addition, but whenever I do a change or search/replace, I feel it’s on me, to make sure I really want that change, as @mkupper suggests.

                                For that, the usefulness of the Mark comes into play and allows one to see what they’ll be changing ahead of time using the supplied regex. Using these things in bulk text changes is always a risk so again, these changes need to be tempered with good reason, as @Alan-Kilborn suggests, rather than willly-nilly giving users more complex operations than they truly need which just adds to more confusion, support problems and trying to explain to people how to properly use it which will benefit, what? Maybe 1% of the total users?

                                That’s what the scripting option is supposed to be for, so that the 1% (which most likely will also be superusers with the skills, anyways) can craft custom capabilities that the other 99% don’t need to be able to use or heaven forbid, stumble into and send them straight here looking for people to unscrew their lack of understanding of what “Don’t push the red button” means. :-)

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

                                  @mkupper said:

                                  An alternative is to use marking and to add a Select Marked Text button. This button likely could replace the Copy Marked Text button on the Mark dialog box.

                                  Or, it could be an additional action button, not replacing an existing one that users could be used to using.


                                  At present, unmarking is moderately awkward but is possible. Ideally, the Mark dialog box was a little more consistent. The Mark All button marks using a combination of the search pattern…

                                  So what I think you’re saying here is that it would be nice to be able to unmark text based on the Find what expression as well. That makes a good deal of sense. Maybe an additional action button could be added for this case.

                                  mkupperM 1 Reply Last reply Reply Quote 4
                                  • mkupperM
                                    mkupper @Alan Kilborn
                                    last edited by

                                    @Alan-Kilborn said in Automatic selection of ALL instances of a searched string, in one go:

                                    Or, it could be an additional action button, not replacing an existing one that users could be used to using.

                                    I had suggested replacing the button to declutter the user interface.

                                    The action would still be available but would take an extra step which is select-all-marked and then copy.

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

                                      @Alan-Kilborn said in Automatic selection of ALL instances of a searched string, in one go:

                                      And the big advantage to that is…?

                                      To be able to work with multiple cursors.

                                      On the rest of the arguments against such a feature.
                                      I don’t think it takes a genius to figure out what “select whatever” would mean in such a case,
                                      and if it’s documented, those unfamiliar with or accustomed to the concept of “multiple cursors/carets/editors/whatever” will understand its purpose.
                                      Does this mean that all potential users are using it correctly,
                                      no, but are all users also using regexes correctly? Backup? …
                                      Someone who wants to test beforehand whether the results of his search will give only the desired result can run mark beforehand …
                                      Whether such an extension is implemented in the find tab as a checkbox,
                                      or gets its own tab or dialog, is in my opinion irrelevant for this discussion,
                                      because it is ONLY about whether Don finds it good or not.
                                      No matter what comes out of this discussion in the end,
                                      if Don has a different opinion on it, and that should be respected because it’s his project,
                                      that and only that will be the deciding factor.
                                      So, I’ve given enough mustard on this subject now, wouldn’t know what more input I could provide.

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

                                        @Ekopalypse

                                        And the big advantage to that is…?

                                        To be able to work with multiple cursors.

                                        Sure, one can just keep saying that over and over again, but I for one haven’t been convinced about any great usefulness gain coming from it.

                                        Of course, my opinion doesn’t matter, but if I feel that way, probably others do, too. No idea how the author of Notepad++ feels about it, but unless someone creates a PR for it, that doesn’t matter, either.

                                        1 Reply Last reply Reply Quote 2
                                        • Mark OlsonM Mark Olson referenced this topic on
                                        • Alan KilbornA
                                          Alan Kilborn
                                          last edited by Alan Kilborn

                                          Here’s my entry for a script for this topic.

                                          NOTE: This is probably NOT of any use for dedicated keyboardists as there is really no way to make use of it without touching the mouse. (Of course, the same can be said of most (but not all) “mulit-edit” operations.

                                          What it does is a “scoped multiselect”. For myself, I find this very useful to quickly rename a poorly-named script variable in a localized section of code. Often when I’m coding I need a variable to do something, so maybe I’ll just call it zz until I get its code correct, and only then will I give it a good permanent name, one that suits its (perhaps evolved) function.

                                          Anyway, to use the script, you define two empty carets (to say “only act in the range between them”) and then a non-empty selection between the empties (to define the source text to be replaced), and then you run the script. The empty carets defining scope give me confidence that I won’t be changing text outside of my defined area.

                                          An example helps; take the original example in this thread, and set up the following two empty carets and selection, which is very quick and easy to do with the mouse (just click on line 6, ctrl+click on line 17, and ctrl+doubleclick on simple in line 13):

                                          f8d9f854-2964-4796-bf3b-e24da9e393bb-image.png

                                          Run the script to obtain:

                                          34852a6f-582c-42fb-a651-25b73e3e99d0-image.png

                                          I bind the running of this script to a keycombo, and find this is quicker than Notepad++'s replace function when I need to rename something: No prompts and no typing to get the text I need selected; all I have to do after running the script is type the desired replacement text.

                                          I named this script the somewhat lengthy MultiselectSelectedTextBetweenlEmptyCaretStoppers.py and here is its listing:

                                          # -*- coding: utf-8 -*-
                                          from __future__ import print_function
                                          
                                          #########################################
                                          #
                                          #  MultiselectSelectedTextBetweenlEmptyCaretStoppers (MSTBECS)
                                          #
                                          #########################################
                                          
                                          # references:
                                          #  https://community.notepad-plus-plus.org/topic/24549  <-- this script posted here
                                          #  inspiration:
                                          #   https://github.com/notepad-plus-plus/notepad-plus-plus/issues/8203#issuecomment-623136793
                                          #  for newbie info on PythonScripts, see https://community.notepad-plus-plus.org/topic/23039/faq-desk-how-to-install-and-run-a-script-in-pythonscript
                                          
                                          #-------------------------------------------------------------------------------
                                          
                                          from Npp import *
                                          import os
                                          import inspect
                                          
                                          #-------------------------------------------------------------------------------
                                          
                                          class MSTBECS(object):
                                          
                                              def __init__(self):
                                          
                                                  self.this_script_name = inspect.getframeinfo(inspect.currentframe()).filename.split(os.sep)[-1].rsplit('.', 1)[0]
                                          
                                                  # user must make the 3 required selections in any order:
                                                  sel_tup_list = []
                                                  sel_tup_list.append( (editor.getSelectionNStart(0), editor.getSelectionNEnd(0)) )
                                                  if editor.getSelections() >= 2: sel_tup_list.append( (editor.getSelectionNStart(1), editor.getSelectionNEnd(1)) )
                                                  if editor.getSelections() >= 3: sel_tup_list.append( (editor.getSelectionNStart(2), editor.getSelectionNEnd(2)) )
                                                  sel_tup_list.sort()
                                          
                                                  # conditions-not-correct checking:
                                                  cnc = False
                                                  if not cnc and not editor.getMultipleSelection(): cnc = True  # must have "Multi-editing" enabled in Preferences
                                                  rect_sel_mode = editor.getSelectionMode() in [ SELECTIONMODE.RECTANGLE, SELECTIONMODE.THIN ]
                                                  if not cnc and rect_sel_mode: cnc = True  # can't be in column-block selection mode
                                                  if not cnc and len(sel_tup_list) != 3: cnc = True  # need 3 selections (actually 2 carets and one selection)
                                                  if not cnc and sel_tup_list[0][0] != sel_tup_list[0][1]: cnc = True  # no leading empty caret
                                                  if not cnc and sel_tup_list[1][0] == sel_tup_list[1][1]: cnc = True  # no text selected between carets
                                                  if not cnc and sel_tup_list[2][0] != sel_tup_list[2][1]: cnc = True  # no trailing empty caret
                                                  if cnc:
                                                      cnc_help = '''
                                                      Conditions are not correct for running the script.
                                                      You must have:
                                                      - "Multi-Editing" enabled in Preferences
                                                      - Two empty carets with a stream selection in between
                                                      '''
                                                      self.mb(cnc_help)
                                                      return
                                          
                                                  (start_search_pos, end_search_pos) = (sel_tup_list[0][0], sel_tup_list[2][0])
                                                  search_word = editor.getTextRange(*sel_tup_list[1])
                                                  match_span_tup_list = []
                                                  editor.search(search_word, lambda m: match_span_tup_list.append(m.span(0)), 0, start_search_pos, end_search_pos)
                                          
                                                  if len(match_span_tup_list) > 1:
                                                      editor.setSelection(*match_span_tup_list[0][::-1])  # args to setSelection are caret then anchor
                                                      for (start_match_pos, end_match_pos) in match_span_tup_list[1:]:
                                                          editor.addSelection(end_match_pos, start_match_pos)
                                                      self.sb_output('{} selections made'.format(len(match_span_tup_list)))
                                          
                                              def mb(self, msg, flags=0, title=''):  # a message-box function
                                                  return notepad.messageBox(msg, title if title else self.this_script_name, flags)
                                                  
                                              def sb_output(self, *args):  # output to N++'s status bar (will be overwritten by N++ e.g. when active tab is changed)
                                                  notepad.setStatusBar(STATUSBARSECTION.DOCTYPE, ' '.join(map(str, args)))
                                          
                                          #-------------------------------------------------------------------------------
                                          
                                          if __name__ == '__main__': MSTBECS()
                                          
                                          Mark OlsonM 1 Reply Last reply Reply Quote 3
                                          • Alan KilbornA Alan Kilborn referenced this topic on
                                          • Mark OlsonM
                                            Mark Olson @Alan Kilborn
                                            last edited by

                                            @Alan-Kilborn
                                            Getting this error:

                                            Traceback (most recent call last):
                                              File "%AppData%\Roaming\Notepad++\plugins\Config\PythonScript\scripts\multiSelectBetweenEmptyCaretSelections.py", line 65, in <module>
                                                if __name__ == '__main__': MSTBECS()
                                              File "%AppData%\Roaming\Notepad++\plugins\Config\PythonScript\scripts\multiSelectBetweenEmptyCaretSelections.py", line 61, in __init__
                                                self.sb_output('{} selections made'.format(len(match_span_tup_list)))
                                            AttributeError: 'MSTBECS' object has no attribute 'sb_output'
                                            

                                            Eyeballing the code, it looks like calling self.mb will probably cause the same problem. I guess you forgot to define those methods.

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