How to paste text to end of every line? Sublime 2
Solution 1
You can use the Search & Replace feature with this regex ^([\w\d\_\.\s\-]*)$
to find text and the replaced text is "$1"
.
Solution 2
Yeah Regex is cool, but there are other alternative.
- Select all the lines you want to prefix or suffix
- Goto menu Selection -> Split into Lines (Cmd/Ctrl + Shift + L)
This allows you to edit multiple lines at once. Now you can add *Quotes (") or anything * at start and end of each lines.
Solution 3
Here's the workflow I use all the time, using the keyboard only
- Ctrl/Cmd + A Select All
- Ctrl/Cmd + Shift + L Split into Lines
- ' Surround every line with quotes
Note that this doesn't work if there are blank lines in the selection.
Solution 4
- Select all the lines on which you want to add prefix or suffix. (But if you want to add prefix or suffix to only specific lines, you can use ctrl+Left mouse button to create multiple cursors.)
- Push Ctrl+Shift+L.
- Push Home key and add prefix.
- Push End key and add suffix.
Note, disable wordwrap, otherwise it will not work properly if your lines are longer than sublime's width.
Solution 5
Let's say you have these lines of code:
test line one
test line two
test line three
test line four
Using Search and Replace Ctrl+H with Regex let's find this: ^
and replace it with "
, we'll have this:
"test line one
"test line two
"test line three
"test line four
Now let's search this: $
and replace it with "
, now we'll have this:
"test line one"
"test line two"
"test line three"
"test line four"
Related videos on Youtube
Tony H.
Updated on July 08, 2022Comments
-
Tony H. almost 2 years
I'm curious if there is a way to paste text to the end of every line in Sublime 2? And conversely, to the beginning of every line.
test line one
test line two
test line three
test line four
...
Say you have 100 lines of text in the editor, and you want to paste quotation marks to the beginning and end of each line.
Is there an easy way to do this or a plugin that anyone would know of? This would often save me a lot of time on various projects.
Thanks.
-
TWiStErRob over 11 yearsCurrently this is the second demo on the homepage... sublimetext.com
-
-
Tony H. almost 12 yearsAwesome! So i found the following worked for me: Using regex: (?>\x0D\x0A?|[\x0A-\x0C\x85\x{2028}\x{2029}]) will find the end of every line. And using regex: ^[a-zA-z0-9!@#$")(*&!+_-] will find the beginning of every line (provided all characters are added for the regex to search for at the beginning each of the lines). Thanks @yohann !
-
Yada almost 12 yearsThis is so much easier to do.
-
Tiago Almeida over 11 yearsSo useful that I had to suggest an edit to improve the keys layout.
-
sehe over 11 yearsIf you're doing this with a regex in a powerful editor like Vim or ST2 you're missing the clue. Why degrade a decent editor into
sed
? -
dbn over 11 yearsThis is a much better answer. You can also use shift (Windows/Linux) or option key (Mac) while selecting a region with your mouse to get the same result.
-
F. Mayoral about 11 yearsThank you for saving 3 hours of my life
-
fionbio about 11 yearsYou may often find yourself editing multiple lines as described above, but then want to join them back together into a single line. Shortcut to join multiple lines: ctrl/cmd+J
-
Curtis Gibby about 11 yearsYou can also trigger column selection by middle-clicking and dragging.
-
andrej about 11 yearsdoes not work for me in st2, step 3 deleted all lines and replaced it with " the step 3 should be
Home
"End
" -
Vladimir Hraban almost 11 yearsGreat! Regex is an overkill for such a task that is built-in natively
-
Pieter Schreurs almost 11 yearsThis is a good answer, but for very large line counts, it can be quite alot more CPU intensive than using find & replace.
-
dbn almost 11 years@andrej - that sounds like a bug. Make sure you are using the newest version of st2, and if so, report the bug at sublimetext.userecho.com
-
andrej almost 11 years@dbw running sublime text 2.0.1 build 2217. Posted as bug at sublimetext.userecho.com/topic/184875-
-
Patrick McDonald almost 11 years@andrej I can reproduce your problem if I have any blank lines in the selection
-
palaniraja over 10 years@decker you could save the action as macro and try the csv after spliting them to multiple files. Any editor would struggle with a large file with multiple cursors. Give vim a try, it might work.
-
Admin about 10 years@palaniraja I have problems using this when I want to prefix all lines, including blank, empty ones, because the empty lines have their cursors placed at the end of the preceding line. Is there any way to place the cursor on the blank line itself?
-
Jim Raynor almost 10 yearsHow to deal with blank lines then? I want to block quote my paragraph by inserting the '>' to the beginning of every lines. In my paragraph there are blank lines :) I want to add the symbol to the beginning of these lines as well.
-
palaniraja almost 10 years@jim you could apply for all lines and replace
>\n
with just\n
-
Michael Osofsky over 9 yearsI think this deserves to be the preferred answer
-
Code Whisperer over 9 yearsM-m-m-m-monster killlllll!
-
jkd about 9 yearsTo place cursor at the beginning of the lines, on Mac go CMD+Left Arrow after following the directions above. Probably (not sure) CTRL+Left Arrow for windows.
-
Daniel Flippance almost 9 yearsOh Em Gee, that's the coolest feature I've ever seen in a text editor
-
Matt over 8 yearsI've used Sublime for nearly 3 years and I cannot believe i've only just discovered how to do this... Thanks!
-
Dribbler over 8 yearsThis is the most Sublime Text specific way to solve the problem and uses a fundamental feature of Sublime Text. As the OP inquired about Sublime Text in the title, it should be the accepted answer.
-
Phil over 7 yearsthis is the actual fix. the home and the end keys are crucial for this to work properly
-
james-see over 6 yearsworked really well in sublime text 3, saved tons of time
-
avance over 6 yearsIn Sublime Text 3 (for Mac), the home and end keys were not working for me. If you have this trouble, check: coderwall.com/p/upolqw/…
-
vladli almost 6 yearsNot really @Phil. You can also use Cmd+Left/Right arrow to navigate as well. I think the same will work on windows too
-
Mike Nguyen over 4 yearsWork like an amazing charm!
-
Devashish over 3 yearsThis is not a good solution when you are adding to an insane number of lines. In my case, I need to modify 250K lines at once.