Łukasz
Łukasz

Reputation: 36191

Way to remove two non-consecutive lines in vim

Is there a way to remove two non-consecutive lines with a single vim command (which could be undone in a single step).

I have file like this:

if (enabled) {
    const a = 1;
    const b = 2;

    console.log(a + b);
}

And would like to end up with this:

    const a = 1;
    const b = 2;

    console.log(a + b);

Upvotes: 1

Views: 298

Answers (2)

Peter Rincker
Peter Rincker

Reputation: 45087

Use visual mode and paste over the whole block.

yiBvaBVp

Vimcasts episode: Pasting from Visual mode

For more help see:

:h iB
:h aB
:h v_p

Upvotes: 2

Marth
Marth

Reputation: 24802

If you select your example (with v$% for instance), then

:'<,'>g/{\|}/d

will give you what you want (if there are no nested { or }).


Another way would be to record a macro, something like

# qa to start recording, the final q to stop. ^O is <Ctrl-o>
qaf{%dd^Oddq             

then use it with @a.


Edit: more general solution (still using visual mode though):

You could add a mapping:

:xnoremap <leader>d <esc>:'<d \| '>d<cr>

to delete the first and last line of the last visual selection.

Upvotes: 1

Related Questions