106

Lets say I want to have a 10 rows of data but I want a value to increment for each row or piece of data. How do I increment that value?

For example....If I have these rows, is there a regex way of replacing the id values to increment?

<row id="1" />
<row id="1" />
<row id="1" />
<row id="1" />
<row id="1" />

--- Here is what I would like it to look like... (if the first row's id goes up one thats ok)

<row id="1" />
<row id="2" />
<row id="3" />
<row id="4" />
<row id="5" />
3
  • @Amber Notepad++ has a limited regex functionality. Commented Sep 29, 2011 at 20:13
  • 2
    Though regex was mentioned (and tagged) as a possible method, the OP seemed less concerned with the particular method than just knowing if Notepad++ could do the task....which voithos' answer addresses perfectly. Commented Sep 5, 2015 at 22:14
  • 1
    if someone want this for VS Code: marketplace.visualstudio.com/items?itemName=Asuka.insertnumbers Commented Jun 21, 2023 at 18:11

5 Answers 5

176

Not sure about regex, but there is a way for you to do this in Notepad++, although it isn't very flexible.

In the example that you gave, hold Alt and select the column of numbers that you wish to change. Then go to Edit->Column Editor and select the Number to Insert radio button in the window that appears. Then specify your initial number and increment, and hit OK. It should write out the incremented numbers.

Note: this also works with the Multi-editing feature (selecting several locations while maintaining Ctrl key pressed).

This is, however, not anywhere near the flexibility that most people would find useful. Notepad++ is great, but if you want a truly powerful editor that can do things like this with ease, I'd say use Vim.

Sign up to request clarification or add additional context in comments.

4 Comments

To me was: select the column with Alt+Shift and then do the Number to insert. Works perfectly
Mark the block with Alt and pressed left mouse button, then press ALT+C and choose the desired start and end numbers and the incremental value
Tip: If you want to add a number to all lines, it works also without selecting anything. It will then start adding the numbers on every line starting at the line where the cursor is, at the column of the cursor. Bonus tip: It doesnt really like putting the number at the end if all lines are different lenghts, so it is then best to put it at the start of the line and then move the newline to after the number with a regex
19

I was looking for the same feature today but couldn't do this in Notepad++. However, we have TextPad to our rescue. It worked for me.

In TextPad's replace dialog, turn on regex; then you could try replacing

<row id="1"/>

by

<row id="\i"/>

Have a look at this link for further amazing replace features of TextPad - http://sublimetext.userecho.com/topic/106519-generate-a-sequence-of-numbers-increment-replace/

2 Comments

I guess it helps if I actually pay attention. Ended up installing SublimeText. :) Thanks.
But Make sure you hit "Replace All" button. "Replace next" will not increment the counter. It took hours to figure this out. \i Replace with numbers starting from 1, incrementing by 1. \i(10) Replace with numbers starting from 10, incrementing by 1. \i(0,10) Replace with numbers starting from 0, incrementing by 10. \i(100,-10) Replace with numbers starting from 100, decrementing by -10.
15

i had the same problem with more than 250 lines and here is how i did it:

for example :

<row id="1" />
<row id="1" />
<row id="1" />
<row id="1" />
<row id="1" />

you put the cursor just after the "1" and you click on alt + shift and start descending with down arrow until your reach the bottom line now you see a group of selections click on erase to erase the number 1 on each line simultaneously and go to Edit -> Column Editor and select Number to Insert then put 1 in initial number field and 1 in incremented by field and check zero numbers and click ok

Congratulations you did it :)

Comments

6

Since there are limited real answers I'll share this workaround. For really simple cases like your example you do it backwards...

From this

1
2
3
4
5

Replace \r\n with " />\r\n<row id=" and you'll get 90% of the way there

1" />
<row id="2" />
<row id="3" />
<row id="4" />
<row id="5

Or is a similar fashion you can hack about data with excel/spreadsheet. Just split your original data into columns and manipulate values as you require.

|   <row id="   |   1   |   " />    |
|   <row id="   |   1   |   " />    |
|   <row id="   |   1   |   " />    |
|   <row id="   |   1   |   " />    |
|   <row id="   |   1   |   " />    |

Obvious stuff but it may help someone doing the odd one-off hack job to save a few key strokes.

1 Comment

replacing (.*) with <row id="\1" /> in regex mode without . matches newline will get you 100% of the way there.
4

http://docs.notepad-plus-plus.org/index.php/Inserting_Variable_Text

Notepad++ comes equipped with a Edit -> Column "Alt+C" Editor which can work on a rectangular selection in two different ways: Coledit.png inserting some fixed text on every line including and following the current line, at the column of the insertion point (aka caret). Initially selected text is left untouched. As the picture illustrates, a linear series of numbers can be inserted in the same manner. The starting value and increment are to be provided. Left padding with zeroes is an option, and the number may be entered in base 2, 8, 10 or 16 - this is how the computed values will be displayed too, padding being based on the largest.

1 Comment

It will be much better if you attach the image in the post to avoid confusion reading your answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.