automatic line numbering help.



  • Hi I love notepad++ and use it for all my code editing. I’m hoping it can help with an issue I have with automation scripts for a program called “do it again”. This lightweight tool makes simple recordings of your actions and allows you to re-run them and loop them.

    It saves them as text with an extension .dia. which can be opened and edited in any text editor. I usually use the find and replace function to reduce the delay time between each action which is great but sometimes I need to edit add or remove lines.

    here is an example of the code…

    MouseLDown 942 804 [3]
    MouseLUp 942 804 [4]
    Pause 40 [5]
    MouseLDown 909 1003 [6]
    MouseLUp 909 1003 [7]
    Pause 40 [8]
    MouseLDown 518 302 [9]
    Pause 40 [10]
    MouseLUp 518 302 [11]
    Pause 40 [12]

    I up till now for removal I just put a pause command in but if I want to add something I need to change the [number] at the end of the line for every following line so that it matches the line number.for example in notepad++ it looks like this…

    3 MouseLDown 942 804 [3]
    4 MouseLUp 942 804 [4]
    5 Pause 40 [5]
    6 MouseLDown 909 1003 [6]
    7 MouseLUp 909 1003 [7]
    8 Pause 40 [8]
    9 MouseLDown 518 302 [9]

    if I add a line

    3 MouseLDown 942 804 [3]
    4 MouseLUp 942 804 [4]
    5 KeyDown 124 [ ]
    6 Pause 40 [5]
    7 MouseLDown 909 1003 [6]
    8 MouseLUp 909 1003 [7]
    9 Pause 40 [8]
    10 MouseLDown 518 302 [9]

    I would have to change it to

    3 MouseLDown 942 804 [3]
    4 MouseLUp 942 804 [4]
    5 KeyDown 124 [ 5]
    6 Pause 40 [6]
    7 MouseLDown 909 1003 [7]
    8 MouseLUp 909 1003 [8]
    9 Pause 40 [9]
    10 MouseLDown 518 302 [10]

    to match the line number at the front. Simple to do with 10 lines of code but when you change line 3 of a 1000+ line script it is tiresome work.

    is there some way of using find and replace or a macro so say

    var x = 0

    if line of text ends [?] {
    if [?] != line number {
    [?] == [line number]}
    else {
    x++}
    }
    else{
    x++}

    excuse my poor syntax.



  • now working smarter (though still a pain).

    change extention to .csv
    now using NP++ to replace " " with “,”

    opening in excell
    finding all rows with “pause” and deleting, this means all rows left contain 4 columns.
    Column 4, which needs the row number, I am using the string ="[" &ROW()&"]" this fills the box as i need is [row number].
    save as csv

    return to NP++
    replace"," with " "
    save and change the extention back to .dia.

    it is a long ass way of doing it but it is quicker than renumbering each line by hand.

    anyone know how i can do this easier and all withing NP++??



  • Hello, james-robert-billington and All,

    I supposed that each number beginning your code is, simply, the automatic line number feature and not a number, which does begin each line !

    Well, the most simple way would be to use a Lua or Python script to achieve such a task ! In the meanwhile, I just propose you a semi-automatic way, which, first, writes an automatic numbering list and, secondly, use a regex search/replacement !

    • First, place the cursor at column 1 of the first line of the block of text which needs renumbering

    So, for instance, assuming your text below, where a new line KeyDown 124 [ ] has been added

    MouseLDown 942 804 [3]
    MouseLUp 942 804 [4] 
    KeyDown 124 [ ]
    Pause 40 [5]
    MouseLDown 909 1003 [6]
    MouseLUp 909 1003 [7]
    Pause 40 [8]
    MouseLDown 518 302 [9]
    

    Now :

    • Place the cursor in line 1, at column 1, right before the word MouseLDown ( IMPORTANT )

    • Open the Column Editor… ( ALT + C )

    • Select the option Number to Insert

    • Type 3, as Initial number :

    • Type 1 as Increase by :

    • Type 1, as Repeat :

    • Set the Leading zeros option, although it’s not mandatory

    • Select the Dec decimal format

    • Click on the OK button

    At once, any line downwards, will be updated with an automatic numbering, in column 1, till the last line of your file. Don’t panic ! With the following S/R, these numbers will disappear all at once !

    03MouseLDown 942 804 [3]
    04MouseLUp 942 804 [4]
    05KeyDown 124 [ ]
    06Pause 40 [5]
    07MouseLDown 909 1003 [6]
    08MouseLUp 909 1003 [7]
    09Pause 40 [8]
    10MouseLDown 518 302 [9]
    11Some text
    12Other text
    13....
    

    Finally, open the Replace dialog ( Ctrl + H )

    SEARCH (?-s)^0*(\d+)(.+[)(?:\d+|\x20+)]|^\d+

    REPLACE ?2\2\1]

    • Select the Regular expression search mode

    • Set the Wrap around option

    • Click on the Replace All button

    => You should get the following text, with the correct renumbering of your code and the suppression of the leading numbers, located downwards :

    MouseLDown 942 804 [3]
    MouseLUp 942 804 [4]
    KeyDown 124 [5]
    Pause 40 [6]
    MouseLDown 909 1003 [7]
    MouseLUp 909 1003 [8]
    Pause 40 [9]
    MouseLDown 518 302 [10]
    Some text
    Other text
    ...
    

    Et voilà !

    Note : Of course, this method also supposed that any line of code, located after the block of text to be modified, does not begin, itself, with some digits ! If it’s not the case, just tell me about it !

    Best Regards,

    guy038

    P.S. :

    You must be a fulfilled father, with your two adorable daughters !



  • All lines baring line 1, which does not have the [n] on it, begin with a word. this method is perfect. I will have to make a good old text file with the instructions, well at least the search and the replace strings, but still brilliant. thank you so much. And the girls are blessings even when annoying me :).



  • Hi , I would recommend you to install the Pythonscript plugin. So you can do all kind of document processing.
    I had similar scripts for similar tasks - I’ve edited one so it will work for your case.
    So once you have the plugin installed, this script will do the replacement in the whole active document
    (assuming brackets are used only for numbers and do not appear in other context):

    def cursorinfo():						# get caret info
    	pos = editor.getCurrentPos()			# current caret position
    	LN = editor.lineFromPosition(pos)		# current line number
    	# LP = editor.positionFromLine(C)		# buffer position at current line start
    	return pos, LN
    	
    # replace text in brackets with a number 
    def replace_num(S, num):
    	LC = S.find( "[" ) 
    	if LC >= 0 :
    		RC = S.find( "]" )
    		number = str(num)
    		S_new = S[0:LC + 1] + number + S[RC:]
    		return S_new
    	return S
    
    # process all text
    def process():
    	textL = []
    	for i in range (0, total):
    		s = editor.getLine(i)		# get text in line
    		s = replace_num(s, i+1)
    		textL.append(s)
    	text = "".join(textL)
    	return text
    	
    pos, LN = cursorinfo()
    total = editor.getLineCount()       #  get lines total 
    
    editor.setText( process() )
    editor.gotoPos(pos)			# restore caret position