Macro - copy and paste part of line
-
Hello. I am looking for help with creating a macro. I would need to copy part of a line in a document, I would like to copy always the part that starts with a tab space and ends at the end of the line. I always place the cursor in the part of the text.
Then I would need to reverse this process, I would like to replace the text on the line from the tab space to the end.Is something like this possible?
-
@Martin-Steiner said in Macro - copy and paste part of line:
Is something like this possible?
I will say it is possible, however you haven’t explained in full what you intend to do with the copy, nor if the replacement is variable.
The macro I created below will, from the current position of the cursor, select the next valid string (which is after the last tab on a line, until the end of the line) using the Find function, and then using Ctrl-C adds that string to the clipboard ready for the next action. It leaves the selected string highlighted so you could just type the replacement at that time.
This macro can be added to the shortcuts.xml file (which is normally sited at %appdata%\Notepad++) but can vary depending on how you run Notepad++. If you do edit the shortcuts file with Notepad++ be very careful that it is the only step you do in a session. You might be best to read the FAQ post “What is %AppData%” in the FAQ section before undertaking the edit and also the online manual reference here refers to editing any of configuration files associated with Notepad++.
Terry
</Macro>
<Macro name=“select©” Ctrl=“no” Alt=“no” Shift=“no” Key=“0”>
<Action type=“3” message=“1700” wParam=“0” lParam=“0” sParam=“” />
<Action type=“3” message=“1601” wParam=“0” lParam=“0” sParam=“(?<=\t)([^\t\r\n]+)$” />
<Action type=“3” message=“1625” wParam=“0” lParam=“2” sParam=“” />
<Action type=“3” message=“1702” wParam=“0” lParam=“512” sParam=“” />
<Action type=“3” message=“1701” wParam=“0” lParam=“1” sParam=“” />
<Action type=“0” message=“2178” wParam=“0” lParam=“0” sParam=“” />
<Action type=“2” message=“0” wParam=“42002” lParam=“0” sParam=“” />
</Macro> -
@Terry-R
Yes, you got me right, that’s exactly what I was looking for. Thank you very much for your help.
One more question, how would this macro look like if the end of the copied line was a tab space again? -
@Martin-Steiner
Well if there was a change in requirement then the macro would either need editing to include another tab before the line, or you would record the new macro to do that, leaving the current one to solve your initial query.Actually the end of line will have an end of line marker, not a tab. So I presume you really mean to copy a different portion of the line, part way along it.
You might need to explain some more.
Terry
-
@Terry-R
Yes, it would be another macro. The principle is the same, only the beginning and end of the copied part is tab space. -
@Martin-Steiner said in Macro - copy and paste part of line:
Yes, it would be another macro
This line is the one which selects the next valid string. So for a different selection all that’s needed is for the sParam string (in red) below to change to suit. This is called a regular expression (regex). Not sure how much you understand about regex but this is like a filter that will only allow strings/characters to slip thru (be selected) if they fit the filter.
<Action type=“3” message=“1601” wParam=“0” lParam=“0” sParam=“(?<=\t)([^\t\r\n]+)$
” />Now I could easily give you another regex to fit your new requirement however I think it might be doable for yourself to give it a go. From your image you want to start immediately after a
>
and go until the next tab is found. With that knowledge you should be able to edit the regex. Use the www.regex101.com website to read and decipher what the code means. Try it, then show us what you have if unable to achieve it and we can help. We do like people to try this themselves as we aren’t a data transformation service, but do expect some learning to take place by yourself as the posts continue. There are also several FAQ posts which have references to valuable regular expression sources if you need a bit more knowledge. I also use www.rexegg.com as a quick reference tool.Terry
PS if editing the shortcuts file by inserting a modified version of the above macro, be sure to give it a different name as can be seen on the second line of the macro, “macro name”.
-
@Terry-R
Thank you for the direction and help, I really appreciate it.
According to my findings, the regex entry could look like this:<Action type=“3” message=“1601” wParam=“0” lParam=“0” sParam=“(?<=\t)([^\t\r\n]+)(?=\t)” />
But I have a problem with the fact that it only selects part of the next line, not the one where I place the cursor.
This regex expression in this text: (?<=\t)([^\t\r\n]+)$
This regex expression in this text: (?<=\t)([^\t\r\n]+)(?=\t)
Is this a necessary feature or can it be modified in some other way?
I would need the beginning of the selection to look before the cursor and the end after it. -
@Martin-Steiner
You obviously don’t understand how a regular expression works. Any look behind has to be a fixed number if characters, so placing the cursor in the middle of what you want to select will never work.As I said in my first reply The macro I created below will, from the current position of the cursor, select the next valid string. You readily accepted the first solution suggesting you had tested it and it worked for you. So why with the second request have you changed your process and put the cursor in what seems to be a different location on the line?
But if you absolutely need to place the cursor there then the simple addition of another key sequence to the macro might fix the problem. I’m not on a PC right now but there is a shortcut key combo, I think it’s alt+home which moves the cursor to the start of the line the cursor is currently on.
So as a test, place the cursor somewhere on a line and use that key combo and see if it moves it back. There is the shortcut mapper in the menus that will show current key combos that achieve different things. It might be something you will find useful.
So once the correct shortcut is found you will need to re-record the macro with the move to start of line key combo first, then the find function with the regex, then a ctrl-c to copy the text. I will leave that as an exercise for you. The online manual has information of the macro system if you’re unsure. It is linked from the
?
menu.Terry
-
@Terry-R
I’m sorry, I probably didn’t express myself very accurately the first time, it could also be the language barrier. However, your help brought me to my goal and now I have two macros that fulfill exactly what I needed and it’s to your credit. Thank you again for your time. :-)<Macro name="CopyMIX" Ctrl="no" Alt="no" Shift="no" Key="0"> <Action type="0" message="2345" wParam="0" lParam="0" sParam="" /> <Action type="3" message="1700" wParam="0" lParam="0" sParam="" /> <Action type="3" message="1601" wParam="0" lParam="0" sParam="(?<=\t)([^\t\r\n]+)$" /> <Action type="3" message="1625" wParam="0" lParam="2" sParam="" /> <Action type="3" message="1702" wParam="0" lParam="512" sParam="" /> <Action type="3" message="1701" wParam="0" lParam="1" sParam="" /> <Action type="0" message="2178" wParam="0" lParam="0" sParam="" /> <Action type="2" message="0" wParam="42002" lParam="0" sParam="" /> </Macro> <Macro name="CopyUnity" Ctrl="no" Alt="no" Shift="no" Key="0"> <Action type="0" message="2345" wParam="0" lParam="0" sParam="" /> <Action type="3" message="1700" wParam="0" lParam="0" sParam="" /> <Action type="3" message="1601" wParam="0" lParam="0" sParam="(?<=\t)([^\t\r\n]+)(?=\t)" /> <Action type="3" message="1625" wParam="0" lParam="2" sParam="" /> <Action type="3" message="1702" wParam="0" lParam="512" sParam="" /> <Action type="3" message="1701" wParam="0" lParam="1" sParam="" /> <Action type="0" message="2178" wParam="0" lParam="0" sParam="" /> <Action type="2" message="0" wParam="42002" lParam="0" sParam="" /> </Macro>