Search code examples
vim

Is there a native way to shift text vertically in Vim while exchanging with an adjacent line?


Consider the following text file:

--> Foobar
    Barfoo

I would like to transform this text to

    Foobar
--> Barfoo

There are several ways to do this. One possibility is to use REPLACE mode to overwrite the first line with three spaces, and then use REPLACE mode.

However, I'm wondering if there's a native way to select the array text (maybe in visual block mode) and pull it down to an adjacent line.


Solution

  • After learning about visual re-selection (gv), I figured out how to do this in a way that generalizes to any kind of visual selection (the first step is different depending on what one wants to select and move).

    1. Select the block visually: 0vf>
    2. Yank the block into a register: y
    3. Re-select the block: gv
    4. Replace all the text with a space character: r[Space]
    5. Move down a line: j
    6. Enter Replacement mode with [Shift]R.
    7. Paste the register contents [Control]R"

    I recorded this as a macro and it works reasonably well.