4

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.

Community
  • 1
  • 1
Tem Pora
  • 2,043
  • 2
  • 24
  • 30

4 Answers4

10

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

  1. It won't clobber any registers
  2. Won't shift the "0-"9 registers
  3. Won't affect the current search and/or search history (like in the :%s based answer)
  4. Can be immediately repeated by doing @: - no macros, no mappings :)
  5. 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).

sehe
  • 374,641
  • 47
  • 450
  • 633
  • 2
    It is really more helpful than the previous answer, so i accepted this. @sehe: "command mode is vastly underrated" this is a really insightful comment. – Tem Pora Nov 11 '12 at 07:12
  • 1
    @TemPora Thanks for the kind words. Command mode takes getting used to. Usually as long as you have linewise operations there is a command mode equivalent for doing things (e.g. `%>|%y+|undo` is convenient for posting on SO :)) – sehe Nov 11 '12 at 12:36
6

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.

romainl
  • 186,200
  • 21
  • 280
  • 313
  • Wow, 3 different answers in a couple of minutes... you guys are really quick today :-) – Ingo Karkat Nov 07 '12 at 15:43
  • 3
    You don't need the anchoring `^...$`, as `.` will match anything (except the newline) by default: `:s/.*/&&`. – Ingo Karkat Nov 07 '12 at 15:44
  • 1
    I have added an answer with 4 fewer keystrokes, **and** it doesn't affect search pattern history. See http://stackoverflow.com/a/13280126/85371 - Eternal praise for Ex mode commands – sehe Nov 07 '12 at 23:33
  • @sehe: I'm only starting to use Ex commands myself. I've introduced `:m` in my daily usage recently and it kicks ass. Your answer is awesome. – romainl Nov 08 '12 at 07:10
  • 1
    @romainl Cheers :) `g//m$` is nifty, so many times for me too – sehe Nov 08 '12 at 09:50
1

Go to the line you want to append to itself. Then type: 0y$$p

Explanation:
0 - for going to the start of the line
y$ - yank everything from the cursor to the end of the line
$ - for going to the end of the line
p - for "putting" the yank buffer

pitseeker
  • 2,535
  • 1
  • 27
  • 33
  • Might be worth adding `^` to the beginning of that. – Benj Nov 07 '12 at 15:37
  • Instead of `$p`, you can also just use `P`, as the cursor is at the beginning after the yank. – Ingo Karkat Nov 07 '12 at 15:42
  • I knew that but I want to do it for a bunch of lines with a command something like `:%s/$/`. Thanks. – Tem Pora Nov 07 '12 at 15:44
  • Oh - if you wanted repeats, **do** have a look at my Ex answer: `:g/^/t.|-j` or `g/pattern/t.|-j` for specific lines (matching `pattern`) – sehe Nov 07 '12 at 23:40
1

Why don't you try: yyPJx.

This will append a copy of the current line to it's self.

You could even:

noremap ,l yyPJx

In your .vimrc if you want to map a keystroke to accomplish this.

Benj
  • 31,668
  • 17
  • 78
  • 127
  • 2
    It maybe worth noting that `J` will adjust the white space by removing the indent and inserting up to 2 spaces. Depending on your needs `gJ` might be worth a look. – Peter Rincker Nov 07 '12 at 15:56
  • @PeterRincker Thanks Peter, `gJ` definitely could be the right choice, depending on requirements as you say. – Benj Nov 07 '12 at 16:17