Kevin Thompson
Kevin Thompson

Reputation: 2506

vim: left align consecutive lines with the current line

Often times when I paste into vim I get cascading indents that are quite frustrating to fix. The result will look something like this

This is line one
    This is line two
        This is line three
            This is line four

I'd like to know if there is a way that I could tell vim to align lines two through four with line one. If line one text is starting at cursor position 6 is there a way to say "make the next ten lines also start at position 6?"

Upvotes: 11

Views: 5405

Answers (3)

romainl
romainl

Reputation: 196546

Before pasting, do :set paste, after pasting, do :set nopaste.

Or use Vim's built-in paste commands with the clipboard register:

"+p (paste after the cursor or below the line)
"+P (paste before the cursor or above the line)

See :help 'paste' and :help registers.

Upvotes: 2

Benj
Benj

Reputation: 32398

You can use :set paste to avoid this when pasting in text. And you can set the indent level for a range with left.

:<range>left3

E.g.

.,+4left3

Will set the indent of the next 4 lines to 3.

Note: Range can be defined in visual mode, just select some lines with S-v and then press :left4

Upvotes: 4

pb2q
pb2q

Reputation: 59617

To correct this cascading indentation, you can re-indent a block using =. Select a visual block and type = or supply a motion: =4j to re-indent the next 4 lines.

You might avoid the cascading indentations by setting paste before pasting: :set paste. After the paste :set nopaste.

Upvotes: 15

Related Questions