How to paste text to end of every line? Sublime 2
Asked Answered
F

7

167

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.

Firebug answered 22/5, 2012 at 20:42 Comment(1)
Currently this is the second demo on the homepage... sublimetext.comPrismoid
S
29

You can use the Search & Replace feature with this regex ^([\w\d\_\.\s\-]*)$ to find text and the replaced text is "$1".

Speiss answered 22/5, 2012 at 20:45 Comment(2)
Awesome! 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 !Firebug
If 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?Plumbing
K
594

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.

Kamila answered 20/7, 2012 at 10:54 Comment(10)
This 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.Askja
You 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+JTrigraph
Great! Regex is an overkill for such a task that is built-in nativelyLadylove
This is a good answer, but for very large line counts, it can be quite alot more CPU intensive than using find & replace.Ongun
@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.Kamila
@Kamila 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?Brindisi
How 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.Auspex
@jim you could apply for all lines and replace >\n with just \nKamila
To 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.Antoninus
This 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.Buddha
G
81

Here's the workflow I use all the time, using the keyboard only

  1. Ctrl/Cmd + A Select All
  2. Ctrl/Cmd + Shift + L Split into Lines
  3. ' Surround every line with quotes

Note that this doesn't work if there are blank lines in the selection.

Grilse answered 2/11, 2012 at 10:23 Comment(4)
does not work for me in st2, step 3 deleted all lines and replaced it with " the step 3 should be Home"End"Rubenstein
@Rubenstein - that sounds like a bug. Make sure you are using the newest version of st2, and if so, report the bug at sublimetext.userecho.comAskja
@dbw running sublime text 2.0.1 build 2217. Posted as bug at sublimetext.userecho.com/topic/184875-Rubenstein
@Rubenstein I can reproduce your problem if I have any blank lines in the selectionGrilse
E
61
  1. 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.)
  2. Push Ctrl+Shift+L.
  3. Push Home key and add prefix.
  4. Push End key and add suffix.

Note, disable wordwrap, otherwise it will not work properly if your lines are longer than sublime's width.

Editorial answered 16/3, 2016 at 7:1 Comment(3)
this is the actual fix. the home and the end keys are crucial for this to work properlyJessalin
In 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/…Microhenry
Not really @Phil. You can also use Cmd+Left/Right arrow to navigate as well. I think the same will work on windows tooBarrault
P
38

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"
Photoflood answered 22/12, 2013 at 9:52 Comment(1)
worked really well in sublime text 3, saved tons of timeArmington
S
29

You can use the Search & Replace feature with this regex ^([\w\d\_\.\s\-]*)$ to find text and the replaced text is "$1".

Speiss answered 22/5, 2012 at 20:45 Comment(2)
Awesome! 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 !Firebug
If 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?Plumbing
A
24

Use column selection. Column selection is one of the unique features of Sublime2; it is used to give you multiple matched cursors (tutorial here). To get multiple cursors, do one of the following:

Mouse:

  • Hold down the shift (Windows/Linux) or option key (Mac) while selecting a region with the mouse.

  • Clicking middle mouse button (or scroll) will select as a column also.

Keyboard:

  • Select the desired region.
  • Type control+shift+L (Windows/Linux) or command+shift+L (Mac)

You now have multiple lines selected, so you could type a quotation mark at the beginning and end of each line. It would be better to take advantage of Sublime's capabilities, and just type ". When you do this, Sublime automatically quotes the selected text.

Type esc to exit multiple cursor mode.

Askja answered 10/12, 2012 at 0:50 Comment(2)
You can also trigger column selection by middle-clicking and dragging.Cordiality
This 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.Pinero
G
2
  1. Select all lines you want to add a suffix or prefix.(command+ A to select all the lines)
  2. Press command+shift+L. This will put one cursor at the end of every line and all the selected lines would still be selected.
  3. For adding suffix press command+right and for adding prefix command+left. This will deselect all the earlier selected text and there will only be cursors at the end or start of every line.
  4. Add required text
Globe answered 10/1, 2023 at 7:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.