I want to append the entire content of the line (excluding the ending newline) to the line itself.
I saw this solution :%s/$/\*/g
here:
How can I add a string to the end of each line in Vim?
But it is appending the character * to the lines. I tried both :%s/$/*/g
and :%s/$/\*/g
but the same result.
I am using VIM - Vi IMproved version 7.3.46
PS: it seems, as a new user i am not allowed to post this question as a comment there. thanks.
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
:%s
based answer)@:
- no macros, no mappings :)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 successorNote: 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).
You can use this substitution:
:s/^.*$/&&
^.*$
means "whatever (.*
) is between the beginning (^
) and end ($
) of the line".&
represents the matched text so we are substituting the whole line with itself followed by itself again.edit
Ingo's comment is spot-on: :s/.*/&&
does the same with less typing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With