Sublime Forum

Multi-Selection Alignment

#1

Sublime Alignment is a plugin that aligns multiple selections and multi-line selections. When aligning multi-line selections, all lines are first indented to the same level. Upon subsequent execution, all equal signs are aligned to the same column. When aligning multiple selections, all selection points are aligned to the same column.

Please see wbond.net/sublime_packages/alignment for full documentation, including installation instructions, settings, key bindings, etc.

Bugs should be reported at github.com/wbond/sublime_alignment/issues.

[size=150]Updates[/size]

Version 2.0.0

  • Removed the old ctrl+shift+a
    key binding for Windows/Linux and cmd+shift+a for OS X to prevent conflicts with the Expand to Tag command key binding. ctrl+alt+a should be used on Windows/Linux and ctrl+cmd+a on OS X.

Version 1.1.3

  • Added settings file for CSS to align on :
    characters also

Version 1.1.2

  • Fixed the indentation import to work when this package is loaded before the Default package

Version 1.1.1

  • Fixed a math error when converting mid-line spaces to tabs

Version 1.1.0

  • Added mid_line_tabs settings

  • Fixed handling of mid-line alignment when align_indent is false and indentation is done via tabs

  • Added an additional key binding, ctrl+alt+a (Windows, Linux) or cmd+ctrl+a (OS X) that doesn’t conflict with the new Expand Selection to Tag command from Sublime Text b2102

Version 1.0.6

  • Fixed a bug with mid-line tabs causing incorrect mid-line alignment

Version 1.0.5

  • Fixed a bug with multi-selection alignment

Version 1.0.4

  • Added align_indent, alignment_chars, alignment_space_chars and alignment_prefix_chars settings

  • Enhanced plugin to not indent blank lines when setting to trim trailing white space is enabled

Version 1.0.3

  • Added Preferences menu entries and command palette commands for preferences

Version 1.0.2

  • Enhanced plugin to remove extra whitespace not necessary for alignment

  • Fixed handling of equal signs without whitespace before them

Version 1.0.1

  • Added multi-line alignment functionality

Version 1.0.0

  • Initial release
1 Like

#2

Thanks for this nice and useful plugin.

Actually it doesn’t work cos the decorator @handles_not_found is not declared, I have to remove this line:

class AlignmentCommand(sublime_plugin.TextCommand): @handles_not_found def run(self, edit):
to

class AlignmentCommand(sublime_plugin.TextCommand): def run(self, edit):

0 Likes

#3

@bizoo

Thanks for pointing that out. I had fixed it in my local copy, but I forgot to commit and push it to github.

0 Likes

#4

Thanks! I plan on using this all the time. :smile:

0 Likes

#5

Sweet!

If you could change this slightly to not have to place the cursor, but instead select the code, then keypress. Awesome!

Also, it doesn’t quite work right in this case;

var test = 1, anothertest = 2, testme = "another var";
“anothertest” gets left behind.

0 Likes

#6

I was thinking of adding a mode for a single multi-line selection that would indent all of the lines to the same level. It sounds like this is what you are looking for, correct?

For your example of failure, where are your cursors placed?

0 Likes

#7

Correct!

Beginning of each identifier.

0 Likes

#8

@jbrooksuk

I’ve updated the plugin to be a bit more powerful. I believe the issue you were having with alignment is that the plugin was intended for aligning things like equal signs in the middle of lines, do it always inserted spaces. The way that sublime measures columns of characters, only a single space would have been inserted if you use tabs for indenting.

That said, I’ve updated the plugin to do what I think you were intending to do, which is to align the left edge of each line to the same indent level. If you have a multi-line selection, the plugin will cause all lines to be indented to the same level. As a bonus, if you run it a second time, the first set of equal signs with be aligned using spaces.

The original functionality for multiple selections still exists and functions that same way.

If any of what I described is confusing, please see the readme for examples.

0 Likes

