Indent code in Visual Studio Code

Asked

Viewed 50,339 times

19

How to create a shortcut to make spaces equal between assignments?

Ex:

$nome = "Maria Ferreira";
$telefone = "99645876";

To

$nome     = "Maria Ferreira";
$telefone = "99645876";

6 answers

24


Natively I don’t know shortcut that does this.

But I use the extension Better Align that does just that and you can configure on the keyboard the shortcut you want. https://marketplace.visualstudio.com/items?itemName=wwm.better-align

Goes into File > Preferences > Keyboard Shortcuts And create your shortcut like this for example:

{ "key": "ctrl+alt+i",  "command": "wwm.aligncode",
                           "when": "editorTextFocus && !editorReadonly" }

inserir a descrição da imagem aqui

In the link above you have the complete documentation.

  • Thank you, that’s right.

  • @Nice Herick you solved there, tmj! If you want a native shortcut that only makes a simple identation of the tags try (Ctrl+K Ctrl+F), but it only idents the tags, does not align and generates the spaces as in the ok extension.

  • only regarding curiosity is indicated identar this way or by some is considered bad formatting of the code ?

  • 2

    @luizricardo6n as far as I know it’s a matter of taste anyway. Some people find the code more "readable" when indented this way, but not that this is the rule, it may be that in your team may be a "good practice", but in the team of another company may not be used... In the Google Guide itself for example they do not indent CSS or HTML in this way... https://google.github.io/styleguide/htmlcssguide.html

  • 1

    Okay, thanks for clarifying.

12

To indent the code, use Alt+Shift+F.

  • This is the default command!

3

0

Just to add up-to-date information:
To update the part: editorTextFocus && !editorReadonly, you must click with the right-click on the line and choose the option: Change When Expression or Ctrl+K Ctrl+E with the selected line.

inserir a descrição da imagem aqui

You can also open the file keybindings.json, in the Palette of commands (Ctrl+Shift+P), search for Preferences: Open Keyboard Shortcuts (JSON), but it’s read-only.

REFERENCE:

  1. https://code.visualstudio.com/docs/getstarted/keybindings#_Keyboard-Rules
  • Dude, I don’t understand what you mean by that? How does it work? It’s no use here....

-1

Visual studio 2017 (Windows) ==> Ctrl + K + D to automatically ident

  • The question was asked regarding Visual Studio Code and not Visual Studio

-2

Commands may change depending on the version of vscode; logo, to know the command, access File > Preferences > Keyboard Shortcuts and consult. If this does not work, try indenting with this command: Ctrl + [

Browser other questions tagged

You are not signed in. Login or sign up in order to post.