feihtthief
feihtthief

Reputation: 7103

How do I get a cursor on every line in VS Code?

I'm trying to use the multi-cursor functionality of Visual Studio Code on a large(ish) file.
The file is too large to select every line individually with ctrl alt up or ctrl alt down.
In Sublime Text, I would select everything and press ctrl shift L.
Is there a similar thing in VS Code? I've tried using a regex search for ^ (caret), but that gives me an error stating "Expression matches everything".

Upvotes: 253

Views: 245992

Answers (8)

ZachB
ZachB

Reputation: 15416

The command Selection / Add Cursors to Line Ends Alt+Shift+i will put a cursor on every line in the current selection. (For mac use Opt+Shift+i)

Tip: You can pull up the keyboard shortcut reference sheet with Ctrl+K,Ctrl+S (as in, those two keyboard combos in sequence). (For mac use Cmd+K,Cmd+S)

Upvotes: 439

Ahmad Ismail
Ahmad Ismail

Reputation: 13982

Real Lines vs Display Lines

First we have to understand the difference between Real Lines and Display Lines to completely understand the answer of the question.

When Word Wrap is enabled, each line of text that exceeds the width of the window will display as wrapped. As a result, a single line in the file may be represented by multiple lines on the display.

The easiest way to tell the difference between Real Lines and Display Lines is by looking at the line number in the left margin of the text editor. Lines that begin with a number correspond to the real lines, which may span one or more display lines. Each time a line is wrapped to fit inside the window, it begins without a line number.

Cursor At the Beginning of each Display Lines:

display lines

Cursor At the Beginning of each Real Lines:

enter image description here

Answer to the Question

Now that we know the difference between Display Lines and Real Lines, we can now properly answer the actual question.

Hold AltShift and select the text block.

Press Home to put cursor on the beginning of every Display Line.

Press End to put cursor on the end of every Display Line.

Press HomeHome (Home twice) to put cursor on the beginning of every Real Line.

Press EndEnd (End twice) to put cursor on the end of every Real Line.

Please understand that AltShiftI put cursor at the end of every Real Line.

Special Case:

If there are tabs at the beginning of each line, then to put cursor at the beginning of every Real Line we have to use: EndEndHomeHome

If there are empty lines between Lines then before using the commands, we can use: Remove empty lines: in Selection which comes with the Remove empty lines extension.

The solution mentioned here for this particular scenario is somewhat tricky and non-deterministic. So, i use select-by extension to address the issue. The keybindings i use are:

{
    "key": "ctrl+alt+left",
    "command": "moveby.calculation",
    "when": "editorHasMultipleSelections",
    "args": {
        "charNrEx": "0"
    }
},
{
    "key": "ctrl+alt+right",
    "command": "moveby.calculation",
    "when": "editorHasMultipleSelections",
    "args": {
        "charNrEx": "currentLine.length"
    }
},

Upvotes: 10

How do I get a cursor on every line in VS Code?

A. For small snippets

When the snippet is small, press Ctrl Alt 🠅 or Ctrl Alt 🠇, as suggested in your question.
If you accidentally mark one or two lines too many – press Ctrl U to unmark the last line.
(Release Alt before pressing U.)

B. For large snippets

  1. Use Shift Page up or Shift Page down – and Shift 🠅 or Shift 🠇 – to select the snippet.

  2. Press Alt Shift i, then Home Home. (Yes, that's Home twice.)

  3. Type whatever it is you want your lines to start with. (For example, > .)

Reference

Upvotes: 3

  • Hold Alt+Shift+i

  • Hold Home (fn+-> Mac) for right-most or End for left most(fn+<- Mac)

enter image description here

Upvotes: 69

Borys Lebeda
Borys Lebeda

Reputation: 499

Hold Alt+Shift and select the block. Then press End or Right button. You get selected individual lines.

I use version VSCode 1.5.3 in Windows.

Upvotes: 48

Bubba
Bubba

Reputation: 11

Install the extension Sublime Commands.

[Sublime Commands] Adds commands from Sublime Text to VS Code: Transpose, Expand Selection to Line, Split into Lines, Join Lines.

(Don't forget to add the keybinding(s) from the extensions details page to your keybindings.json)


Doesn't VS Code already have a "split into lines" command?

Yes, yes it does. However it differs from the one in Sublime.

In VS Code, when you split into lines your selection gets deselected and a cursor appears at the end of each line that was selected (except for the last line where the cursor appears at the end of the selection).

In Sublime, when you split into lines a cursor appears at the end of each line (with the same exception as in VS Code) and the selection is divided on each line and "given" to the same line.

Upvotes: 1

nikhilweee
nikhilweee

Reputation: 4579

This feature is actually called split selection into lines in many editors.

  • Sublime Text uses the default keybinding, CTRLSHIFT L
  • VSCode uses ALTSHIFTI
  • For Atom you actually need to edit your keymap to something like this
'.platform-win32 .editor, .platform-linux .editor':
  'ctrl-shift-L': 'editor:split-selections-into-lines' 

Upvotes: 13

seg-s
seg-s

Reputation: 55

I have the same problem, i'm used to Alt + drag to do 'box selections' in visual studio but it does'n work in code.

It seems to be impossible for now to do it differently than by selecting every single line.

However plugins should be supported soon so we will likely see a plugin for this if not implemented directly by microsoft.

From visual studio uservoice forums:

We plan to offer plugin support for Visual Studio Code. Thank you for your interests and look for more details in our blog in the coming weeks. http://blogs.msdn.com/b/vscode.

For the preview we are looking for exactly this type of feedback. Keep it coming.

Sean McBreen – VS Code Team Member

Upvotes: 0

Related Questions