Vim Emacs Open Rectangle Equivalent

Emacs has an open-rectangle function that allows you to select a rectangular area (for example, the visual block of a Vim image), then press a key combination to fill this rectangle with spaces by clicking on any existing content on the right:

BeforeAfter

This is really useful when working with vertically aligned columns of text. It seems to me that I can also do this in Vim using the visual block + search and replace. But I cannot understand why my search and replacements are not related to my rectangle when I try it.

:'<,'>s/\^/ / 

This actually discards the entire row rather than opening this selected area. I tried replacing:

 :'<,'>s/\v(.*)/ \1/ 

But it has the same effect. How can I get my template to understand that I only want to replace each line in the selected block with spaces + selected area? Simple replacements, such as just working with letters, but using ^ or .* Does not work as I expected.

I am aware of the ability to hit β€œI” and insert some spaces to return to normal mode, but it’s harder to judge when you backtrack in large numbers over many lines.

+6
source share
3 answers

What about:

 yPgvr<Space> 

This blocks the block and inserts it to duplicate it, then reselects the original block and replaces it with spaces.

+8
source

Another way:

  • The visual block selects only one column.

  • Hit nI<Space><Esc> with n - the number of columns you want.

+6
source

As an answer option to romainl, I have the following:

 vnoremap <C-Space> I<Space><Esc>gv 

It allows you to simultaneously insert n spaces through a predefined counter and iteratively add columns by reapplying the mapping.

+2
source

All Articles