Insert sequential numbers at start of lines
-
@Alan-Kilborn I know itās years later, but i found myself using your script. Iām a total noob when it comes to programming, all i could do is modify what goes after the numbers.
Now, how could i delete what the script just did (i.e delete the numbers, but leave the lines like they were before the script ran) ? I know i can use Undo to do it on the spot, but letās say i wanna undo the numbering a day after. -
You donāt need programming to delete the line numbers that were inserted.
Hereās how to do it:
- Move the caret to line 1 and column 1
- Press Shift+Alt+b to Begin Select in Column mode
- Move the caret to the last line and put it right after the last digit of the line number (previously added by the script)
- Press Shift+Alt+b again to End Select in Column mode
At this point you should have the line numbers selected in a rectangular block so simply pressing Delete should eliminate them.
-
@Alan-Kilborn Okay, i knew how to do that, but it does not help me.
First, iād have to do it twice, because i set the script up to put a number, then :, then a space (5: ), so till 9 its 3 characters long and after is 4 characters long.
And again, i have to do this multiple times in a document, every day and using the column mode is a bit tedious. I was hoping that i could do it with a script.
Now, i select the text i need, press a key and your script instantly does its job. Iād like to have the same thing with deleting, but i donāt know if itās possible or not. :) -
@Ionut-Dorin said in Insert sequential numbers at start of lines:
i set the script up to put a number, then :, then a space (5: ), so till 9 its 3 characters long and after is 4 characters long
It would have been better to just show some of this text rather than describing itā¦
But I think if you do a replacement operation it will be good?:
Find:
^\d+:
Replace: set to nothing
Search mode: Regular expression
Options: Wrap around
Action: Replace allYou can record this replacement op as a macro.
-
@Alan-Kilborn
1: Line content
2: Line content
3: Line content
4: Line content
5: Line content
6: Line content
7: Line content
8: Line content
9: Line content
10: Line content
11: Line content
12: Line content
ā¦ and to become
Line content
Line content
Line content
Line content
etc.
Find: ^\d+: doesnāt work and plus, i donāt need it for the whole document, just for the lines i select. -
@Ionut-Dorin said in Insert sequential numbers at start of lines:
Find: ^\d+: doesnāt work
If you did exactly what I said to do, I see no reason why it wouldnāt work on your sample data.
i donāt need it for the whole document, just for the lines i select.
Ok, so donāt checkmark Wrap around but make your selection first and then checkmark In selection.
-
@Alan-Kilborn Yes, that did it, I think i read too fast and didnāt see that you said āSearch mode: Regular expressionā and i didnāt check that, so maybe thatās why it didnt work.
Also, i turned it into a macro for ease of use.
Thank you so much for your help and patience, youāre a lifesaver. -
@Alan-Kilborn said in Insert sequential numbers at start of lines:
Sure, most anything is possible with programming.
Hi Alan, the scripts have been working really well, thank you so much for your help. One thing Iāve noticed is that, when using the script for selected lines, it has to be a block selection for the script to work. I canāt select individual lines (eg: line 1, line 4, line 7) and insert numbers at that the start of only those lines.
Iāve tried:
- Multi-selection using Ctrl + single-click at the start of each line.
- Multi-selection using Ctrl + single-click at the start and end of each line.
- Multi-selection using Ctrl + double-click to select the first word of each line.
Numbers 1 & 2 donāt actually select any text so when I run the script I get a āNo text selectedā message.
Number 3 only places a number at the start of the line for the last word I select.Is it possible or am I expecting more than the script can achieve?
-
You must be referring to the
TextFxInsertLineNumbers.py
script, because the other script discussed in this topic,InsertLineNumbersOnLines.py
, doesnāt deal with any selected text at all.Hereās a modified version of the script Iām calling
TextFxInsertLineNumbers2.py
:# -*- coding: utf-8 -*- from __future__ import print_function # references: # https://community.notepad-plus-plus.org/topic/23206/insert-sequential-numbers-at-start-of-lines # https://community.notepad-plus-plus.org/topic/23166/faq-desk-how-do-i-replicate-the-features-of-textfx from Npp import * #------------------------------------------------------------------------------- class TFILN2(object): def __init__(self): handled_line_nbr_list = [] running_line_nbr = 1 line_tup_list = [] for s in range(editor.getSelections()): start_line = editor.lineFromPosition(editor.getSelectionNStart(s)) end_line = editor.lineFromPosition(editor.getSelectionNEnd(s)) line_tup_list.append((start_line, end_line)) line_tup_list.sort() for (start_line, end_line) in line_tup_list: for line_nbr in range(start_line, end_line + 1): if line_nbr not in handled_line_nbr_list: line_content = editor.getLine(line_nbr).rstrip('\n\r') editor.replaceLine(line_nbr, '{n:08} {c}'.format(n=running_line_nbr, c=line_content)) running_line_nbr += 1 handled_line_nbr_list.append(line_nbr) editor.setSel(editor.getCurrentPos(), editor.getCurrentPos()) #------------------------------------------------------------------------------- if __name__ == '__main__': TFILN2()
This version handles all types of selections.
-
@Alan-Kilborn said in Insert sequential numbers at start of lines:
This version handles all types of selections.
Perfect!!! Thank you so much šš