How to edit all lines in Visual Studio Code - visual-studio-code

I have a list of data to which I need to put a ' symbol at the start of the line and at the end of the line. So the original data looks like this:
abcde
cdeab
deabc
eabcd
And I want all of the lines to look like this:
'abcde'
'cdeab'
'deabc'
'eabcd'
In my real data, I would have 10,000 of lines. So if I can do something like Ctrl+Shift+A to select the entire document and then have some magic shortcut to change from selecting all lines to editing all lines that would be perfect!

You could edit and replace with a regex:
Find (Ctrl+F):
^(.+)$
Replace:
'$1'
This regex finds any content on a line and wraps it inside quotes. The $1 refers to whatever is matched inside the parentheses in the regex. In this case, it's "one or more characters" i.e. everything on the line. Be sure to tick the regex icon.
If every line may or may not have a space before the content, and you want every line to have a space, try this:
Find:
^ ?(.+)$
Replace (notice the space before the first quote):
'$1'

Here is an easy way to do this:
Ctrl+A to select all or select your desired text.
Shift+Alt+I to put a cursor at the end of each line.
Type your ' (or whatever you want at the end).
Home will move all your cursors to the beginning of the lines.
Type your ' (or whatever you want at the beginning of all the lines).

You can use the Alt + Shift shortcut.
First press Alt + Shift then click the mouse button on the first line.
Go to the last line, and then do the same.
This will mark all the parts of one side. Whatever you type will be reflected in the marked spaces.
Do the same on the other side too.

Use Toggle Multi curosr Modified from action pane.
Select the cursor points with ctrl + <Mouse click> , you can modify everything simultaneously.
This will require lots of manual efforts if lines are more

You can use Find and Replace.
Besides, paste to Excel and using a function to add character '.

The first thing that came to my mind - replace abcde with 'abcde' line by using option Find and Replace option. I'm pretty sure Visual Studio Code has something similar to that.

You can use the Shift +Alt shortcut for windows and for Mac use Shift + Option
First press Alt + Shift/Shift + Option then click the mouse button on the first line.
This will mark all the parts of one side. Whatever you type will be reflected in the marked spaces.

Place Cursor where you want to insert/delete text.
Goto Selection Menu and choose Column Selection Mode
Scroll to the bottom of the data and shift + click in the last line where you placed the first cursor.
Perform action (add/delete whatevs)
Repeat for whatever other areas you want to change.
v: 1.74.3

1- You can use the Ctrl + H shortcut (menu Edit → Replace)
Enter abcde in Find Control.
Enter 'abcde' in Replace Control.
Then press Ctrl + Alt + Enter.

Related

Is there a function or a method to add " , " after every row in VSC?

I have a .json file which is about 600+ lines and it looks like this:
{data0..}
{data1..}
{data2..}
To use it i need to add [ before and ] after to make it an array (no problem)
but i need to add " , " after every row. Is there a function/method to do that faster than manual typing?
Every object contain data in it.
Or anything to make it readble?
Alternatively, and simpler,
Ctrl-A to select all.
Shift-All-I to put a cursor at each line end.
Type your ,.
Press Ctrl-H for the Search-Replace window
In the first line type \n which means new line and make sure the regular expressions button is pushed - it's 3rd in the row and looks like this: .*
In the second line type ,\n - to replace new line with a comma followed by a new line
Push the "replace all" button
Notice: it assumes every JSON object occupies strictly single line

Select multiple lines with cursors at each line start

