Vi Editor

Hi ,

I would like to delete all lines after 40th line in vi editor . Please let me know what command I can use for it?

Also how to replace one word with another in the middle of 5th line?

Thanks

This should work...

a) delete all lines after 40th line

:41,$d

b) replace one word with another in the middle of 5th line?

:5s/ORIG/REPLACE
1 Like

To delete all lines from 40 onward within the editor you would type:-

:41,$ d

From the command line I think you could

sed -i '41,$ d' filename

If you are editing the file on screen, then to change a word just move to the line, position the cursor at the begginning of the word to be replaced and type cw Nothing will appear on screen when you press the c but the word will disappear when you press the w and leave you in input mode ready to type the replacement word. When you have finished, remember to press Escape to get back to command mode.

Save your edits with :wq

I hope that this helps, but please let me know if I have missed the point.

Kind regards,
Robin

1 Like