Once again, command mode is vastly underrated:
:t.|-j
DONE
Update I saw in another comment that you want to do this for a range. This is easy too. See below
This is basically the Ex equivalent of yyPJx
except
- It won't clobber any registers
- Won't shift the "0-"9 registers
- Won't affect the current search and/or search history (like in the
:%s
based answer)
- Can be immediately repeated by doing
@:
- no macros, no mappings :)
- will result in atomic undo (whereas the
yyPJx
approach would result in 3 separate undo steps)
Explanation:
:t
is synonym for :copy
:j
is short for :join
:-j
is short for :-1join
, meaning: join the previous line with it's successor
Note: If you wanted to preserve leading whitespace (like yyPgJx
instead of yyPgJx
) use:
:t.|-j!
Update for repeats, with a visual selection type
:'<,'>g/^/t.|-j
Which repeats it for every line in the visual selection. (Of course, :'<,'>
is automatically inserted in visual mode). Another benefit of this approach is that you can easily specify a filter for which lines to duplicate:
:g/foo/t.|-j
Will 'duplicate' all lines containing 'foo' in the current buffer (see windo, bufdo, argdo to scale this to a plethora of buffers without breaking a sweat).