posting code (by copy & paste) with line numbers
-
I want to publish code snippets in a forum and refer to them in the explanatory text. It would be nice if I could copy the code snippets with the respective line numbers - is that possible?
If so, how? -
-
@goolyThank you, but this does not work with copy (Ctrl+C) & paste (Ctrl+V) :(
Any solution for this? -
@gooly said in posting code (by copy & paste) with line numbers:
@goolyThank you, but this does not work with copy (Ctrl+C) & paste (Ctrl+V) :(
The line numbers aren’t part of the text, so Notepad++ doesn’t have them available for copy/paste with the text normally.
In theory, one could ask for that as a feature (but I doubt it’s likely to be implemented); as a second alternative, one could write a script in a scripting plugin (like PythonScript), which would do the copy then insert the line numbers, or someone in theory could write a full plugin to accomplish this (maybe some other plugin already has copy-with-line-number, but I don’t know of it); but if you are willing to do a multi-step process, it can be done in native Notepad++:
- One Time Setup: for a normal installation of Notepad++,
- File > Exit all copies of Notepad++, then run one copy of Notepad++.
- File > Open:
%AppData%\Notepad++\shortcuts.xml
- Just before
</Macros>
, add:
and save<Macro name="NumberLines" 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="^" /> <Action type="3" message="1625" wParam="0" lParam="2" sParam="" /> <Action type="3" message="1602" wParam="0" lParam="0" sParam=" | " /> <Action type="3" message="1702" wParam="0" lParam="768" sParam="" /> <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" /> <Action type="0" message="2316" wParam="0" lParam="0" sParam="" /> <Action type="2" message="0" wParam="42089" lParam="0" sParam="" /> <Action type="0" message="2318" wParam="0" lParam="0" sParam="" /> <Action type="0" message="2312" wParam="0" lParam="0" sParam="" /> <Action type="2" message="0" wParam="42089" lParam="0" sParam="" /> <Action type="2" message="0" wParam="42034" lParam="0" sParam="" /> </Macro> <Macro name="UnNumber Lines" 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="^\h*\d+ \| " /> <Action type="3" message="1625" wParam="0" lParam="2" sParam="" /> <Action type="3" message="1602" wParam="0" lParam="0" sParam="" /> <Action type="3" message="1702" wParam="0" lParam="768" sParam="" /> <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" /> </Macro>
- File > Exit, then run one copy of Notepad++.
From now on, you have two new macros in the Macro menu:
Number Lines
andUnNumber Lines
(and, if you want, you can use Macro > Modify Shortcut/Delete Macro to launch the Shortcut Mapper to add a keyboard shortcut for running these macros)Every time you want to copy some or all of your code with the line number:
- Run Macro >
Number Lines
, which will add a|
at the beginning of every line, then launch the Column Editor - Fill out the Column Editor as follows:
- ⦿ Number to Insert,
- Initial Number:
1
- Increase by:
1
- **Repeat: ** (leave blank)
- Leaing:
Spaces
- Format = ⦿ Dec
- Click OK
(it will remember these values for next time)
- Do your copy of the code (some or all lines, as you desire)
- When done copying code-with-line-numbers, run Macro >
UnNumber Lines
to remove the line prefixes
- One Time Setup: for a normal installation of Notepad++,