178

In Vim, I have the following text:

key => value1 key => value2 key => value1111 key => value12 key => value1122222 

I would like to add "," at the end of each line. The previous text will become the following:

key => value1, key => value2, key => value1111, key => value12, key => value1122222, 

Does anyone know how to do this? Is it possible to use visual block mode to accomplish this?

1
  • Similar question here. Commented Jul 3, 2012 at 11:47

10 Answers 10

246

This will do it to every line in the file:

:%s/$/,/ 

If you want to do a subset of lines instead of the whole file, you can specify them in place of the %.

One way is to do a visual select and then type the :. It will fill in :'<,'> for you, then you type the rest of it (Notice you only need to add s/$/,/)

:'<,'>s/$/,/ 
Sign up to request clarification or add additional context in comments.

4 Comments

The last row has an extra ', should be :'<,'>s/$/,/.
why is there no need for a g? (:'<,'>s/$/,/g)
@ThorbjørnE.K.Christensen because /$/ matches only once anyway.
I don't like how this sets @/. Any ways around this?
214

There is in fact a way to do this using Visual block mode. Simply pressing $A in Visual block mode appends to the end of all lines in the selection. The appended text will appear on all lines as soon as you press Esc.

So this is a possible solution:

vip<C-V>$A,<Esc> 

That is, in Normal mode, Visual select a paragraph vip, switch to Visual block mode CTRLV, append to all lines $A a comma ,, then press Esc to confirm.

The documentation is at :h v_b_A. There is even an illustration of how it works in the examples section: :h v_b_A_example.

10 Comments

No, this is standard Vim: Select some lines in Visual block mode (with C-V) then move the cursor to the end of the line $ and append to all of them A. You'll love :h v_b_A, which is really thorough.
Oh, I see what's going on! I usually use Ctrl-C instead of <Esc> to exit insert mode, and apparently with Ctrl-C this doesn't work! How odd.
You can save a keystroke by using <C-V>ip in place of vip<C-V>.
@accolade, doesn't go into visual block mode that way, and won't work. <C-V> needs to happen after again if you do it that way.
@NoelEvans because vip places the cursor at the bottom left corner of the selection and A has no special meaning for visual modes (check with :vmap A). it doesn't appear to be possible to move the cursor to a right side edge with o or O as they only function for visual block mode and vip does not produce a block selection (see :h v_o). that still probably wouldn't suffice except for in the case that one corner is longer than all the rest.
|
54

Another solution, using another great feature:

:'<,'>norm A, 

See :help :normal.

3 Comments

@Swiss, you will like this comment, then.
@Swiss, you might also like udioca's exposé on :normal. I found it informative!
Didn't know about this subreddit. Thanks.
53

ex mode is easiest:

:%s/$/, : - enter command mode % - for every line s/ - substitute $ - the end of the line / - and change it to , - a comma 

Comments

12

The substitute command can be applied to a visual selection. Make a visual block over the lines that you want to change, and type :, and notice that the command-line is initialized like this: :'<,'>. This means that the substitute command will operate on the visual selection, like so:

:'<,'>s/$/,/ 

And this is a substitution that should work for your example, assuming that you really want the comma at the end of each line as you've mentioned. If there are trailing spaces, then you may need to adjust the command accordingly:

:'<,'>s/\s*$/,/ 

This will replace any amount of whitespace preceding the end of the line with a comma, effectively removing trailing whitespace.

The same commands can operate on a range of lines, e.g. for the next 5 lines: :,+5s/$/,/, or for the entire buffer: :%s/$/,/.

Comments

5
:%s/$/,/g 

$ matches end of line

3 Comments

That will add $ to the line as well, not just ,.
/g is used to perform the substitution on every occurrence in a line. Because there's only one $ in a line you can safely drop it.
Yeah my mistake on the ,$. and the /g is from habbit, and yes it is optional.
4

If you want to add ',' at end of the lines starting with 'key', use:

:%s/key.*$/&, 

1 Comment

Or :g/key/s/$/,. See :help :global.
3

I have <M-DOWN>(alt down arrow) mapped to <DOWN>. so that I can repeat the last command on a series of lines very quickly. with this mapping I can:

A,<ESC> 

And then hold alt while pressing down repeatedly to append the comma to the end of each line.
This works well for me because it allows very good control over what lines do and do not get the change.
(I also have the other arrows mapped similarly to allow for easy repeating of .)

Here's the mapping line to paste into your vimrc:

map <M-DOWN> <DOWN>. 

Comments

2

Following Macro can also be used to accomplish your task.

qqA,^[0jq4@q 

Comments

0

I find this the easiest:

:%norm A, 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.