How can I add a string to the end of each line in Vim?

Viewed 338481

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

I tried the code unsuccessfully

:%s/\n/*\n/g
10 Answers

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
:%s/$/\*/g

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

Also:

:g/$/norm A*

Also:

gg<Ctrl-v>G$A*<Esc>

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.

:%s/\n/*\r/g

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

%s/\s*$/\*/g

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

Related