I want to select multiple lines and put a cursor at the beginning of each line. Sublime Text can do this with Ctrl-Shift-L
select multiple lines
ctrl + shift + L and then put cursor at beginning of each line
Press Crtl + Shift + Alt + Arrow up/down to select multiple lines in Visual Studio Code. Note that the selected lines will be in one column (if possible).
You can also mark some lines and then do this combination and you have all selected lines included.
Moreover you can press and hold Alt and click the lines you need. This way you can select multiple lines that are not neighbours or in the same column.
To do exactly what Ctrl-Shift-L does in Sublime Text, you must do:
On Windows:
Select the lines.
Alt-Shift-I (will add multiple cursors)
Shift-Home (will go at the beginning of each line and be selected)
On Mac :
Select the lines.
alt-shift-I (will add multiple cursors)
cmd-shift-←
(will go at the beginning of each line and be selected)
More information in this answer.
Put cursor at beginning of first line
Ctrl-Shift-Alt-Arrow down/up will put a cursor at the beginning of the following/preceding lines
Ctrl-I will select those lines with the cursor at the beginning of each line.
NOTE : On my vscode the cursors look like they might be shifted down one line but they actually are not - the are in the right place. If you start typing, it works but you have to hit Enter when you are done to get back separate lines. It is a little quirky but works as you would expect.
EDIT (using a hint from #Maxime's answer)
Select your test first.
Alt-Shift-I : puts cursors at the end of each of those lines but text unselected (I as in island not a lowercase L)
Function-Home : put cursors at beginning of each line.
Ctrl-I : selects all lines.
Important: read the NOTE above.
--------------------- v1.43 see How to put the cursor at the end of all selected lines in Visual Studio Code? with column selection mode it is easy to put the cursor at the beginning or end of lines selected by dragging.
You can hold alt and click the places you need with the mouse. This way you can select rows that aren't related, like row 10,15,18. Also you can select at different places in the same row.
I hope this helps someone, but there is a setting in VSCode called Editor: Multi Cursor Modifier which may do what OP is asking:

How to select every N line in vscode?

How can I select every N lines in visual studio code.
I can't find a proper regualr expression can let me do this.
Press Ctrl+F or command+F.
If not already enabled, press Alt+R or option+command+R to toggle RegEx searching (or press the .* button).
Enter (.*\n){N} into the search field, replacing N with the number of lines to select (such as (.*\n){2} for every second line).
Press Alt+Enter or option+return or Select All Occurrences of find Match from the command palette to select every Nth grouped lines.
Press ← to place the cursor at the beginning of every Nth line, or → then ← to place the cursor at the end of every Nth line.
Ctrl+H
Check the regex icon .*
Search: (^.*?$[\n]){9}
That RegExp will find [ed. but not select] 9 lines of code at a time - empty lines do count as a line.
What are you going to replace them with?
If you want to replace every nth line, like every 9th line with some new text, try this regex:
((.*\n){8})(.*\n)
and replace with $1[new line 9 stuff here]
Select Multi lines in VsCode
Visual code natively supports this functionality.
But you have to select the lines manually.
Hold the alt button and click where you want to select the data
You can also select multiple lines
For more details:Visual Studio Code Documentation

How do you select a range of lines in Visual Studio Code?

How do you select a range of lines (from a start line number to an end line number) in Visual Studio Code?
I had to delete lines starting from line number 17158 through 1644546 which was nearly impossible to do if I used scrolling. So I followed 4 simple steps:
Go the line where you want to start deleting (17158 in my case).
Using keyboard, press Ctrl+G which opens a box to enter the line number to go to.
Enter the line number in the box (1644546 in my case). VS Code will take you to the line you wanted to go to.
Hold the shift key and click on the line. Hopefully, this should select all the lines that you wanted to delete.
Since version 1.46 it seems like you can set an anchor and select from anchor to cursor: https://code.visualstudio.com/updates/v1_46#_accessibility
First set a selection anchor (default binding Ctrl+K Ctrl+B)
Then go to the line that you want, either by mouse or by Go To Line command
Then Select From Anchor to Cursor (default binding Ctrl+K Ctrl+K)
To cancel selection hit esc.
Click at the first column of the first line. Then scroll down to the last line that you want to select, HOLD Shift key and click on the last line.
Try this out...
Type #region at the start of the line which you want to select and #endregion at the end of the line and collapse it using the side ruler available in VS code .Now since it will in a single region you can select the region easily
Hi y'all I found a really fun way to do it. Ctrl-= will select to the last go back.

How to search and replace 2 lines (together) in Eclipse?

I would like to search multiple files via eclipse for the following 2 lines:
#Length(max = L_255)
private String description;
and replace them with these two:
#Length(max = L_255, message="{validator.description.len}")
private String description;
Another tip on how to get the regex for a selected block.
Open one of the files that contains the multiple lines (multiline) to search or replace.
Click Ctrl+F and select "Regular expression". Close the Find/Replace window.
Select the block you need and click again Ctrl+F to open the Find/Replace window.
Now in the Find text box you have the regular expression that exactly matches your selection block.
(I discovered this, only after creating manually a regexp for very long block :)
Search are multi-line by default in Eclipse when you are using regex:
(\#Length\(max = L_255)\)([\r\n\s]+private)
I would like to add "private String description;"
(\#Length\(max = L_255)\)([\r\n\s]+private\s+?String\s+description\s*?;)
replaced by:
\1, message="{validator.description.len}")\2
It works perfectly in a File Search triggered by a CTRL-H.
As mentioned in Tika's answer, you can directly copy the two lines selected in the "Containing Text" field: those lines will be converted as a regexp for you by Eclipse.
CTRL+H does take two lines if you use regexp (and you don't have to write the regexp by yourself, eclipse does that for you).
Select your lines.
Click CTRL+H. The search dialog opens up.
If "Regular expression" is already checked, eclipse will have converted the two lines you search for into regexp for you, click Search.
If "Regular expression" if not already checked", check it and click Cancel (eclipse remembers your choice).
Select your lines again.
Click CTRL+H. The search dialog opens up. This time "Regular expression" is already selected. eclipse will have converted the two lines you search for into regexp for you, click Search.
A quick tip for including multiple lines as part of a manually constructed regular expression:
Where you would normally use .* to match any character zero or more times, instead consider using something like (?:.|\r?\n)*. Or put an extra ? at the end to make it non-greedy.
Explanation: . doesn't match new lines so need to do an "either-or": The parentheses match either the . before the pipe or the new line after it. The ? after \r makes the carriage return before the line feed optional to allow Windows or Unix new lines. The ?: excludes the whole thing as a capturing group (which helps to avoid a stack overflow).
Click Ctrl + F and select "Regular Expression" and then search the lines. In case to perform the same on multiple files, click Ctrl + H, click on 'File Search' and perform the same.
Select the folder that contains all your files and press Ctrl+H.