Adding a comma to each line using sublime text 2

I am trying to use sublime text search and replace function and regular expression to match a string of numbers in each line and add a comma to each. So here is an example file:

273794103 418892296 134582886 380758661 109829186 248050497 2167935715 374858669 

I want this to be:

  273794103, 418892296, 134582886, 380758661, 109829186, 248050497, 2167935715, 374858669, 

I tried to do this (\d+)\n and replace it with $1, , but that will not work. Any idea why? FYI For those who are not in sublime, but in regular expression, Sublime Text uses the Python regular expression mechanism.

+48
regex sublimetext2
Nov 03 '13 at 6:43
source share
8 answers

I would recommend this

Find What : $ // match all the ends of your lines
'Replace with :, // replaces all line ends with coma

this will work with any file :-)

+65
Nov 03 '13 at 6:51
source share

To add a comma to any line

  • Select the lines you want to change.

  • CTRL + SHIFT + L

  • RIGHT_ARROW

  • COMMA

Using ctrl + shift + L is a way to change all selected rows. Very comfortably: -)

+150
Dec 27 '13 at 23:31
source share

Replacing .+ With $0, worked for me

+4
Nov 03 '13 at 6:47
source share

You can also use multicursors in ST for this. Select the area, go to Selection -> Split into Lines (for this there is a key binding, but it is platform-specific, it will be indicated next to the menu entry), click to the right and insert a comma.

+3
Nov 03 '13 at 7:38
source share

Here's how you do it on a Mac:

Command + shift + L > Right Arrow > Comma


and Windows / Linux :

Ctrl + shift + L > Right Arrow > Comma

+2
Oct 02 '17 at 7:07 on
source share

I tried in eclipse on mac, it works fine for me.

 Find: '(.)$' Replace with: '$1");' 

In my case, I have to add ''); 'at the end of the line. You can replace, depending on your needs.

+1
Jul 23 '16 at 8:02
source share

I tried to do this (\ d +) \ n and replace it with $ 1 , but that doesn’t work. Any idea why?

A single string search stops at \n , so it cannot be part of a regular expression. Instead, try using the $ end of line qualifier

 s/(\d+)$/$1,/ 
0
Nov 03 '13 at 9:03
source share

I can use the following macro:

 [ { "args": null, "command": "split_selection_into_lines" }, { "args": { "by": "characters", "forward": true }, "command": "move" }, { "args": { "characters": "," }, "command": "insert" }, { "args": { "extend": false, "to": "eof" }, "command": "move_to" } ] 

save to comma.sublime-macro and change key bindings - user

{"keys": ["super +"], "Command": "run_macro_file", "arg": {"file": "Packages / user / comma.sublime-macro"}},

PD: you need the preium to highlight your lines to add a comma.

-one
Aug 05 '16 at 2:53 on
source share



All Articles