Reputation: 33644
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
Reputation: 908
For Window User:
select all line OR select part of line => Ctrl+A.
Bring cursor to last of each Line => Ctrl+Shift+L
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
Reputation: 224
Ctrl + H is the command to open the find what and replace with panel.
Upvotes: 0
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
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
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
Reputation: 4479
To add comma to any line
Select the lines you want to modify
CTRL + SHIFT + L
RIGHT_ARROW
COMMA
Using ctrl + shift + L is how you can modify all selected lines. Very handy :-)
Upvotes: 238
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
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
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