Vi command to insert at the begin of a line for lines in a file

Vi command to insert at the begin of a line for lines in a file

Post by gpalad » Fri, 20 Jan 2006 05:48:12


What is the "Vi" command to insert some line of code at the beginning
of all the lines in a file globally at one shot?
 
 
 

Vi command to insert at the begin of a line for lines in a file

Post by Sash » Fri, 20 Jan 2006 05:57:08


In command mode,
:%s/^/insert_your_code_here/
Sashi

 
 
 

Vi command to insert at the begin of a line for lines in a file

Post by gpalad » Fri, 20 Jan 2006 06:00:21

Thank you so much Sashi.
 
 
 

Vi command to insert at the begin of a line for lines in a file

Post by Chris F.A. » Fri, 20 Jan 2006 06:03:06


Since this is a shell newsgroup, not a vi (or other editor)
newsgroup, I'll give you a shell method:

sed "s/^/Text to insert/" FILE > tempfile &&
mv tempfile FILE

If you want, you can probably run it from within vi.

--
Chris F.A. Johnson, author | < http://www.yqcomputer.com/ >
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
 
 
 

Vi command to insert at the begin of a line for lines in a file

Post by libdav » Fri, 20 Jan 2006 06:11:52

:%s/^/new stuff/
 
 
 

Vi command to insert at the begin of a line for lines in a file

Post by hq00 » Fri, 20 Jan 2006 14:31:42


If you want to insert many lines in Vi the you might want to put those
code in a separate file, say 'some.code', and use the command:

:g/^/ -r some.code

Change the '^' to '.' if you only want to process non-blank lines.

HTH.
--
Regards,
hq00e