189

I'm using Visual Studio 2010 and C#. How can I indent the selected text to left/right by using shortcuts?

enter image description here

In the Delphi IDE the equivalents are Ctrl+Shift+I and Ctrl+Shift+U

starball
  • 20,030
  • 7
  • 43
  • 238
RBA
  • 12,337
  • 16
  • 79
  • 126
  • 75
    CTRL+K and after CTRL+D? – RBA Jan 25 '12 at 23:40
  • I got 5 good answers in the same time...which one to accept? +1 for all the answers. thank you – RBA Jan 25 '12 at 23:43
  • 3
    Note that the newer versions of Delphi also use [tab] and [shift][tab] keys, but still support the old indent/unindent block commands – Gerry Coll Jan 26 '12 at 00:28
  • 4
    just by removing and re-adding the last curly brace, VS2010 will auto-indent the code block for you – Simon Jan 26 '12 at 05:21

9 Answers9

298

Tab and Shift+Tab will do that.

Another cool trick is holding down ALT when you select text, it will allow you to make a square selection. Starting with VS2010, you can start typing and it will replace the contents of your square selection with what you type. Absolutely awesome for changing a bunch of lines at once.

Mike Christensen
  • 88,082
  • 50
  • 208
  • 326
  • Not quite clear what you mean about the VS2010 feature, but that kind of sounds like SyncEdit, which Delphi has had for years and years now. – Mason Wheeler Jan 26 '12 at 01:21
  • 2
    @Mason It's nowhere near as powerful and SyncEdit. It allows you to make a rectangular selection. So if the selection covers multiple lines it does not need to cover multiple columns. Then when you type you replace each line with what you type. So your variables have to be aligned vertically for it to be any use. This is one area where RAD Studio wins hands down. – David Heffernan Jan 26 '12 at 09:17
  • FYI, Tab and Shift+Tab also work under Delphi XE2, (as does the ALT 'trick') although in earlier versions it will just replace your selection with a tab character. – Peter Jan 27 '12 at 07:57
  • tab & shift+tab works for indenting Python code in Visual Code Code 1.11. – Julia Zhao May 04 '17 at 01:05
  • 1
    In Visual Studio 2015 and onwards theres Edit > Format Selection (Ctrl + K, Ctrl + F) which formats the current selection. – brandito Apr 06 '18 at 06:00
142

Ctrl-K, Ctrl-D

Will just prettify the entire document. Saves a lot of messing about, compared to delphi.

Make sure to remove all indents by first selecting everything with Ctrl+A then press Shift+Tab repeatedly until everything is aligned to the left. After you do that Ctrl+K, Ctrl+D will work the way you want them to.

You could also do the same but only to a selection of code by highlighting the block of code you want to realign, aligning it to the left side (Shift+Tab) and then after making sure you've selected the code you want to realign press Ctrl+K, Ctrl+F or just right click the highlighted code and select "Format Selection".

MoKai
  • 3
  • 2
Tony Hopkinson
  • 20,172
  • 3
  • 31
  • 39
  • 7
    Ctrl-K-D does format the entire document. There is another shortcut for formatting the selected text (Ctrl-K-F maybe?). – Dercsár Jan 25 '12 at 23:42
  • there are also in Delphi free 3d party tools which can do that and can be integrated in the IDE. – RBA Jan 25 '12 at 23:47
  • Aye looked at them. Would change every file in 15 years of source code, not to mention a big fight over how the code should be indented. BeyondCompare would have a fit. :( – Tony Hopkinson Jan 26 '12 at 00:07
  • @RBA - or built in the latest versions (as well as the [TAB] handling) – Gerry Coll Jan 26 '12 at 00:24
  • 1
    @Tony, Delphi now has a built-in code formatter (for a few versions now) that you can customize to suit almost any style. There have been free, open source IDE add-ins for years that also can be customized to work with almost any formatting you want. The "compared to Delphi" portion of your answer is therefore wrong. Not downvotin' - just sayin'. :) – Ken White Jan 26 '12 at 01:00
  • Stuck on Delphi 2005. :( Don't ask!. Besides even if I had one, 15 years, 5 companies, 50+ developers and 3189 management coding standards later. Not happening.. – Tony Hopkinson Jan 26 '12 at 15:01
  • Please remove the ctrl-k+ctrl-d shortcut... the question is about a selection of text not the entire document. I just prettified my entire source file and it took 45 minutes of hunk discarding in sourcetree to remove the unwanted changes from the file for commit. – riverofwind Jan 22 '23 at 19:52
46

If you would like nicely auto-formatted code. Try CTRL + A + K + F. While holding down CTRL hit a, then k, then f.

Hulk1991
  • 3,079
  • 13
  • 31
  • 46
Mausimo
  • 8,018
  • 12
  • 52
  • 70
30

Tab to tab right, shift-tab to tab left.

Giovanni Galbo
  • 12,963
  • 13
  • 59
  • 78
18

Visual studio’s smart indenting does automatically indenting, but we can select a block or all the code for indentation.

  1. Select all the code: Ctrl+a

  2. Use either of the two ways to indentation the code:

    • Shift+Tab,

    • Ctrl+k+f.

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
17

You can just use Tab and Shift+Tab

Mike Christensen
  • 88,082
  • 50
  • 208
  • 326
Bassam Mehanni
  • 14,796
  • 2
  • 33
  • 41
7

Just hit Tab to push it over or on the menu bar Edit --> Advanced --> Format Selection and that will auto indent, the keyboard shortcut is also shown in the menu.

Schreinbo
  • 235
  • 1
  • 5
0

If the move-left and move-right shortcuts do not appear on your screen, click at the rightmost position of your toolbar at the top. You should get "Add or Remove Buttons." Add the buttons "decrease line indent" and "increase line indent"

user3029478
  • 179
  • 1
  • 2
0

Firstly select the text you want to edit(indent), then press Tab to see the snippet move towards the right.

Preet Shah
  • 26
  • 3