AfBu
AfBu

Reputation: 10339

Multiline editing in Visual Studio Code

Is it possible to enable multiline editing like in Sublime Text?

For example, press Ctrl to place additional cursor carets and being able to write/delete on multiple places in the document at one time.

Upvotes: 1022

Views: 1126351

Answers (30)

Abhijith S
Abhijith S

Reputation: 542

For the latest Mac Users in VS code:

option + Command + up/down for multiselect.

Ctrl + E for going to end of the line, Ctrl + shift + E for going to end of the line with selection.

Upvotes: 0

ishan
ishan

Reputation: 87

I am using the latest version of VS code i.e., 1.46.1 (May 2020) in Windows 10. Just press Alt+mouse left click on the lines you want to select. This will let you select multiple lines at once and let you edit them. Also Press Esc to exit from it. This is really helpful if you're coming from sublime text.

Update 1 - above mentioned solution is still valid with Windows11 (April 2024).

Suppose you're on a certain line number and want to select lines below and above, to do this simply press Ctrl + Alt + Up or Down arrow.

If you want to select either above lines or below lines then with above key combination use Shift also.

Upvotes: 7

Alex
Alex

Reputation: 67819

From the version 1.13 (May 2017) you can finally change the default modifier key for creating multiple cursors (add to settings):

"editor.multiCursorModifier": "ctrlCmd"

P.S.: The modifier "follow link" from this moment will be Alt.

Upvotes: 30

Maxime
Maxime

Reputation: 8979

I wanted to select multiple lines and hit "something" to have a cursor for each select lines (similar to Ctrl + Shift + L in Sublime Text). This action in Visual Studio Code is called "Add Cursors to Line Ends".

This was tested in Visual Studio Code 1.77.0 (April 2023) and works on both Windows and Mac.

Here is the way:

  1. Select the lines you want to have multiple cursors.
  2. Simply hit Alt + Shift-I.

You now have one cursor per selected line.

Upvotes: 80

Akif
Akif

Reputation: 6836

Simple Answer:

  1. Install Multi Line tricks extension for Visual Studio Code
  2. Select the lines and press Alt+Shift+L (Windows/Linux) or Option+Shift+L (Mac)

Upvotes: 1

user8202629
user8202629

Reputation:

Solved using just two steps!

  1. Ctrl + F

  2. Alt + Enter

It's equal to Sublime Text's Alt + F3.


Note: For macOS, + F followed by + Enter is equivalent to Sublime Text's + + G.

Enter image description here

Upvotes: 262

ganiular
ganiular

Reputation: 639

  • To add multiple cursors at any text position This is done by using Ctrl+Click. Firstly you have to enable Multiple-Cursor Modifier. Simply type this option on Command Palette by Ctrl+Shift+P to enable it.

  • To remove a cursor form multiple text position. Do Ctrl+Click on the cursor.

Upvotes: 14

prakhar newatia
prakhar newatia

Reputation: 239

Well, I used the Alt key to select the lines where I wanted to edit, and that's it. Alternatively, we can use the Ctrl + Alt + UP/DOWN arrow if the things to be edited are above or below.

Upvotes: 5

Viraj Singh
Viraj Singh

Reputation: 2358

Step 1:

Select the word to be replaced.


Step 2:

Use Ctrl + F to select its multiple occurrences.


Step 3:

Use Alt + Enter to set cursor at all the found occurrences.


Step 4:

Just start typing the new word.


Alternatively on Mac:

⌥ Opt + Click to set cursor on the click location.

Upvotes: 26

Supun Sandaruwan
Supun Sandaruwan

Reputation: 2418

According to VS Code 1.55.2 version(2021) under User > Commonly Used section there is an option as Multi Cursor Modifier. See below image. enter image description here

for multi cursor default key is alt if you want it can change to Ctrl. Moreover, for suitable places, you can hold the Middle Mouse button and move to down.

Upvotes: 9

Nacimota
Nacimota

Reputation: 23275

On Windows, you hold Ctrl+Alt while pressing the up or down arrow keys to add cursors.

Mac: ⌥ Opt+⌘ Cmd+/

Linux: Shift+Alt+/

