Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have the following lines:

    '0,
    '1,
    '2,
    '3,

How to put quote before the comma?

I've tried the following so far:

%s/[0-9],/&/g

so & here replaces the matched pattern...

share|improve this question

5 Answers 5

Maybe you forgot to define the general form where you need this to work. To do just what you asked for without guessing, this is the most simple (lazy!) solution that works for your example input:

%s/,/',/

If you want to handle more complex cases too, you should write better examples including the most complex case where you need the pattern substitution to work, and ideally an example of the target output too.

share|improve this answer
%s/,$/',/g

$ is the end of the line

share|improve this answer

Usually I use macros for tasks like this.

qqEi'<Esc>0jq3@q

will get the job done (note the <Esc> part is an escape key press, not 5 separate keys). Here is a break down:

  • qq starts recording a macro and assign it to the q key.
  • Ei'<Esc> goes to the end of the word and inserts the '.
  • 0j goes to the beginning of the next line.
  • q ends the recording.
  • 3@q plays the macro 3 times, applying the same thing to the other 3 lines.

Replace 3 with the number of lines that you have.

share|improve this answer

If you want to move the quota from place before the number to a place before the comma, then you can use grouping

%s/'\([0-9]\),/\1',/

or to put another quotation mark before the comma, just include it into the first group:

%s/\('[0-9]\),/\1',/
share|improve this answer
1  
How is this answer different from Krzysztofs? –  Bernhard Oct 28 '12 at 20:14

You can use something like:

%s/\('[0-9]\),/\1',/

It uses \( and \) to create selection group which is then referenced by \1 (which means first group) on the substitute side.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.