1

This is the text in my file, which I'm editing with vim:

enter image description here

I want to change it to the the following content:

enter image description here

I have tried using this regular expression :%s/^.*$/\g<0>,\g<0>/g, but it's not working..

2
  • Please don't post text as a picture. Post text as text Commented Jun 24, 2016 at 12:03
  • 2
    Another alternative you can use also the backreferecing if you want :%s/\v(.+)/\1,\1/ Commented Jun 24, 2016 at 13:24

4 Answers 4

3

You can achieve that with the following replacement:

:%s/^.*$/&,&/
         ^ ^ 

The ^.*$ pattern will match the whole line, and & is a backreference to the whole match. So, the replacement is inserting back the whole match, a comma, and again the whole match.

NOTE: The ^ and $ in this ^.*$ pattern can be omitted, but a lot of people prefer to keep them explicit in the pattern to facilitate pattern readability and further pattern testing.

An alternative in case you want to avoid matching empty lines:

:%s/.\+/&,&/
    ^^^

The .\+ will match 1 or more characters.

Sign up to request clarification or add additional context in comments.

4 Comments

@Downvoter, please let know what is bad about this answer and why it is not helpful and what research it lacks.
Don't you think that the flag g is useless at the end of the substitution as you are going to select all the line !!
@Meninx: That is not critical, isn't it? It is again the same stuff as ^ and $.
Exactly I was on the cusp of the stating that but I thought that will be too much ! haha and the question is am I wrong ?
3

using :s

%s/.*/&,&/

Note

  • you don't need the ^ and $ since the regex is greedy.
  • you don't need the g flag either.
  • if you want to golf a bit, you don't need the last /

Or use normal command:

%norm! yaWA,^R"

or

%norm! yg_A,^R"

the ^R, you press <c-v><c-r>

4 Comments

@WiktorStribiżew take it easy.. I never downvote answers at SO. because people post answer trying to help others. unless it is a completely wrong answer. If I downvote, I always post comment.
I am novice at vim.could you explain the normal command for me?thanks very much
@elsonwx normal just like doing keystrokes in normal mode. read :h :norm for help doc. In this example, yaW yank the WORD, then A enter INSERT mode, added the , and <C-R> filled the yanked WORD. (yg_) will yank the whole line without the linebreak.
@WiktorStribiżew and your answers are very helpful to me, thanks very much.but WiktorStribizew is the first to answer this question ,so I adopted his answer.thanks for your warm-hearted anwser
1

Another way to do this without using regular expressions is to record a macro as you change the first line, then play the macro to change the remaining lines.

ggqay$A,<ESC>p+q4@a

Explanation:
gg move cursor to beginning of first line
qa start recording into register a
y$ copy the line
A go to insert mode at the end of the line
,<ESC> insert a comma and go back to normal mode
p paste the copy
+ move the cursor to the start of the next line
q stop recording
4@a play the macro four times to change the next four lines.

Comments

-1

Execute these one by one:

:%s/abc/abc,abc/g
:%s/123/123,123/g
:%s/this/this,this/g
:%s/them/them,them/g
:%s/my/my,my/g

Similarly, replace the string with desired string for manipulating any string.

7 Comments

if OP has 500 lines?
well then I would write a bash script for that which will use capabilities of just echo without using vim.
And such answers are not downvoted, and mine was - I just can't get it.
:s accepts regex, obviously, you don't know how to use regex. to be honest, even if you wrote a shell script, your shell script is not a good one either. regex is very basic and important skill for vim and shell programming.
@WiktorStribiżew from your rep I can tell, you are not new at SO. we have seen/experienced downvoting without a reason, haven't we? at least I know it is happening very often.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.