120
text
text
text
text

after the editing, those text becomes to

1 text
2 text
3 text
4 text

It's easy in Notepad ++ with the Column Editor, but I don't know how to do the same in Sublime Text 2.

4

8 Answers 8

135

The Text Pastry plugin does the job very well. It offers the Insert Numbers Syntax.

Select multiple lines with CMD+SHIFT+L (Sublime default):

   text|
   text|
   text|
   text|

Move the cursor where you want it:

 | text
 | text
 | text
 | text

And insert numbers with Text Pastry by hitting CMD+ALT+N (in Windows: CTRL+SHIFT+ALT+N) and entering 1 space 1 space 0:

 1| text
 2| text
 3| text
 4| text

Where 1 space 1 space 0 stands for:

  • Integer to start with 1
  • Increment by 1
  • Padd leading zeros 0

Replace CMD with CTRL on Windows or Linux machines.

3
  • 12
    For me the Text Pastry input is a text box. When I saw [1][1][0] above I thought it means typing 110 which will not get your what you want. You need to enter is 1 1 0 with spaces. When using kbd tags you include spaces? Just wondering what's the visual difference between 110 and 1 1 0 if using kbd tags.
    – AnnanFay
    Commented Feb 25, 2018 at 0:19
  • I'm sorry, I don't know how to do kbd spaces. But you get the idea now? :)
    – q9f
    Commented Feb 25, 2018 at 9:40
  • 2
    The correct answer is below by @Nicoolasens. No need for plugins
    – Dennis
    Commented Jan 7, 2023 at 20:54
115

copy of this post https://stackoverflow.com/a/64083371/5902698

You want to add a number at each row that you selected, but not the same. For example, you select 5 cursors and you want to write 1 2 3 4 5.

select your 5 cursors (maybe you can use the shortcut ctrl + shift + L)
enter image description here

ctrl + shift + P (P for macOS) and select Arithmetic enter image description here

Because you have 5 cursors, it propose 1 2 3 4 5
enter image description here enter image description here

If you want you can change your step of iteration
enter image description here

Or start from an other number than 1
enter image description here

Add even numbers
enter image description here

7
  • 2
    This answer looks simple. Will be better to mention how to select multiple cursers. CTRL+ALT+UP/DOWN for windows. Thanks! Commented Oct 9, 2020 at 5:31
  • 13
    amazing - so happy I scrolled past all the plugin suggestions
    – p-mcgowan
    Commented Nov 30, 2020 at 14:14
  • 1
    I've been working with sublime text for a long time, and I've could've used this forever... thanks so much! Commented Oct 12, 2021 at 19:07
  • 1
    perfect answer, no plugin required
    – caub
    Commented Oct 19, 2021 at 15:17
  • 1
    This is just brilliant! I have always hated having to install plugins to automate such trivial tasks... Interestingly, the number of steps involved is same as that by a plugin or even less in some cases, lol.
    – Fr0zenFyr
    Commented Jul 14, 2023 at 14:42
49

For this particular case you can use Increment Selection package. Just press Ctrl+Alt+I (Command+Control+I) while having multiple cursors at the beginning of each line. In addition to melinath's answer, here's an example of how to do it:

You will need Package Control first. It shouldn't take more than 30 seconds to install both things.

Steps:

  1. Install Package Control.
  • Open Command Palette: Ctrl+Shift+P (Mac: Command+Shift+P).
  • Type Install Package Control and click to install.
  1. 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.
  1. Add line numbers to the beginning of each line.
  • Select all lines with Ctrl+A (Mac: Command+A)
  • Change selection to multiple lines with Ctrl+Shift+L (Mac: Command+Shift+L)
  • Go to the start of each line by pressing Home (Mac: Command+)
  • Use Increment Selection with Ctrl+Alt+I (Mac: Command+Control+I)

Result:

increment-selection.gif


Other examples for Increment Selection

Increment Selection can also replace numbers, prefix numbers with leading zeroes, increment letters, increment by a step 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|

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

Featured similar plugins

  • Selection Evaluator: Evaluate selected mathematical expression with Ctrl+Shift+M (Mac: Command+Shift+M).
2
  • 3
    FWIW this solution works in sublime text 3.2.2 build 3211 as well. Total game changer no need to have a spreadsheet open just to produce a series of numbers.
    – VanBantam
    Commented Mar 30, 2020 at 22:43
  • 1
    you saved my hours. thumbs up
    – NomanJaved
    Commented Aug 18, 2020 at 11:44
26

The IncrementSelection plugin enables this behavior. If you have Package Control installed, you can just search for and install it. Easy peasy!

1
  • I found this plugin to be quite excellent. I was manually generating an SVG pattern and needed to increment several plot positions. The plugin did the math and everything for me. <path d="M330,0C220,63.7,0,191,0,191"/><path d="M330-2C220,61.7,0,189,0,189"/> <path d="M330-_C220,__.7,0,___,0,___"/> Commented Dec 31, 2016 at 1:09
3

Use Emmet package try this :

  {$ text${newline}}*4
2
  • 1
    Very good! BTW, if you want to change the counter base value to 3 (e.g. 3, 4, 5, 6), replace the first $ with $@3. If you want to reverse the counter (e.g. 4, 3, 2, 1), replace the first $ with $@-. If you want to reverse the counter back to 3 (e.g. 6, 5, 4, 3), replace the first $ with $@-3. If you want padding zeros (e.g. 001, 002, 003, 004), replace the first $ with $$$. See docs.emmet.io/abbreviations/syntax/#item-numbering
    – Rockallite
    Commented Oct 25, 2017 at 12:21
  • This came in handy! {$ => $,${newline}}*12 to create array with keys & values, months of the year Commented Nov 15, 2018 at 1:23
3

My simple workaround (up until now when I found this thread) was to go to my spreadsheet editor generate the sequence there, copy, back to subl, expand the cursor over however many lines and paste.

This approach can be applied in any text editor that allows you to expand (clone) the cursor.

2

You can use the plugin ConyEdit to do this, use its command line cc.abl '#1 ' to append before lines with the contents that you want.

-1

@Nicoolasens This answer is great! Just adding one detail: On macOS you can follow these steps to put a curser at the end of each line:

  1. Use command+A to choose all lines;
  2. Use command+shift+L to put a curser after each line.
1

Not the answer you're looking for? Browse other questions tagged or ask your own question.