81

I am trying to use sublime's text search and replace function and regex to match a string of number in each line and append a comma to each. So here's the sample file:

 273794103
 418892296
 134582886
 380758661
 109829186
 248050497
 2167935715
 374858669

I want this to be:

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

I tried doing this (\d+)\n and replacing it with $1, but this doesn't work. Any idea why? FYI for those who are not into sublime but into regex, Sublime Text uses Python's regex engine.

adit
  • 32,574
  • 72
  • 229
  • 373

10 Answers10

231

To add comma to any line

  1. Select the lines you want to modify

  2. CTRL + SHIFT + L

  3. RIGHT_ARROW

  4. COMMA

Using ctrl + shift + L is how you can modify all selected lines. Very handy :-)

John Foley
  • 4,373
  • 3
  • 21
  • 23
108

I'd recommend this

'Find What': $ // matching all ends of your lines
'Replace With': , // replaces all line ends with a coma

this will work with any file :-)

bukart
  • 4,906
  • 2
  • 21
  • 40
16

Here's how you'd do it on a Mac:

Command+shift +L > Right Arrow > Comma


and Windows/Linux:

Ctrl+Shift +L > Right Arrow > Comma

hoangthienan
  • 826
  • 1
  • 9
  • 14
5

Replacing .+ with $0, worked for me

serejja
  • 22,901
  • 6
  • 64
  • 72
4

For Window User:

  1. select all line OR select part of line => Ctrl+A.

  2. Bring cursor to last of each Line => Ctrl+Shift+L

  3. Add comma(,) which will reflect to all line.

** If you want to add comma(,) at start of each Line , After step 2 press => Home(button from keyboard , all cursors will head to start of the line)

Finally Ctrl+s to save the changes.

cheers

3

You can also use the multi cursors in ST to do it. Highlight the region, go to Selection -> Split into Lines (there's a key binding for this, but it's platform specific. It'll be listed next to the menu entry), press right, and insert the comma.

skuroda
  • 19,514
  • 4
  • 50
  • 34
1

I tried in eclipse in mac it working fine for me.

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

My case I have to add '");' at the end of line. You can replace, as per your need.

Sun
  • 3,444
  • 7
  • 53
  • 83
0

I tried doing this (\d+)\n and replacing it with $1, but this doesn't work. Any idea why?

Single line search stops at \n, hence it can't be part of regex. Instead, try using end of line specifier $

s/(\d+)$/$1,/
jkshah
  • 11,387
  • 6
  • 35
  • 45
0

Ctrl + H is the command to open the find what and replace with panel.

Tim Diekmann
  • 7,755
  • 11
  • 41
  • 69
Jerrin stephen
  • 204
  • 3
  • 12
-1

I can use the next 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 in comma.sublime-macro and edit Key Bindings - User

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

PD: you need previum select your lines to add comma.

rral
  • 554
  • 3
  • 20
  • because -1 ?? You can check here the output: https://www.dropbox.com/s/ri0cn4n1e9n0i1r/Kazam_screencast_00000.mp4?dl=0 That's wrong? – rral Aug 05 '16 at 07:30
  • I guess the downvote is for the way of the solution... for me it seems to work, but also seems to be to much for such a small problem – bukart Aug 11 '16 at 08:44