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.

Tiago Mussi
  • 801
  • 3
  • 13
  • 20
Tony H.
  • 1,975
  • 4
  • 14
  • 20

7 Answers7

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.

DocMax
  • 12,094
  • 7
  • 44
  • 44
palaniraja
  • 10,432
  • 5
  • 43
  • 76
  • 7
    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. – dbn Dec 10 '12 at 00:41
  • 5
    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+J – fionbio Mar 14 '13 at 16:29
  • Great! Regex is an overkill for such a task that is built-in natively – Vladimir Hraban May 24 '13 at 08:44
  • 3
    This is a good answer, but for very large line counts, it can be quite alot more CPU intensive than using find & replace. – Ceilingfish Jun 05 '13 at 09:53
  • 1
    @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. – palaniraja Oct 10 '13 at 05:31
  • @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? –  Feb 26 '14 at 03:09
  • 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. – Jim Raynor May 07 '14 at 08:11
  • @jim you could apply for all lines and replace `>\n` with just `\n` – palaniraja May 11 '14 at 11:14
  • 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. – jkd Mar 11 '15 at 14:55
  • 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. – Devashish Nov 21 '20 at 11:04
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.

Patrick McDonald
  • 64,141
  • 14
  • 108
  • 120
  • 3
    does not work for me in st2, step 3 deleted all lines and replaced it with " the step 3 should be `Home`"`End`" – andrej Apr 26 '13 at 07:26
  • @andrej - that sounds like a bug. Make sure you are using the newest version of st2, and if so, report the bug at http://sublimetext.userecho.com/ – dbn Jun 06 '13 at 18:10
  • 1
    @dbw running sublime text 2.0.1 build 2217. Posted as bug at http://sublimetext.userecho.com/topic/184875-/ – andrej Jun 11 '13 at 09:55
  • @andrej I can reproduce your problem if I have any blank lines in the selection – Patrick McDonald Jun 11 '13 at 10:50
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.

Yuri P.
  • 187
  • 5
Harry
  • 1,282
  • 12
  • 17
  • 3
    this is the actual fix. the home and the end keys are crucial for this to work properly – Phil Aug 25 '16 at 18:03
  • In Sublime Text 3 (for Mac), the home and end keys were not working for me. If you have this trouble, check: https://coderwall.com/p/upolqw/fix-sublime-text-home-and-end-key-usage-on-mac-osx – avance Jan 24 '18 at 22:26
  • Not really @Phil. You can also use Cmd+Left/Right arrow to navigate as well. I think the same will work on windows too – vladli May 14 '18 at 12:52
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"
Tomatrox
  • 693
  • 7
  • 14
29

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

Yohann
  • 6,047
  • 3
  • 26
  • 33
  • 7
    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 ! – Tony H. May 22 '12 at 21:25
  • 5
    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`? – sehe Dec 09 '12 at 03:07
  • Thank you for saving 3 hours of my life – F. Mayoral Mar 11 '13 at 16:35
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.

Toby Allen
  • 10,997
  • 11
  • 73
  • 124
dbn
  • 13,144
  • 3
  • 60
  • 86
  • 3
    You can also trigger column selection by middle-clicking and dragging. – Curtis Gibby Apr 23 '13 at 18:12
  • 2
    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. – Dribbler Feb 02 '16 at 14:03
1
  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