SaltyCrane Blog — Notes on JavaScript and web development

Working with columns in Emacs

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.

Comments


#1 nathan commented on :

Sweet Jesus, you rebound C-x? Masochist! ;)

Anyway, the way I'd do what you're doing is with a quick macro:

C-x ( C-a static void C-e A-\ (void); C-x )

(The A-\ is there because I've gotten in the habit of insuring there's no trailing whitespace every time - some people are so sloppy!)

Then I call-last-kbd-macro (I have it mapped to C->) a time or two, if I'm uncertain it'll work, followed with M-0 C-> to blast through the rest of the buffer.

In case you were interested. :)

Though, honestly, I prefer the look your way generated - easier to read, IMHO - so I'm going to have to remember this use of rectangles!

Diggin' your blog here, btw!


#2 Eliot commented on :

Nathan, thanks for the comment. I'm glad you like the blog. I've heard great things about macros, but I haven't gotten into the habit of using them. I need to discipline myself to start using them. Regarding C-x, I just enabled CUA mode when I started using Emacs because C-x,C-c,C-v, etc. are so prevalent on Windows (and everywhere else). Most of the time it is not a problem-- it becomes a problem when I have text selected (as in this example). I do sometimes wonder if I'm missing the proper Emacs was of doing things by using CUA shortcuts to copy and paste all the time.