Sublime Text 2 increment numbers
Asked Answered
E

4

17

I have a JSON file that looks like this:

    "Algeriet" :
    [
        {
            "name" : "Nyårsdagen",
            "date" : "2013-01-01",
            "ID" : "1"
        },
        {
            "name" : "Mawlid En Nabaoui Echarif",
            "date" : "2013-01-24",
            "ID" : "2"
        },
        {
            "name" : "Första maj",
            "date" : "2013-05-01",
            "ID" : "3"
        },
       ...
     ]

Now I would like to begin incrementing the IDs from 0 instead of 1. How can I do this with Sublime Text 2? I have installed the Text Pastry plugin but I'm not sure how to select the IDs in the text so that I can replace these values.

Esemplastic answered 4/3, 2013 at 10:0 Comment(0)
E
31

Solved it by doing these steps:

  1. Do a find and replace for regex "ID" : "\d+" and replacing it with a string which I know does not exist anywhere in the file (I replaced it with "ID" : "xyz"
  2. Make a multiple selection on "ID" : "xyz"
  3. Using the Text Pastry plugin "Number Sequence (\i)" on the multiple selection
Esemplastic answered 4/3, 2013 at 12:36 Comment(1)
Other option: 1. Select the text (only one occurrence): "ID" : " 2. Press ALT+F3 (command "find_all_under") to select all occurrences 3. Navigate to the first character (Right arrow) and select all IDs (CTRL+SHIFT+Right). Now you have all identifiers selected.Mystical
J
21

You can use Increment Selection. Just press Ctrl+Alt+I over multiple selections of numbers.

You will need Package Control first. All of this shouldn't take more than 30 seconds to install.

Steps:

  1. Install Package Control.
    • Open Command Palette: Ctrl+Shift+P (Windows/Unix) / Command+Shift+P (Mac).
    • Type Install Package Control and click to install.
  2. Install Increment Selection package.
    • Open Command Palette again.
    • Type Package Control: Install Package, click on it and wait a short period.
    • Type Increment Selection and click on it to install.
  3. Select multiple numbers and press Ctrl+Alt+I (Windows/Unix) / Command+Control+I (Mac).

Result:

invert-selection.gif


Other examples

Increment Selection can also prefix numbers with leading zeroes, increment letters, increment by a step, generate line numbers and more.

   [1] text    [1] text    [1] ->  1| text  2| text  3|
   [a] text    [a] text    [a] ->  a| text  b| text  c|
  [01] text   [01] text   [01] -> 01| text 02| text 03|
[05,3] text [05,3] text [05,3] -> 05| text 08| text 11|
[5,-1] text [5,-1] text [5,-1] ->  5| text  4| text  3|

[#] line -> 1| line
[#] line -> 2| line
[#] line -> 3| line
[#] line -> 4| line
[#] line -> 5| line

Hint: [] stands for a selection, | stands for a caret.

Judicial answered 13/4, 2019 at 1:3 Comment(0)
H
7

With the new add-text-with-sequence feature of the Text Pastry plugin, it's even a step less:

  1. Find all "ID" : "\d+" (with regex search activated)
  2. In the Text Pastry Command Line, enter as "ID" : "0"
Hygiene answered 16/5, 2017 at 14:7 Comment(2)
This is now better then accepted answer. It's simple & it works! Don't forget to add + in regex: \d+ if you have numbers with more digits.Bluegrass
it's 2020/5/5, and this answer works for Text Pastry, ST3. I prefer this answer because I have to replace 1k+ selections.Ballon
C
0

This is now possible out of the box, I think. I got it from an answer to a question about inserting consecutive numbers. I also posted a similar answer to a similar question about increasing selected numbers

  1. Multi-select the numbers. For example, by highlighting the text "ID" : " of one entry, then typing cmd + ctrl + g to select all matching entries, then moving your cursor so you can select just the number parts of the text.
  2. cmd + shift + P
  3. Pick "Arithmetic"
  4. Type in x - 1
  5. Press enter
Capitalist answered 6/6, 2024 at 16:27 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.