Alignment Package

Advertisement

We are going to see how to use the Alignment package into the Sublime Text 3.

Table of Content

Introduction Introduction

While development it is nice to see the code with well-aligned or with good indentation.

Most of the users may use the PHPStrom, VSCode, or something else. They have some extensions which do the same thing.

But, I’m a fan of Sublime Text Editor. So, I’m sharing here how to use the Alignment Package of Sublime Text Editor.

So, Lets check out the below an example of aligning and misaligned code.

Example of Misaligned Code Example of Misaligned Code

$data = array(
    'one' => 'One',
    'two' => 'two',
    'three' => 'Three',
    'four' => 'Four',
    'five' => 'Five',
);

Top ↑

Example of Well Aligned Code Example of Well Aligned Code

$data = array(
    'one'   => 'One',
    'two'   => 'two',
    'three' => 'Three',
    'four'  => 'Four',
    'five'  => 'Five',
);

I’m using the Sublime Text 3 (Unregistered). IMO it is best for the development. It has a lot of awesome features.

In this article, I’m explaining the Alignment package (It is in TOP 25 today).

Alignment Git Repo – https://github.com/wbond/sublime_alignment/

Top ↑

How to Install Alignment Package? How to Install Alignment Package?

Just follow below simple steps:

  1. Press Command+Shift+P to open Command Palette.
  2. Type Install Package until you see Package Control: Install Package.
  3. When the list of packages appears, type Alignment until you find it.
  4. Press Enter to install Sublime Alignment.

Check out below GIF for reference to know How to Install the alignment package.

Installing NPM alignment package
Installing NPM package.

Now, Open the preferences file for Sublime Alignment:

Windows: Preferences > Package Settings > Alignment > Settings-User
Linux: Preferences > Package Settings > Alignment > Settings-User
Mac OS X: Sublime Text 2 > Preferences > Package Settings > Alignment > Settings-User

{
    // The mid-line characters to align in a multi-line selection, changing
    // this to an empty array will disable mid-line alignment
    "alignment_chars": [
        "=", ":"
    ]
}

Here, We have added the = and : So, When you select the code which include the = or : then it aligns with these characters.


Top ↑

Using Alignment Package Using Alignment Package

Select the lines you wish to align. Press Ctrl+Alt+A (Windows & Linux) or Command+Ctrl+A (Mac OS X)

Check out below example to align the code.

BEFORE

$data = array(
    'one' => 'One',
    'two' => 'two',
    'three' => 'Three',
    'four' => 'Four',
    'five' => 'Five',
);

AFTER

$data = array(
    'one'   => 'One',
    'two'   => 'two',
    'three' => 'Three',
    'four'  => 'Four',
    'five'  => 'Five',
);

Check out the below GIF example for reference.

Sublime Text Alignment Package Usage
Using Alignment Package

The sublime text provides a lot of useful keyboard shortcuts. Check out the complete list of Sublime Text 3 Keyboard Shortcuts Cheat Sheet.

Leave a Reply