Here’s my Emacs gem of the day.
Let’s say you’re writing code and you or the folks you work with like to line up all your variable declarations in nice, neat columns – e.g.:
int number; const Thingamajig foo; float fraction;
It takes a bit of work to create these and then more work later on to fix them when you change types and the columns get screwed up or when people get into tabs vs. spaces wars. Emacs can help…
Just use M-x align-current. This command has knowledge of how to do aligning for various constructs in various languages and modes.
If you’re doing something more estoeric that the default rules don’t know about or you want to have more control, you can align things by specifying a regular expression.
Here’s how:
- Select the region (a quick way to do this is to use M-h which is the default keybinding for mark-paragraph)
- M-x align-regexp – it will prompt for “Align regexp:”, a regular expression.
- Since you’re columns are separated by two or more spaces, you could use <space><space>+ (this is a silly example since align-current would work here). If your columns were separated by tabs, then you could type C-q TAB here instead.
- Voila! Like magic you’re columns are aligned.
If you’re going to be doing this a lot of times in a row, you might consider creating a quick and dirty macro to automate it. Just enclose the above process in between a C-x ( and a C-x ) and then use C-x e to execute the macro.
If you’re going to be doing this a lot, you might go as far as to bind it to a key.
thats a neat tip ! Something I have always wanted.
Great thanks.
Very nice tip.