326

I want to add * to the end of each line in Vim.

I tried the code unsuccessfully

:%s/\n/*\n/g
Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697
  • 1
    Yours doesn't work because on the right hand side of `:s///`, `\n` corresponds to a null character. The section `:help sub-replace-special` is relevant. – doubleDown Jun 15 '13 at 13:26

10 Answers10

477

Even shorter than the :search command:

:%norm A*

This is what it means:

 %       = for every line
 norm    = type the following commands
 A*      = append '*' to the end of current line
Nathan Fellman
  • 122,701
  • 101
  • 260
  • 319
Cyber Oliveira
  • 8,178
  • 4
  • 28
  • 18
421
:%s/$/\*/g

should work and so should :%s/$/*/g.

dirkgently
  • 108,024
  • 16
  • 131
  • 187
70

I think using visual block mode is a better and more versatile method for dealing with this type of thing. Here's an example:

This is the First line.  
This is the second.  
The third.

To insert " Hello world." (space + clipboard) at the end of each of these lines:

  • On a character in the first line, press Ctrl-V (or Ctrl-Q if Ctrl-V is paste).
  • Press jj to extend the visual block over three lines.
  • Press $ to extend the visual block to the end of each line. Press A then space then type Hello world. + then Esc.

The result is:

This is the First line. Hello world.  
This is the second. Hello world.  
The third. Hello world.  

(example from Vim.Wikia.com)

Glory to Russia
  • 17,289
  • 56
  • 182
  • 325
nicole
  • 701
  • 5
  • 2
  • 7
    Instead of pressing `jj` one can hit `G` to move to the last line. This is useful in large files, where pressing `j` until you hit the last line isn't practical. – nash Nov 29 '13 at 08:02
  • Agree that visual block is cleanest here, more extensible too – Peter Dolan May 17 '18 at 02:28
  • 1
    Note that this doesn't seem like it's working on all lines at first, but after you press Esc it goes through and adds it to all. – Noumenon Mar 15 '19 at 17:43
  • Visual Block causes issues and errors when using vim as an extension for other things, like VS Code. In those cases, the search and replace the original user is asking for is the reliable option. – Brettins Oct 11 '19 at 19:10
  • make sure you use Capital A, lowercase "a" doesn't work. – Jeff Hoye Jan 24 '20 at 10:29
45

Also:

:g/$/norm A*

Also:

gg<Ctrl-v>G$A*<Esc>
Brian Carper
  • 71,150
  • 28
  • 166
  • 168
  • Ahhh... The second one can be used with a selection of lines too. Nice! – Tim Fletcher Oct 13 '11 at 16:03
  • Could you please explain the first example? – Cody Poll Mar 08 '15 at 19:12
  • @CodyPoll `g` is `global` command that does something with every matching line. `$` matches every line. It could be also `^` or `.*`. norm executes commands that you normally use in command mode. So `A*` means append `*` to the end of line. – defhlt Jul 30 '16 at 09:49
20

If u want to add Hello world at the end of each line:

:%s/$/HelloWorld/

If you want to do this for specific number of line say, from 20 to 30 use:

:20,30s/$/HelloWorld/

If u want to do this at start of each line then use:

:20,30s/^/HelloWorld/
utkarsh
  • 211
  • 2
  • 3
10

One option is:

:g/$/s//*

This will find every line end anchor and substitute it with *. I say "substitute" but, in actual fact, it's more of an append since the anchor is a special thing rather than a regular character. For more information, see Power of g - Examples.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • What does s// mean in the regex? I know a similar command, :g/ / /p, (grep). I am pretty sure that your command is close to mine, at least in the structure. – Léo Léopold Hertz 준영 Feb 28 '09 at 00:23
  • 1
    The s is substitute - it replace the end of line anchor with the asterisk (well, not actually replaces it since it's an anchor point). – paxdiablo Feb 28 '09 at 03:50
8

You don't really need the g at the end. So it becomes:

:%s/$/*

Or if you just want the * at the end of, say lines 14-18:

:14,18s/$/*

or

:14,18norm A*
glts
  • 21,808
  • 12
  • 73
  • 94
Pedro Norwego
  • 121
  • 1
  • 1
6

...and to prepend (add the beginning of) each line with *,

%s/^/*/g
User 1058612
  • 3,739
  • 1
  • 27
  • 33
4
:%s/\n/*\r/g

Your first one is correct anywhere else, but Vim has to have different newline handling for some reason.

  • In my version of Vim \n works, but this just replaces the newline with a *, effectively joining the lines. – Nathan Fellman Mar 01 '09 at 07:50
  • 1
    This command is verified to work fine on every version of Vim I've tried. Are you using the windows version or something? –  Mar 01 '09 at 23:44
3
%s/\s*$/\*/g

this will do the trick, and ensure leading spaces are ignored.

Conner
  • 30,144
  • 8
  • 52
  • 73
ng.
  • 7,099
  • 1
  • 38
  • 42