Working with columns 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 WishIWerePythonOrLispTo 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 WishIWerePythonOrLispI 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.
Related posts
- Magit in Spacemacs (evil-magit) notes — posted 2018-11-02
- Switching from Emacs to Vim (actually Spacemacs) — posted 2015-12-31
- Colorized, interactive "git blame" in Emacs: vc-annotate — posted 2011-05-28
- My Emacs Python environment — posted 2010-05-10
- Emacs espresso-mode for jQuery — posted 2010-03-10
- Notes on C++ development with Emacs on Ubuntu Linux — posted 2009-07-08
Comments
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!
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.