187

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

4
  • 74
    CTRL+K and after CTRL+D?
    – RBA
    Jan 25, 2012 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, 2012 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, 2012 at 0:28
  • 4
    just by removing and re-adding the last curly brace, VS2010 will auto-indent the code block for you
    – Simon
    Jan 26, 2012 at 5:21

9 Answers 9

296

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.

5
  • 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. Jan 26, 2012 at 1: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. Jan 26, 2012 at 9: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, 2012 at 7:57
  • tab & shift+tab works for indenting Python code in Visual Code Code 1.11.
    – Julia Zhao
    May 4, 2017 at 1:05
  • 1
    In Visual Studio 2015 and onwards theres Edit > Format Selection (Ctrl + K, Ctrl + F) which formats the current selection.
    – brandito
    Apr 6, 2018 at 6:00
140

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

7
  • 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, 2012 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, 2012 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. :( Jan 26, 2012 at 0:07
  • @RBA - or built in the latest versions (as well as the [TAB] handling)
    – Gerry Coll
    Jan 26, 2012 at 0: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, 2012 at 1:00
46

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

5
  • 3
    Ctrl-D (format document) is a better shortcut as it does the same thing ( Ctrl-A selects the whole document, then K + F formats the selection ) Jun 6, 2012 at 9:16
  • 5
    @Raghurocks: What James meant was Ctrl+K+D, presumably, which formats the entire document. May 2, 2013 at 21:01
  • Thank you. Although a is optional. Mar 2, 2015 at 16:36
  • 1
    CTRL K + F to format the selected code CTRL A + K + F, means CTRL A select all, then format the selected code, means all
    – Ali Adravi
    Jul 6, 2015 at 20:38
  • [Ctrl]+[K],[Ctrl]+[D] is what does it for me. The way I remember is because Kevin Durant is a great basketball player. I always say "Kevin Durant will fix the formatting for me."
    – Jamie
    Apr 13, 2021 at 22:38
29

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

17

You can just use Tab and Shift+Tab

17

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.

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.

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"

0

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.