I am very new to Vim and this problem has stumped me.

I am using vim to delete all lines containing a certain word. I have tried saving the edited text with to a new file but it always contains the exact same lines as the original file. What command do I use to save my edited text to a new file and keep the original file? Bear in mind, my file is very large and will take a long time to move to the bottom.

  • I have used :w then typed a new filename, but that file is exactly the same as the original, it isn't saving the changes I've made – Emma Nov 6 '15 at 11:38
  • @Emma try: vim +g/word/d +'w bar' +q! foo, where foo is the original file and bar is the target file, and word is the filtering word. What happens? – muru Nov 8 '15 at 18:51

I'm not sure to understand your need.

If you only want to save lines filtered into a new file, you can play with

" equivalent to "grep the_word > the-filename"
:let @a =''
:v/the_word/y A
:call writefile(@a, 'the-filename')

However a more likely scenario is: if you do multiple edits, :saveas will permit to save the current buffer under in another file. But the current filename won't be changed. If you do some more edits, and hit :save/:update, the old file will get overwritten. You need to call :file in order to change the filename of the current buffer. After that, all following saves will be done to the new file, leaving the old one unchanged.


I'm not sure this is what you want, but let's say you've opened a file called foo, and after editing it you want to save the result to a new file called bar. To do this you can type :saveas bar.

If a file called bar already exists, you will have to tell vim to overwrite it by adding an exclamation mark :saveas! bar.

To navigate between the two files foo and bar, you can type :b# or<C-^> (the Control modifier key and the ^ key pressed at the same time).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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