#9

New update:

The multi-line selection mode now properly handles equal signs that do not have a space before them. Equal signs are aligned to the left-most column possible while leaving at least one space to the left of each equal sign/operator.

github.com/wbond/sublime_alignm … all/master

0 Likes

#10

It would be useful for javascript to check not only for ‘=’ but for ‘:’ too. The equal sign could have precedence.

Nice Plugin

0 Likes

#11

I love that plugin!
I really like having my "="s aligned.
As senzo said, “:” alignment would be great for JavaScript and JSON.

0 Likes

#12

A quick modification for “:” alignment

Replace

equal_pt = view.find('=', pt).a

with

import re
if re.search("=", view.substr(view.line(pt))):
  equal_pt = view.find('=', pt).a
elif re.search(":", view.substr(view.line(pt))):
  equal_pt = view.find(':', pt).a
0 Likes

#13

I made it work with this syntax below:

var test = "sadsad", asd = "Asdasd", asdasdasd = "asdasd";

Buy commenting out this part of the code:

# for pt in points: # pt += adjustment # length = max_col - view.rowcol(pt)[1] # max_length = max([max_length, length]) # adjustment += length # view.insert(edit, pt, (' ' if use_spaces else '\t') * length)

0 Likes

#14

[quote=“rdougan”]I made it work with this syntax below:

var test = "sadsad", asd = "Asdasd", asdasdasd = "asdasd";

Buy commenting out this part of the code:

# for pt in points: # pt += adjustment # length = max_col - view.rowcol(pt)[1] # max_length = max([max_length, length]) # adjustment += length # view.insert(edit, pt, (' ' if use_spaces else '\t') * length)[/quote]

You don’t use this plugin the way it was designed, take a look at the documentation from wbond: https://github.com/wbond/sublime_alignment
The code you commented remove the left alignment and only keep the alignment of “=”, what you probably want.
This plugin must probably be splitted in 2 commands, one for the left alignment and one for the “=” (or “:”) alignment.

0 Likes

#15

I should hopefully be able to add some options for alignment characters and disabling left alignment in the next day or so.

Hopefully I will also figure out how to create a .sublime-package file for those who are so inclined.

0 Likes

#16

I’ve got this error under Ubuntu 11.04 x86_64 kernel 2.6.38-10-generic

Traceback (most recent call last): File "./sublime_plugin.py", line 255, in run_ File "./Alignment.py", line 65, in run AttributeError: 'NoneType' object has no attribute 'a'

0 Likes

#17

Over the past few days I added settings to the plugin for more customization. I also fixed some bugs.

The best way to get the newest version, and to automatically stay up to date with the latest version is to use the Package Control package manager I’ve been working on. Once you manually update Alignment through Package Control, it will automatically check for and install updates whenever Sublime Text 2 starts. You can learn more at wbond.net/sublime_packages/package_control.

@rdougan

The newest version allows disabling indentation alignment on a per-filetype basis.

@xavi

The newest version fixed that bug.

0 Likes

#18

@wbond: thanks for this awesome plugin, my code never looked that good!
There is a way of changing space indent to tab?

Thanks!

0 Likes

#19

@iamntz

The left indent should automatically use tabs or spaces based on the current settings. In terms of mid-line indenting, unfortunately I was not able to figure out last night how to measure the actual displayed width of a tab character via the API. I can get the row and column, but the column is just the number of characters from the beginning of the line. I know ST2 can get the real column number since it is displayed in the status bar, I just couldn’t find a method to translate to that. Without being able to tell how wide each tab is being displayed, I wouldn’t be able to do mid-line tab spacing.

0 Likes

#20

To get the ‘visual’ column of a point, you could replace

view.rowcol(pt)

by

import indentation indentation.line_and_normed_pt(view, pt)
Row is always the same for both.

An addition to the API for it would be nice.

Thanks for your work wbond.

0 Likes