6

I often encounter situations in editing text or code where I want to insert a rectangle of spaces to align things, but I don't know exactly the number of spaces to insert.

For example, consider the following (very much contrived) snippet:

void *var = (void *)typedVar;
void *otherVar = voidStarOtherVar;
int intVar = x*y;
int intVar2 = y*z;

In default C-mode, M-x align results in this, which is better:

void *var      = (void *)typedVar;
void *otherVar = voidStarOtherVar;
int   intVar   = x*y;
int   intVar2  = y*z;

However, suppose my desired alignment (for some reason) is this:

void *var      = (void *)typedVar;
void *otherVar =         voidStarOtherVar;
int   intVar   =         x*y;
int   intVar2  =         y*z;

The only way I know to do that is with M-x string-rectangle on the bottom three lines, and type in the exact number of spaces.

However, I don't want to count the number of characters in (void *) before typing in the spaces, thus it would be nice to have an "interactive" rectangle string insert. For example, I type a space in this interactive mode, and I see it reflected immediately in the text. I enter another space, and it is inserted. In this way I can interactively align the text to my desired position.

Is there a built-in way to accomplish this? Or, failing that, can I create this functionality somehow?

0

3 Answers 3

7

Open rectangle may be useful for your example:

C-x r o' Insert blank space to fill the space of the region-rectangle (open-rectangle'). This pushes the previous contents of the region-rectangle to the right.

So you mark the desired rectangle, and this function will push content to the right of the rectangle.

Sign up to request clarification or add additional context in comments.

2 Comments

This is great! Thanks. One followup question: is there such a thing as a rectangular mark? For example, if I want to select a rectangle but there isn't enough text to move to the right to the desired column. Hopefully that makes sense.
@proc-self-maps Any of the two solutions I presented does what you call "rectangular mark", I'm not sure if multiple-cursors will do it as well. They also each have immediate feedback of space insertion.
2

Another alternative to those already presented is multiple-cursors on github, which is highly interactive and quite fun. There's a sublime Emacs Rocks! episode covering it on YouTube.

Comments

2

Yes. The built-in mode is cua-selection-mode.

Here is a video of cua-mode working. cua-selection-mode activates just the rectangular selection aspect of it.

You can also get this functionality with iedit, but that's an external package. Its main purpose is editing mirrors.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.