How to avoid multi cursor on wrapped lines in VS Code?

Viewed 753

I have turned on Word Wrap in VSCode, so that long lines are wrapped at certain window widths.

If I want to comment out some code or text, I usually just move the cursor to the beginning of the first line of the block of code, use ctrl+shift+down to add cursors, and type the // or # etc..

The problem with Word Wrap is that I not only get cursors in the beginning of each actual line, but in the beginning of every line as it is displayed at the moment in the editor.

For example, for the following document:

1 This is the first line.
2 Second line.

It might be wrapped like this:

1 This is the first 
  line.
2 Second line.

So if I use the method above and add % I would get:

1 %This is the first 
  %line.
2 %Second line.

But what I want is the following:

1 %This is the first 
  line.
2 %Second line.

Otherwise, I have a % in the middle of the line, just because I resized the editor window.

At the moment, I actually turn off Word Wrap to achieve this, but I hope there is a better way?

3 Answers

Follow the next steps:

  1. Alt+Shift+i to put a cursor on every selected line
  2. Cmd/Ctrl+← to move all cursors to the beginning of the [wrapped] lines
  3. Cmd/Ctrl+← (again) to move all cursors to the actual beginning of the [wrapped] lines

Enjoy!

Update v1.62+ (October 2021)

This is now natively available in VSCode thanks to this PR on changing the default behavior of InsertCursorAbove/Below. However, it's not enabled by default.

You'll need to update your keybindings to pass in an args like this:

{
  "key": "shift+alt+down",
  "command": "editor.action.insertCursorBelow",
  "when": "textInputFocus",
  "args": { "logicalLine": true },
},
{
  "key": "shift+alt+up",
  "command": "editor.action.insertCursorAbove",
  "when": "textInputFocus",
  "args": { "logicalLine": true },
},
Related