Cursor jumps to the first column after saving file when on a blank line - visual-studio-code

In vscode when the cursor is on a blank line and I save the document the cursor jumps to the first column of the line. How can I prevent this?

So the line really isn't blank if the cursor isn't already in the first column. The whitespace you have on that line is being removed when you do a save. To prevent all the whitespace on a line from being removed, disable this setting:
Files : Trim Trailing Whitespace

Related

How to remove the empty lines in an html file

In the image below there are empty lines before the head, body and closing /html tags and within the body tag I want to remove. How?
put cursor at end of line with text
press Shift-ArrowRight as many times till cursor at pos 1 selecting all newlines till next text
Ctrl+Shift+L to select all similar parts
ArrowRight
Backspace as much times as needed
Esc to leave multi cursor
According to your screenshot, you can try to reduce the line height to reduce the gaps between two successive lines.
Open Settings
Type Line height in the search bar
Specify your desired line height
And done.
Also you could try reducing your font size which would automatically decrease the line height thus reducing gaps in between successive lines.
Use \n\n* in the Find Widget (Ctrl+H) with the regex option (.*) icon enabled, replace with \n. Replace All.
or better change this setting:
// List of tags, comma separated, that should have an extra newline before them. `null` defaults to `"head, body, /html"`.
`HTML > Format: Extra Liners`
In your settings.json:
"html.format.extraLiners": "" // just leave empty
Now when you format the html file those empty lines above head/body/\html should be removed (select all and Command Palette: Format Selection).

In VSCode, how can I turn a multi-line comment into a paragraph with no line breaks?

What's an easy way to convert a multi-line comment (e.g. JSDoc with each line separated by line breaks) into a paragraph without any line breaks that I can copy into an email or another document?
I know I can use search & replace with regular expressions, but is there a more ergonomic way to do it?
You probably knew that you can use multiple cursors to change multiple lines at once, but did you know you can also use them to remove line breaks? Assume you start with this comment:
/**
* Returns a new `Temporal.LocalDateTime` instance representing the first
* valid time during the current calendar day and time zone of `this`.
*
* The local time of the result is almost always `00:00`, but in rare cases it
* could be a later time e.g. if DST starts at midnight in a time zone. For
* example:
* ```
* const ldt = Temporal.LocalDateTime.from('2015-10-18T12:00-02:00[America/Sao_Paulo]');
* ldt.startOfDay; // => 2015-10-18T01:00-02:00[America/Sao_Paulo]
* ```
*/
First part: use multiple cursors to remove the prefix characters on each line.
Click on the upper-left corner of the comment (the /**).
Now hold down Cmd+Shift (Alt+Shift on PC) and click after the */ on the last line of the comment section.
This will create a columnar, multi-line selection that includes the non-text prefix characters on each line. If the selection doesn't include all the prefix characters, you can hold down the Shift key and use the left or right arrow keys to adjust the width of the selection.
Press the Delete key to remove prefix characters on all lines.
Second part: it's time to delete the line breaks and replace them with spaces. I discovered today that you can use multiple cursors for this part too!
After you've deleted the prefix text above, but before you've pressed any other keys, press the backspace key. It will delete the line breaks but leave each cursor in the same place!
Type the spacebar once to insert one space to replace each line break.
Press ESC to clear multiple selections, and delete the extra space at the start of the line. You may have an extra space(s) at the end of the line too that may need trimming.
Copy the resulting one-line text.
Use Cmd+Z (Ctrl+Z on Windows) to undo the last few changes so your code comment will be back to normal.
Now you can paste the copied text into an email!
The same solution works to replace line breaks with spaces in any multi-line text, not only code comments.
I'm sure that many of you already knew how to do this trick, but I found it so easy and so cool that I thought it was worth sharing as a Q&A here so others can learn about this trick too.
Here's what the steps look like in the VSCode IDE:
Before deleting, you should see something like this:
After deleting prefix characters:
After deleting line breaks (note the multiple cursors are still there):
After inserting spaces in place of the deleted line breaks:
I usually select the first line break, then hit/hold command+D repeatedly to add cursors at all line endings I want to edit. Then, just hit space once.

move cursor after final Line?

Once in a time I remember that in VSCode I could move my cursor after the final line. That is, if a file doesn't end with a trailing newline and I click the space after the last line, the cursor would be placed after the final line where there is no line number at the left. However, now in VSCode 1.37.1 it not longer behave like this. When I click the space, it just put the cursor at the end of the last line. How can I restore the old behaviour? I searched the settings but I can't find relevant option.
Try:
"editor.renderFinalNewline": false
I think that is what you want - if there is a newline at the end of the final line you can click in the line below (which has no line number) and enter text there.

How do I prevent Visual Studio Code from automatically deleting whitespace in blank lines

When I move the cursor from a line without typing anything on it, VS Code seems to automatically delete the whitespace from that line so that, when I put the cursor back on that line, it ends up flush to the left, forcing me to tab to the desired indentation level again. What setting do I change to make it not do that? I tried Googling this problem but all the answers were about removing blank lines with regular expressions, which is not my issue at all.
/* Automatically remove indentation in empty lines on save */
"emptyIndent.removeIndent": true,

Regex to delete all empty lines whatsoever?

In Windows 10 I use a text editor (I'd like not to point out a particular one but I usually use Visual Studio Code AKA "VSCODE").
I need a way to delete all blank lines whatsoever only with regex after I matched them with this code:
^\s*$
After I match the lines themselves, how is it possible to delete the lines?
AFAIK, regex only edit lines, not deleting lines or adding lines.
I desire a way to delete all matched (empty).
Hitting "Enter" or "Delete" in the empty "replace" box doesn't delete lines:
You were close. You were missing the new line character in your regex. This worked for me:
^\s*$\n
Without the newline character, you are matching the blank line itself which you then replace with nothing but you've left the newline character so it still leaves an empty line in place.