adding comma to each line using sublime text 2

RegexSublimetext2

Regex Problem Overview


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.

Regex Solutions


Solution 1 - Regex

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

Solution 2 - Regex

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

Solution 3 - Regex

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

Solution 4 - Regex

Replacing .+ with $0, worked for me

Solution 5 - Regex

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

Solution 6 - Regex

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.

Solution 7 - Regex

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.

Solution 8 - Regex

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

Solution 9 - Regex

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

Solution 10 - Regex

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionaditView Question on Stackoverflow
Solution 1 - RegexJohn FoleyView Answer on Stackoverflow
Solution 2 - RegexbukartView Answer on Stackoverflow
Solution 3 - RegexhoangthienanView Answer on Stackoverflow
Solution 4 - RegexserejjaView Answer on Stackoverflow
Solution 5 - RegexRajesh Prasad YadavView Answer on Stackoverflow
Solution 6 - RegexskurodaView Answer on Stackoverflow
Solution 7 - RegexSunView Answer on Stackoverflow
Solution 8 - RegexjkshahView Answer on Stackoverflow
Solution 9 - RegexJerrin stephenView Answer on Stackoverflow
Solution 10 - RegexrralView Answer on Stackoverflow