113
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.

yulanggong
  • 1,348
  • 2
  • 9
  • 12
  • 6
    Maybe this question can be useful: http://stackoverflow.com/questions/14574941/add-a-number-to-each-selection-in-sublime-text-2-incremented-once-per-selection – Riccardo Marotti Feb 01 '13 at 08:38
  • This is a duplicate of the question to which @RiccardoMarotti linked. – dbn Feb 01 '13 at 20:42
  • 1
    I recommend the plugin Text Pastry. I posted about it in the [related question](http://stackoverflow.com/a/14670627/776778) – aanton Feb 03 '13 at 08:17
  • 2
    Nice plugin! I've wrote a plugin based on @Riccardo Marotti's answer, https://github.com/yulanggong/IncrementSelection – yulanggong Feb 03 '13 at 08:25

8 Answers8

132

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 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.

q9f
  • 11,293
  • 8
  • 57
  • 96
  • 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 Feb 25 '18 at 00:19
  • I'm sorry, I don't know how to do kbd spaces. But you get the idea now? :) – q9f Feb 25 '18 at 09:40
  • 1
    The correct answer is below by @Nicoolasens. No need for plugins – Dennis Jan 07 '23 at 20:54
98

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

You want to have a number at each row that you selected, but not the same. For exemple, 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 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

Nicoolasens
  • 2,871
  • 17
  • 22
  • 2
    This answer looks simple. Will be better to mention how to select multiple cursers. CTRL+ALT+UP/DOWN for windows. Thanks! – Tintu Thomas Oct 09 '20 at 05:31
  • 12
    amazing - so happy I scrolled past all the plugin suggestions – p-mcgowan Nov 30 '20 at 14:14
  • @TintuThomas it doesn't work on my laptop, but I edit to mention the ctrl + shift + L to put multiple cursor on a selection of lines – Nicoolasens Dec 04 '20 at 11:14
  • 1
    I've been working with sublime text for a long time, and I've could've used this forever... thanks so much! – Paul Carlton Oct 12 '21 at 19:07
  • 1
    perfect answer, no plugin required – caub Oct 19 '21 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 Jul 14 '23 at 14:42
  • 1
    As bonus, I have upvoted the copy of this answer on the other post you linked. Cheers! – Fr0zenFyr Jul 14 '23 at 14:43
46

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).
bobasti
  • 1,778
  • 1
  • 20
  • 28
24

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

melinath
  • 800
  • 6
  • 10
  • 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. ``` ``` – davidcondrey Dec 31 '16 at 01:09
3

Use Emmet package try this :

  {$ text${newline}}*4
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • 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 https://docs.emmet.io/abbreviations/syntax/#item-numbering – Rockallite Oct 25 '17 at 12:21
  • This came in handy! `{$ => $,${newline}}*12` to create array with keys & values, months of the year – Craig London Nov 15 '18 at 01: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.

Tony Vlcek
  • 446
  • 4
  • 18
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.

Dick
  • 49
  • 3
-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.
  • Please don't add "thank you" as an answer. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation), you will be able to [vote up questions and answers](https://stackoverflow.com/help/privileges/vote-up) that you found helpful. - [From Review](/review/late-answers/31715570) – Dan May 13 '22 at 08:25