Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim equivalent of Emacs' open-rectangle

Tags:

vim

Emacs has a function called open-rectangle, which allows you to select a rectangular region (i.e. Vim's visual block mode), then hit a key combination to fill that rectangle with spaces, pushing any existing content out to the right:

BeforeAfter

This is really useful when working with vertically-aligned columns of text. I feel like I should be able to do this easily in Vim too, using visual block + a search & replace. But I can't seem to figure out why my search & replace isn't bound to my rectangle when I try it.

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

This actually indents the whole line, instead of opening up this selected region. I've tried replacing:

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

But that has the same effect. How can I get my pattern to understand that I only want to replace each line in the selected block with spaces + the selected area? Simple replacements like just changing letters work, but using ^ or .* doesn't work the way I'd expect.

I am aware of the ability to hit "I" and insert some spaces the drop back into normal mode, but that is harder to judge when you're indenting by a large amount, over many lines.

like image 829
d11wtq Avatar asked Oct 27 '12 05:10

d11wtq


2 Answers

How about:

yPgvr<Space>

This yanks the block and pastes it to duplicate it, then re-selects the original block and replaces it with spaces.

like image 101
hammar Avatar answered Oct 17 '22 15:10

hammar


Another way:

  1. Visual-block select only one column.

  2. Hit nI<Space><Esc> with n being the number of blank columns you want.

like image 6
romainl Avatar answered Oct 17 '22 13:10

romainl