Working with columns in Emacs



This is my OLD blog. I've copied this post over to my NEW blog at:

http://www.saltycrane.com/blog/2007/09/working-with-columns-in-emacs/

You should be redirected in 2 seconds.



In Eclipse, I got a plugin called Columns for Eclipse to cut or paste a column of text. (See this previous post.) In Emacs, this functionality is built-in and columns are called rectangles. Here is how to insert a column of text in Emacs.

I had a list of function names and wanted to turn them into function prototypes by adding "static void " at the beginning and "(void);" at the end.
DoSomethingIntelligent
CalculateSomethingComplicated
DoHardWork
MakeOurCompetitorsBegForMercy
GiveNoMercyToCompetitors
BecomeAwareOfReality
DoSomethingBoring
WonderAboutTheMeaningOfLife
WishIWerePythonOrLisp
To do this, I put the cursor at the beginning of the first function name, pressed CTRL+SPACE to "set the mark" (see section 12 of the manual for more information about the mark), and moved the cursor to the beginning of the last function name. Then do M-x string-insert-rectangle <RET> static void <RET> and got:
static void DoSomethingIntelligent
static void CalculateSomethingComplicated
static void DoHardWork
static void MakeOurCompetitorsBegForMercy
static void GiveNoMercyToCompetitors
static void BecomeAwareOfReality
static void DoSomethingBoring
static void WonderAboutTheMeaningOfLife
static void WishIWerePythonOrLisp
I just realized now that I cannot add the "(void);" to the end with string-insert-rectangle because I do not want to insert the "(void);"s as a rectangle. If I did, it would look like this:
static void DoSomethingIntelligent         (void);
static void CalculateSomethingComplicated  (void);
static void DoHardWork                     (void);
static void MakeOurCompetitorsBegForMercy  (void);
static void GiveNoMercyToCompetitors       (void);
static void BecomeAwareOfReality           (void);
static void DoSomethingBoring              (void);
static void WonderAboutTheMeaningOfLife    (void);
static void WishIWerePythonOrLisp          (void);
But that is ugly and I don't like that. So I guess I'll have to learn how to write a Lisp macro to do this. To be continued...

Note, If you want to delete the "static" from all the prototypes, you can select the text in the same way and do M-x delete-rectangle. Or you could use C-x r d if you haven't rebound C-x like I have.

See also Section 16 Rectangles for more information.

No comments:

About

This is my *OLD* blog. I've copied all of my posts and comments over to my NEW blog at:

http://www.saltycrane.com/blog/.

Please go there for my updated posts. I will leave this blog up for a short time, but eventually plan to delete it. Thanks for reading.