198

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".

Henke
  • 4,445
  • 3
  • 31
  • 44
feihtthief
  • 6,403
  • 6
  • 30
  • 29

8 Answers8

341

The command Selection / Add Cursors to Line Ends altshifti will put a cursor on every line in the current selection. (For mac use optshifti)

Tip: You can pull up the keyboard shortcut reference sheet with ctrlk,ctrls (as in, those two keyboard combos in sequence). (For mac use cmdk,cmds)

jasonleonhard
  • 12,047
  • 89
  • 66
ZachB
  • 13,051
  • 4
  • 61
  • 89
  • 4
    When I tried it, the cursor went to the end of each line. I was looking for beginning of each line. I saw an extension that will allow a keybinding for "cursor at beginning of line," but I was wondering if it's possible to do it in vanilla VS Code? – Tiffany Mar 19 '18 at 15:53
  • 32
    @Tiffany hit the `home` key after. – ZachB Mar 19 '18 at 19:10
  • 5
    That should have been obvious for me. But thank you nonetheless! – Tiffany Mar 19 '18 at 19:34
  • 1
    Just for reference, to draw your cursors without first making a selection use alt + shift + drag selection. Those familiar with Sublime Text know this by using alt + drag selection. – Pouria Almassi Sep 24 '18 at 04:01
  • 8
    `home` for Mac is `fn` + `left` – eibersji Jan 23 '20 at 02:34
  • 4
    to add on to what @eibersji said. For Mac Users - To get to start of multi selected line. ```shift + option + i ``` then press ```fn + left arrow ``` twice , and staying on same selection if you want to go to end of each line from start type ```fn + right arrow ``` twice – anon-sAI Feb 14 '21 at 09:10
  • for Mac users: before `opt` `shift` `I` you must select your text ,for example `cmd` and `a` for affect all. – moraei Apr 24 '21 at 10:21
  • for Windows too. You need to "select all" first, with `Crtl + A` – Alex from Jitbit Oct 13 '21 at 14:32
  • Notice that VS Code limits to 9999 cursors at most – Amit Beckenstein Jul 10 '23 at 12:23
41

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.

guntbert
  • 536
  • 6
  • 19
Borys Lebeda
  • 429
  • 4
  • 6
  • 6
    This is a nice work around, but one of the few things I miss from sublime text, the old `ctrl + shift + L` to add cursors to all selected lines – Des Horsley Feb 20 '17 at 23:49
  • 19
    @DesHorsley I think you're looking for `alt+shift+I`. – ZachB Mar 20 '17 at 22:01
  • 4
    @ZachB you legend. Thanks! – Des Horsley Mar 20 '17 at 22:52
  • @ZachB. Please make than an answer so that I can accept it. – feihtthief Sep 15 '17 at 11:15
  • If you've changed your multi cursor key to `ctrlcmd` instead of `alt` then the shortcut for this is `ctrl + shift + drag` or `cmd + shift + drag`. – pants Feb 12 '19 at 19:05
  • `ALT+SHIFT+I` is the answer for vscode users. However there is multiple cursor limitation that impedes you to edit +10000 lines. Currently being requested as a feat improvement here: https://github.com/microsoft/vscode/issues/81589 – basquiatraphaeu Apr 07 '22 at 19:01
40
  • Hold Alt+Shift+i

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

enter image description here

  • 1
    This answer saves me a lot of time. BTW, how do you record this gif? – Li Jinyao Aug 12 '20 at 03:06
  • 1
    On mac I was using `gifox` for such gifs. – Soner from The Ottoman Empire Aug 12 '20 at 04:23
  • 1
    Saved a ton of time. – M A Salman Oct 29 '20 at 04:48
  • As the animated GIF above shows, if your lines have tabs or spaces at the front, then pressing the Home key will put the cursor *after* the tab. If you need your cursor to go absolutely to the front of the line, *before* the tab, then hit the Right Arrow key instead of the Home key. Note: when making your text selection, you'll need to include the line before your first line, otherwise your first line gets skipped out this way. – ChillyPenguin Nov 25 '22 at 00:33
11

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' 
nikhilweee
  • 3,953
  • 1
  • 18
  • 13
8

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.

Ahmad Ismail
  • 11,636
  • 6
  • 52
  • 87
1

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.

Community
  • 1
  • 1
Bubba
  • 11
  • 4
  • Note that the OP is asking how to perform the task on vscode as they already know how it's done in sublime – alamoot Jun 02 '18 at 19:30
1

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

Henke
  • 4,445
  • 3
  • 31
  • 44
0

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

Community
  • 1
  • 1
seg-s
  • 55
  • 3