Align Code In Visual Studio Code



Align Code In Visual Studio Code

In this article. You can define code style settings per-project by using an EditorConfig file, or for all code you edit in Visual Studio on the text editor Options page.For C# code, you can also configure Visual Studio to apply these code style preferences using the Code Cleanup (Visual Studio 2019) and Format Document (Visual Studio 2017) commands.

This is basically a poor man's spreadsheet. It auto formats a file, aligning columns by padding with spaces. It uses commas for .csv files and vertical bars for .bsv files. You can also add other separators for other file types if you want.

Visual Studio Code lets you perform most tasks directly from the keyboard. This page lists out the default bindings (keyboard shortcuts) and describes how you can update them. Note: If you visit this page on a Mac, you will see the key bindings for the Mac. If you visit using Windows or Linux, you will see the keys for that platform. Automatically formatting code in Android Studio and IntelliJ. Install the Dart plugin (see Editor setup) to get automatic formatting of code in Android Studio and IntelliJ. To automatically format your code in the current source code window, use Cmd+Alt+L (on Mac) or Ctrl+Alt+L (on Windows and Linux).

It will find the first and last lines in the file containing the separator and format all lines between.

When active, the tab key will move to the next field and shift+tab will move to the previous field.

Alignment can also be triggered manually using Auto Align: Align Columns, Auto Align: Align Selection or Auto Align: Align Selection With Separator.

Note: This works well with reasonably small files. With a 2000 line, 10 column file it's still usable. With 10000 lines it starts to lag a bit. I wrote this because I often feel the need to write files in a simple table format, but don't want to use Excel. You may want to take a copy of your file first, just to be safe.

  • [ ] Highlight headings in bold (or something)
  • [ ] Add sort functionality
  • [ ] Add warning if file already contains new separator
  • [ ] Add methods to insert/remove empty columns

Installing

You can install the latest version of the extension via the Visual Studio Marketplace here.

Code Alignment In Visual Studio Code Shortcut Key

Alternatively, open Visual Studio code, press Ctrl+P or Cmd+P and type:

Source Code

The source code is available on GitHub here.

Configuration

autoAlign.associations

This defines which files will be auto aligned, and what separator to use. By default it sets .csv files to commas, and .bsv files to vertical bars. The separator is used as part of a regex. Single character separators are automatically escaped, but if you need a more complicated separator, you may need to escape other characters.

The separator for the current file type can be changed with the 'Auto Align: Change Separator' command.

autoAlign.enabled

Set this to false to disable auto formatting for the associated file extension.

This flag can be set using the command 'Auto Align: Enable auto align mode' and 'Auto Align: Disable auto align mode'. Alternatively, it can be quickly changed by clicking the 'Auto Align' button on the status bar.

autoAlign.delay

This is the delay after which the file is formatted when you stop typing.

autoAlign.dimSeparator

When auto align is enabled, the separators are dimmed to aid readability. Set this to false if you would prefer not to dim them.

autoAlign.collapseOnDisable

If set to true, the fields will all be collapsed (spaces trimmed) when auto align is disabled. The file can also be collapsed manually using the 'Auto Align: Format' command if auto align is not enabled.

autoAlign.extraSpace

By default, an extra space is inserted after the separator to aid readability. If you don't want this, set this flag to false.

autoAlign.endingSeparator

Set to true add an aligned trailing separator.

autoAlign.repositionCursor

Normally, after formatting, an attempt is made to reposition the cursor somewhere sensible. If this gets annoying, you can disable this by setting this to false.

Credits

I shamelessly stole the alignment code from dakara's Transformer extension.

Icon from deleket.

Regex for matching separators unless quoted by Stack Overflow user hwnd.

Microsoft’s cross-platform code editor, Visual Studio Code, is getting a boost with a major update today. Visual Studio Code now includes an auto-indentation feature — which, as the name suggests, will automagically indent your code on the editor.

The auto-indentation feature will automatically indent your code whenever you move a line around or a set of lines around. It will also automatically indent your code when you copy-paste something which will be really useful especially for those who use StackOverflow quite a lot. Auto-indentation on Visual Studio Code is currently disabled by default, and users can add the editor.autoIndent setting to the editor in order to enable it.

Code Formatting In Visual Studio Code Mac

At the moment, Visual Studio Code includes auto-indentation rules for 5 languages, including JavaScript, TypeScript, CSS, HTML, and JSON. Developers of the so-called language extensions for Visual Studio Code can also add indentation rules to their extensions which will allow users to take advantage of this feature for another language like Python or Swift.

Shortcut To Align Code In Visual Studio

There are a couple of other neat features in the new update for Visual Studio Code, including things like an option to automatically close unedited files in a workspace, and you can find the full changelog here.





Comments are closed.