Automatic selection of ALL instances of a searched string, in one go
-
If I understand you correctly, you want something like this mock?
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. -
@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.
-
@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.
-
-
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.
-
@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…?
-
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 optionSelection 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 theMutliple-Selections.py
script. -
First, we decide to delete the word
simple
: We could use the Replace dialog with the search ofsimple
,Nothing
as the replacement and with theIn 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 those10
simple words. Now, using the Replace dialogue is not so easy, because we have to search for the stringa test
and change it to the stringa \r\n test
, just for those ten lines ! If we had used the multiple cursors’ technique, we would only have had to press theEnter
key :-)
And then, after all: if
Neil Hodgson
, and his team of devs, have added theSCI_MULTIPLESELECTADDNEXT
andSCI_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 wordssimple
and deleted them with theDel
key, I tried aCtrl + Z
action in the hope of get the wordssimple
back. But no success ! I suppose that my last version should include a sequenceeditor.beginUndoAction()
andeditor.endUndoAction()
… -
-
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.
-
@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()
andeditor.endUndoAction()
aren’t going to do anything, because your script doesn’t change the text…so there’s nothing to undo. -
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, theCtrl + Z
actions did delete the added text as well as the line-breaks and get the wordssimple
back ! The status of the file was also back tounchanged
!BR
guy038
-
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 theCopy Marked Text
button on theMark
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. TheMark All
button marks using a combination of the search pattern plus theIn selection
checkbox. TheClear all marks
button though ignores the search pattern field but does honorIn 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.
-
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. :-)
-
@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.
-
@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.
-
@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 runmark
beforehand …
Whether such an extension is implemented in thefind
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. -
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.
-
-
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):Run the script to obtain:
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()
-
-
@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. -
I didn’t exactly forget, I just streamlined out some of my usual debug code, before posting.
It appears I got too heavy with my edits. :-(I still have time to edit the script; I’ll fix it. :-)
Thanks for pointing it out.EDIT: I updated the above script listing to fix the problems @Mark-Olson pointed out.
-
Hello, @alan-kilborn and All,
Ah… yes, your proposed script is quite clever and very intuitive ;-)) I didn’t think about invisible caret boundaries for the defined scope
Remark that, at first glance, I thought that the carets did represent the true
|
character ( with code\x7C
). But, thanks to the error message, I quickly understood the problem !I personally think that the logical method is :
-
Do the stream selection of the text first, without holding down the
Ctrl
key -
Then, do the two caret
zero-length
selections, with theCtrl
key pressed
Notes :
- If you prefer an insensitive search, simply add the
import re
command, below theimport inspect
one and change the line :
editor.search(search_word, lambda m: match_span_tup_list.append(m.span(0)), 0, start_search_pos, end_search_pos) # SENSITIVE search
As :
editor.search(search_word, lambda m: match_span_tup_list.append(m.span(0)), re.I, start_search_pos, end_search_pos) # INSENSITIVE search
-
Now, I also found out a trick to simulate a whole-word search :
-
Do a stream selection of the word, including its surrounding
space
chars -
Do the two caret selections
-
Run the script
-
Click on the
Right arrow
key -
Click on the
Left arrow
key -
Click on the
Ctrl
+Left arrow
shortcut
-
Playing around with your script, after a while, I asked myself :
Could it be possible to just do the text selection and that the scope of the search would be, automatically, the complete file contents ?
After some tries, I ended up with this modified part of the script which, indeed, does the job !
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)) ) if editor.getSelections() == 1: # If an unique stream selection: sel_tup_list.append ( (0, 0) ) # Leading caret is supposed to be at the very begining of current filec sel_tup_list.append ( (editor.getLength(), editor.getLength()) ) # Trailing caret is supposed to be at the very end of current file sel_tup_list.sort()
instead of :
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()
Now, although this modification seems quite functional, I would like to know, Alan, if this is the best formulation !
Best Regards,
guy038
A last observation :
If you do the three needed selections, in any order and that you decide to add additional selections ( either non-null text or null caret selections ), no error message is displayed : these additional selections are simply ignored. To my mind, it’s the best way to proceed !
-
-
@guy038 said in Automatic selection of ALL instances of a searched string, in one go:
I personally think that the logical method is :
Do the stream selection of the text first, without holding down the Ctrl key
Then, do the two caret zero-length selections, with the Ctrl key pressed
The “beauty” of the design is that you can do these 3 things in ANY order that you like.
@guy038 said in Automatic selection of ALL instances of a searched string, in one go:
If you prefer an insensitive search
Yes, but for the utmost in safety, I prefer a sensitive, i.e, match case, design. I don’t want to have to consider it, I just want to know I am getting exactly and only what I’ve asked for.
@guy038 said in Automatic selection of ALL instances of a searched string, in one go:
…and that the scope of the search would be, automatically, the complete file contents ?
Again, the empty carets for scoping are there for safety in this “quick” replacement method. If you’re looking for something with wider scope, just use Notepad++ 's own Replace All functionality.
@guy038 said in Automatic selection of ALL instances of a searched string, in one go:
if this is the best formulation
It seems reasonable to me! :-)
There’s rarely one and only one best way with a block of code…