How to paste text to end of every line? Sublime 2

149,136

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

  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.

Solution 4

  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.

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"
Share:
149,136

Related videos on Youtube

Tony H.
Author by

Tony H.

Updated on July 08, 2022

Comments

  • Tony H.
    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
      TWiStErRob over 11 years
      Currently this is the second demo on the homepage... sublimetext.com
  • Tony H.
    Tony H. almost 12 years
    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 !
  • Yada
    Yada almost 12 years
    This is so much easier to do.
  • Tiago Almeida
    Tiago Almeida over 11 years
    So useful that I had to suggest an edit to improve the keys layout.
  • sehe
    sehe over 11 years
    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?
  • dbn
    dbn over 11 years
    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.
  • F. Mayoral
    F. Mayoral about 11 years
    Thank you for saving 3 hours of my life
  • fionbio
    fionbio about 11 years
    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
  • Curtis Gibby
    Curtis Gibby about 11 years
    You can also trigger column selection by middle-clicking and dragging.
  • andrej
    andrej about 11 years
    does not work for me in st2, step 3 deleted all lines and replaced it with " the step 3 should be Home"End"
  • Vladimir Hraban
    Vladimir Hraban almost 11 years
    Great! Regex is an overkill for such a task that is built-in natively
  • Pieter Schreurs
    Pieter Schreurs almost 11 years
    This is a good answer, but for very large line counts, it can be quite alot more CPU intensive than using find & replace.
  • dbn
    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
    andrej almost 11 years
    @dbw running sublime text 2.0.1 build 2217. Posted as bug at sublimetext.userecho.com/topic/184875-
  • Patrick McDonald
    Patrick McDonald almost 11 years
    @andrej I can reproduce your problem if I have any blank lines in the selection
  • palaniraja
    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
    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
    Jim Raynor almost 10 years
    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.
  • palaniraja
    palaniraja almost 10 years
    @jim you could apply for all lines and replace >\n with just \n
  • Michael Osofsky
    Michael Osofsky over 9 years
    I think this deserves to be the preferred answer
  • Code Whisperer
    Code Whisperer over 9 years
    M-m-m-m-monster killlllll!
  • jkd
    jkd about 9 years
    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.
  • Daniel Flippance
    Daniel Flippance almost 9 years
    Oh Em Gee, that's the coolest feature I've ever seen in a text editor
  • Matt
    Matt over 8 years
    I've used Sublime for nearly 3 years and I cannot believe i've only just discovered how to do this... Thanks!
  • Dribbler
    Dribbler over 8 years
    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.
  • Phil
    Phil over 7 years
    this is the actual fix. the home and the end keys are crucial for this to work properly
  • james-see
    james-see over 6 years
    worked really well in sublime text 3, saved tons of time
  • avance
    avance over 6 years
    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/…
  • vladli
    vladli almost 6 years
    Not 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
    Mike Nguyen over 4 years
    Work like an amazing charm!
  • Devashish
    Devashish over 3 years
    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.