Column alignment of text in emacs is discussed briefly.
If you're a neat freak about your code then you probably prefer related items to line up so that they can be read easily. For example the C declarations
RGBA zx_plane_color = {RED, 0.1};
RGBA zy_plane_color = {GREEN, 0.1};
RGBA xy_plane_color = {BLUE, 0.1};
RGBA plane_grid_color = {GREY, 0.5};
RGBA background_color = {WHITE, 1};
are a lot easier to read as when aligned up on the "=" sign
RGBA zx_plane_color = {RED, 0.1};
RGBA zy_plane_color = {GREEN, 0.1};
RGBA xy_plane_color = {BLUE, 0.1};
RGBA plane_grid_color = {GREY, 0.5};
RGBA background_color = {WHITE, 1};
The Emacs command align-regexp
can handle situations like this and more. The alignment above is accomplished with the sequence of commands below (assuming that the region covers the declarations to align)Keystrokes | Action |
---|---|
M-x align-regexp
|
Emacs responds with "Align regexp: " in the minibuffer.
|
= | Declarations are aligned |
To align the after the commas, repeat the same commands but type "," when prompted for a regexp.
Unfortunately, the default is to align on the first occurrence of the regexp. In many cases, you want to format a table of data such as
12, 34, 23123, 3939
2343, 344,
343, 33, 232, 2323, zzzz
The sequence of commands then isKeystrokes | Action |
---|---|
C-u M-x align-regexp
|
Emacs responds with "Complex align using regexp: \(\s-*\) " in the minibuffer.
|
Erase the "
|
Emacs responds with "Parenthesis group to modify (justify if negative) : 1 "
|
Press ENTER |
Emacs responds with "
|
Press ENTER
|
Emacs responds with "Repeat throughout line? (y or n) " |
Type "Y" then press ENTER | Text is aligned. |
The result is
12, 34, 23123, 3939
2343, 344,
343, 33, 232, 2323, zzzz
Notice that the number of items in each row can differ.align-regexp
can handle much more complex situations than the ones covered here but I believe that these describe the two most common cases in general.