340

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

I tried the code unsuccessfully

:%s/\n/*\n/g
1
  • 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
    Commented Jun 15, 2013 at 13:26

10 Answers 10

500

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
9
  • 51
    Man, there exists no vi or vim thread from which I do not learn something. Today I learn about norm. And as they say at Cheers, "Norm!"
    – smcameron
    Commented Mar 1, 2009 at 7:27
  • 6
    For prepending, you'd just use 0i*, 0 takes you to the beginning of the line, i for insert mode, and * for the *. D:
    – Rob
    Commented Oct 12, 2011 at 18:35
  • 9
    As a VimGolfer I can't help but add that using I* rather than 0i* will save you one keystroke. Commented Oct 13, 2011 at 21:50
  • 33
    One other tip: This works for ranges as well. Just highlight the lines in visual mode and press :, this will pre-fill the command with :'<,'> which you can then make into :'<,'>norm A* Commented Oct 13, 2011 at 21:53
  • 11
    For prepending, use I instead - :%norm I* Commented Jul 4, 2016 at 11:46
430
:%s/$/\*/g

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

3
  • Is there a way to insert '' at the same column, since all lines are not of same length, so line 1 might have '' at 15th column, but line 2 has '*' at 25th column.
    – Aman Jain
    Commented Jul 21, 2010 at 20:41
  • 16
    there is only 1 EOL per line, so the g flag is unnecessary.
    – doubleDown
    Commented Jun 15, 2013 at 13:14
  • 1
    I wanted to add / to the start of each line :1,$s/^/\/g worked for me. Commented Dec 17, 2013 at 17:51
79

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)

5
  • 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
    Commented Nov 29, 2013 at 8:02
  • Agree that visual block is cleanest here, more extensible too Commented May 17, 2018 at 2: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
    Commented Mar 15, 2019 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
    Commented Oct 11, 2019 at 19:10
  • make sure you use Capital A, lowercase "a" doesn't work.
    – Jeff Hoye
    Commented Jan 24, 2020 at 10:29
46

Also:

:g/$/norm A*

Also:

gg<Ctrl-v>G$A*<Esc>
3
  • Ahhh... The second one can be used with a selection of lines too. Nice! Commented Oct 13, 2011 at 16:03
  • Could you please explain the first example?
    – Cody Poll
    Commented Mar 8, 2015 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
    Commented Jul 30, 2016 at 9:49
21

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/
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.

2
  • 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. Commented Feb 28, 2009 at 0: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
    Commented Feb 28, 2009 at 3:50
9

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*
6

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

%s/^/*/g
1
  • This doesn't answer the question.
    – doubleDown
    Commented Jun 15, 2013 at 13:12
4
:%s/\n/*\r/g

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

2
  • In my version of Vim \n works, but this just replaces the newline with a *, effectively joining the lines. Commented Mar 1, 2009 at 7: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?
    – user42092
    Commented Mar 1, 2009 at 23:44
3
%s/\s*$/\*/g

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

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