Marked text manipulation
-
Hello, @alan-kilborn and All,
So, as the result of our discussion :
This old post https://community.notepad-plus-plus.org/topic/12710/marked-text-manipulation/8
have been updated https://community.notepad-plus-plus.org/topic/19189/need-a-copy-marked-lines-feature/7
Cheers,
guy038
-
Semi-related info:
Part of this current thread is about how to copy text that one has marked via Marking that requires use of a PythonScript.
In a POST in another thread, I provide a script that will select text given conditions specified by user input. That selected text can then be copied.
Similar results thru some different methods; enough to warrant a cross-link, I guess.
-
Can anyone explain quickly how to use the python script? As in which steps I need to do in Notepad++ to run the script.
-
- Install PythonScript using Plugins > Plugins Admin
- Plugins > Python Script > New Script
- Paste the contents and save under
someName
(whatever name you want) - Plugins > Python Script > Scripts >
someName
- To add a keyboard shortcut for the script:
- Plugins > Python Script > Configuration,
- select the
someName
script and Add to the menu - Restart Notepad++
someName
will now be in the main Plugins > Python Script menu, not just in the Scripts submenu- Settings > Shortcut Mapper > Plugin Commands will see the script, and you can assign a keyboard shortcut
-
Peter maybe only made it 99.9999% clear. Perhaps the newbie 0.0001% might still be left wondering…
Peter’s step 4 would be how you actually run the script.
Be advised that in this particular case, you need to have some red-marked text for the script to operate on (hopefully this part is clear).
I think there is an open issue that would make this script’s functionality a native part of Notepad++. I will try to find it…
-
This may be the issue to which I was referring: https://github.com/notepad-plus-plus/notepad-plus-plus/issues/6095
-
At the time of the earlier discussions, there was no way to copy marked text natively in Notepad++, meaning without resorting to scripting. Now (7.9.1-ish) there is:
Just press the indicated button after you already have marked some text.
Note: I have added similar information in this somewhat-related other THREAD.
-
My preferred solution would have been to select all marked text because this provides more flexibility. This way the user could
- copy
- cut
- delete
- change
the marked search results.
To achieve that I wrote the following Lua script (intended to be used with, well, the LuaScript plugin).
-- ============================================================================= -- Add menu entry to select all items marked by find marks -- ============================================================================= npp.AddShortcut("Select Marked", "", function() local indicatorIdx local pos, markStart, markEnd local hasMarkedItems -- Search find marks have indicator number 31 SCE_UNIVERSAL_FOUND_STYLE = 31 -- Remove selection but do not change cursor position editor.Anchor = editor.CurrentPos hasMarkedItems = false -- Search find mark indicators and select marked text pos = 0 while pos < editor.TextLength do if editor:IndicatorValueAt(SCE_UNIVERSAL_FOUND_STYLE, pos) == 1 then markStart = editor:IndicatorStart(SCE_UNIVERSAL_FOUND_STYLE, pos) markEnd = editor:IndicatorEnd(SCE_UNIVERSAL_FOUND_STYLE, pos) if editor.SelectionEmpty or not editor.MultipleSelection then editor:SetSelection(markEnd, markStart) else editor:AddSelection(markEnd, markStart) end pos = markEnd hasMarkedItems = true end pos = pos + 1 end -- Scroll last selected element into view. This is only because -- Npp/Scintilla will do that anyway when moving the cursor next time if hasMarkedItems then editor:ScrollCaret() end end)
After adding this code to the startup.lua file and restarting Notepad++, the plugin’s submenu provides a new entry Select Marked which can be assigned to a keyboard shortcut.
-
@dinkumoil said in Marked text manipulation:
select all marked text because this provides more flexibility. This way the user could
copy
cut
delete
changeThat’s a good idea as well, especially for delete and change (although those can be achieved, on a one-off basis, by replace-with-nothing and replace-with something ops).
Copy and cut of selected text would jam all of the text together if it were on a selection basis (probably not really useful that way).
I noticed this new Copy Marked Text command inserts a line-ending between the individual globs of marked text, which seems to make it useful, although to tell the truth I haven’t had a real application for it myself, yet.
-
@Alan-Kilborn said in Marked text manipulation:
Copy and cut of selected text would jam all of the text together if it were on a selection basis (probably not really useful that way).
Yes, you are right. Up to now, I’ve used my script only for deleting and editing the selections. But especially being able to multi-edit all occurences of a search term or overwrite them by multi-paste vastly increases productivity.