Note that third-party software may interfere with these shortcuts, preventing them from working as intended (particularly Intel's HD Graphics software on Windows; see comments for more details).

If you experience this issue, you can either disable the Intel/other software hotkeys, or modify the VS Code shortcuts (described below).

Press Esc to reset to a single cursor.

Multiline cursors in Visual Studio Code

Or, as Isidor Nikolic points out, you can hold Alt and left click to place cursors arbitrarily.

Arbitrarily placed multiline cursors in Visual Studio Code

You can view and edit keyboard shortcuts via:

File → Preferences → Keyboard Shortcuts

Documentation:

https://code.visualstudio.com/docs/customization/keybindings

Official VS Code Keyboard shortcut cheat sheets:

https://code.visualstudio.com/shortcuts/keyboard-shortcuts-windows.pdf
https://code.visualstudio.com/shortcuts/keyboard-shortcuts-macos.pdf
https://code.visualstudio.com/shortcuts/keyboard-shortcuts-linux.pdf

Upvotes: 1811

Mohammed Noureldin
Mohammed Noureldin

Reputation: 16936

On windows, you can create additional cursor on the next/previous lines by pressing:

Ctrl + Alt + Shift +Down/Up

Missing the Shift will flip your screen.

Creating additional cursors will allow you to edit multiple lines at once.

Upvotes: 4

Radim Šafrán
Radim Šafrán

Reputation: 608

On Windows..

  1. Select one line you want to change everywhere. (with the mouse cursor)
  2. Press CTRL + F2 and you will start editing all lines at once.

Upvotes: 1

Han
Han

Reputation: 3334

I think it depends on your Visual Studio Code version.

Mine is Linux version Visual Studio Code 1.7.2.

{ "key": "ctrl+shift+up",         "command": "editor.action.insertCursorAbove",
                                     "when": "editorTextFocus" },
{ "key": "shift+alt+up",          "command": "editor.action.insertCursorAbove",
                                     "when": "editorTextFocus" },
{ "key": "ctrl+shift+down",       "command": "editor.action.insertCursorBelow",
                                     "when": "editorTextFocus" },
{ "key": "shift+alt+down",        "command": "editor.action.insertCursorBelow",
                                     "when": "editorTextFocus" }

The point is the shortcuts are not same in all machines, so you should check your configuration. Go to menu:

Menu FilePreferencesKeyboard Shortcuts

Search for editor.action.insertCursorAbove and editor.action.insertCursorBelow and see your current configurations. You may change them if they conflict with operating system's shortcut keys.

Upvotes: 6

Ajay Mahar
Ajay Mahar

Reputation: 91

(NO MOUSE) For macOS, I found this to be very quick!

  1. CMD + f To search the (word) you want to change.
  2. Option + Enter To select all word you search for.

Just update the first word and it will update all the selected.

Upvotes: 6

anouar es-sayid
anouar es-sayid

Reputation: 365

For me Alt + Middle Click (scroll wheel) worked fine You have to click on Alt then long click on Middle Click then scroll Up or down

Upvotes: 2

kintsukuroi
kintsukuroi

Reputation: 1479

On Linux Fedora (I tried with Fedora 31) with KDE, go to:

  1. System Settings
  2. Window Management
  3. Window Behavior
  4. Window Actions
  5. Inner Window, Titlebar and Frame Actions
  6. Click Left Click Box
  7. Pick Do nothing (replacing move/drag)

Now you can select multiple lines with Alt + Left Click!

Upvotes: 0

ford04
ford04

Reputation: 74710

version 1.43 (February 2020)

You can now toggle column selection mode, which changes mouse gestures and arrow keys, via:

  • Menu Bar > Selection > Column Selection Mode
  • Ctrl+Shift+P (Show All Commands) > Toggle Column Selection Mode
  • Bind your key for command "editor.action.toggleColumnSelection"

enter image description here

Note: There is a "Column Selection" panel in the status bar after activation, which you can press to disable it again.

Upvotes: 13

RICHARD ABRAHAM
RICHARD ABRAHAM

Reputation: 2548

Working solution for me was first selecting the required text to edit and then using CTRL + F2 to select all matching data in the page. You may also use CTRL+Shift+L as suggested by @lesterCovax

Please Note: The above solution uses the inherent ability of VSCode editor to select similar text across the entire page, and therefore, be careful.

For me the above solution of Ctrl + ALT + Arrowkeys did not work as it caused the screen to change its display orientation against selecting the lines in VSCode.

enter image description here

Upvotes: 17

LesterCovax
LesterCovax

Reputation: 31

The solution from @maxime of using SHIFT + ALT + i worked for me, when I needed to quickly edit ~20k lines in a file. Most of the other keyboard shortcuts mentioned are only useful for a limited number of line. SHIFT selecting the area you want to column edit, then pressing SHIFT + ALT + i is the most efficient way to handle this.

What I accidentally discovered though, (and hasn't been mentioned here yet) is that there is a limit of 10k lines in "cursor edit mode" (there's an open Github feature request to increase it). This means that for extremely large files, it may be better to script your edits, rather than doing it manually.

For those like @specimen that had to use shortcuts like CTRL + SHIFT + L (default binding is Select all occurrences of current selection) to accomplish this, you should check that you don't have a keymap extension installed. You can check by searching for @recommended:keymaps in the extensions pane, going to File > Preferences > Keymaps (CTRL+K CTRL+M).

The default [Windows] keymap can be found in PDF form HERE, or you can go to File > Preferences > Keyboard Shortcuts to find/modify the current bindings. There is also more in-depth key bindings documentation HERE.

Upvotes: 2

Robin
Robin

Reputation: 5427

In Visual Studio Code just press Alt and place your cursor to the edit place(where you want to edit) and right click to select.

Upvotes: 6

Pulkit Agarwal
Pulkit Agarwal

Reputation: 376

In addition to all of the answers, there is one more way. Select the lines you want and then press:

  • Windows: Shift + Alt + i
  • Mac: shift + option + i

This puts a cursor in every row in the selection.

Upvotes: 9

Shan Dou
Shan Dou

Reputation: 3278

I am using the Sublime Text keymap and the keybinding provided by the top answer did not seem to work :( Could be some conflicts between Visual Studio Code and sublime keymaps.

The keybinding recommended by @Han works for me (much appreciated!):

  • Enter multiline cursor mode with Ctrl + Shift + Up/Down
  • Exit with Esc

(Sidenote) Below is a small example of using Emmet together with the multiline cursor (enabled and disabled with these key bindings listed above):

Enter image description here

Upvotes: 3

Marcelo Agimóvel
Marcelo Agimóvel

Reputation: 1719

If you're using Linux, there's a possibility of a conflict with Alt + click, which is the default for "moving a window".

You can go to menu SettingsWindow BehaviorWindow BehaviorActions tab

Just remove Alt + left (hold) and it will work.

This is the best way, because you don't need to hold two + keys to do such a simple task.

Upvotes: 4

Rui M.
Rui M.

Reputation: 41

My settings: Windows 8.1 64 bits, Visual Studio Code version 1.33.1.

Problem: Conflict with keyboard shortcuts of Intel HD Graphics 4000

I had a problem when I was using the default shortcuts of the Visual Studio Code (Ctrl + Alt + UP, Ctrl + Alt + DOWN). In my case, these commands were turning my screen in 0º and 180º respectively. It's happening, because there is the Graphic Intel application installed on my computer. Then, I just disable the shortcuts of this application.

Here is an tutorial to help someone.

Upvotes: 3

Tyler Becks
Tyler Becks

Reputation: 518

In the latest release of Visual Studio Code, you can now drag the cursor while holding Option (Alt on Windows) to select the same column on multiple rows.

To enable this, make sure you change your editor.multiCursorModifier to look like this:

"editor.multiCursorModifier": "ctrlCmd"

From the Visual Studio Code release notes 1.32.0:

In the following video, the selection begins as a regular selection and then Alt is pressed and held until the mouse button is released:

Example of improved column selection:

Upvotes: 35

Dnyneshwar
Dnyneshwar

Reputation: 748

In Windows, the below combinations work for me:

  • Ctrl + Shift + Alt + down arrow
  • Ctrl + Shift + Alt + up arrow

Upvotes: 8

Andrew
Andrew

Reputation: 310

I am using the vscodevim extension, so I'm not sure if this is a common problem. But, I was having the issue where Ctrl + Alt + UpArrow flipped my screen upside down.

Looking at the Visual Studio Code Basics (I don't know if they changed this in a recent update), it says to use:

Ctrl + Alt + Shift + (Up/down)

Upvotes: 21

xgqfrms
xgqfrms

Reputation: 12206

(Windows 10 pro x64) Here have some ways!

  1. Alt + click

  2. Alt + Ctrl + up/down

  3. Keybindings: Ctrl + click (??? it doesn't work!)

Enter image description here

Upvotes: 17

kiewic
kiewic

Reputation: 16450

On Mac it is:

Option + Command while pressing the up or down arrow keys.

Upvotes: 24

Related Questions