122
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
  • 6
    Maybe this question can be useful: stackoverflow.com/questions/14574941/… Commented Feb 1, 2013 at 8:38
  • This is a duplicate of the question to which @RiccardoMarotti linked. Commented Feb 1, 2013 at 20:42
  • 1
    I recommend the plugin Text Pastry. I posted about it in the related question Commented Feb 3, 2013 at 8:17
  • 2
    Nice plugin! I've wrote a plugin based on @Riccardo Marotti's answer, github.com/yulanggong/IncrementSelection Commented Feb 3, 2013 at 8:25

8 Answers 8

136

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.

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

3 Comments

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.
I'm sorry, I don't know how to do kbd spaces. But you get the idea now? :)
The correct answer is below by @Nicoolasens. No need for plugins
125

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 Comments

This answer looks simple. Will be better to mention how to select multiple cursers. CTRL+ALT+UP/DOWN for windows. Thanks!
amazing - so happy I scrolled past all the plugin suggestions
I've been working with sublime text for a long time, and I've could've used this forever... thanks so much!
perfect answer, no plugin required
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.
|
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 Comments

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.
you saved my hours. thumbs up
26

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

1 Comment

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,___"/>
3

Use Emmet package try this :

 {$ text${newline}}*4 

2 Comments

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
This came in handy! {$ => $,${newline}}*12 to create array with keys & values, months of the year
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.

Comments

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.

Comments

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

Please don't add "thank you" as an answer. Once you have sufficient reputation, you will be able to vote up questions and answers that you found helpful. - From Review

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.