views:

2168

answers:

7

I tried the code unsuccessfully

:%s/\n/*\n/g
+10  A: 

:%s/$/\*/g

should work. So should :%s/$/*/g as MrWiggles points out correctly.

dirkgently
+1. No need to escape the asterisk in the replacement tho
MrWiggles
Yup. More readable to me with one, though.
dirkgently
I won't argue, you have an awesome username so that gets you kudos points ;-)
MrWiggles
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
+1  A: 

%s/\s*$/*/g

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

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

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

Ant P.
In my version of Vim \n works, but this just replaces the newline with a *, effectively joining the lines.
Nathan Fellman
This command is verified to work fine on every version of Vim I've tried. Are you using the windows version or something?
Ant P.
+2  A: 

Note there is no need for the trailing 'g' as only one replacement is possible.

anon
"as only one replacement is possible" *per line*
Thank you Neil! Simplicity is always beautiful :)
Masi
+1  A: 

Another option no-one has mentioned is

:g/$/s//*

paxdiablo
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.
Masi
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
+3  A: 

Also:

:g/$/norm A*

Also:

gg<Ctrl-v>G$A*<Esc>
Brian Carper
+9  A: 

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
Leonardo Constantino
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
Ditto - voted up for "norm"
Nathan Fellman