ChatGPT解决这个技术问题 Extra ChatGPT

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

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.

Currently this is the second demo on the homepage... sublimetext.com

D
DocMax

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.


This is so much easier to do.
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.
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
This is a good answer, but for very large line counts, it can be quite alot more CPU intensive than using find & replace.
I think this deserves to be the preferred answer
P
Patrick McDonald

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.


does not work for me in st2, step 3 deleted all lines and replaced it with " the step 3 should be Home"End"
@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
@dbw running sublime text 2.0.1 build 2217. Posted as bug at sublimetext.userecho.com/topic/184875-
@andrej I can reproduce your problem if I have any blank lines in the selection
Y
Yuri P.

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.


this is the actual fix. the home and the end keys are crucial for this to work properly
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/…
Not really @Phil. You can also use Cmd+Left/Right arrow to navigate as well. I think the same will work on windows too
T
Tomatrox

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"

worked really well in sublime text 3, saved tons of time
Y
Yohann

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


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 !
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?
Thank you for saving 3 hours of my life
T
Toby Allen

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.


You can also trigger column selection by middle-clicking and dragging.
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.