adit
adit

Reputation: 33644

adding comma to each line using sublime text 2

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.

Upvotes: 86

Views: 100185

Answers (10)

Rajesh Prasad Yadav
Rajesh Prasad Yadav

Reputation: 908

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

Upvotes: 6

Jerrin stephen
Jerrin stephen

Reputation: 224

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

Upvotes: 0

hoangthienan
hoangthienan

Reputation: 866

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

Upvotes: 19

rral
rral

Reputation: 584

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.

Upvotes: -1

Sun
Sun

Reputation: 3564

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.

Upvotes: 1

John Foley
John Foley

Reputation: 4479

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 :-)

Upvotes: 238

jkshah
jkshah

Reputation: 11703

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,/

Upvotes: 0

skuroda
skuroda

Reputation: 19744

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.

Upvotes: 3

bukart
bukart

Reputation: 4906

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 :-)

Upvotes: 109

serejja
serejja

Reputation: 23851

Replacing .+ with $0, worked for me

Upvotes: 5

Related Questions