Tag Archives: vi

How to exit VI with or without saving

Although many alternatives exist, I like using vi for all my command line editing needs. To save changes, I usually use SHIFT + Z + Z, exiting vi under most circumstances.

But sometimes, this trick doesn’t work because of write permission problems. In such cases, vi doesn’t close with the above command. Instead, we must either stash our changes in another file, or quit the session without saving. Here’s how to do that.

Quit vi without saving:
:q!

Save current file under a different name:
:w newfile

  • https://www.cs.colostate.edu/helpdocs/vi.html

How to quit vi without saving your changes

It just occurred to me that even though I know my way around vi fairly well, I never had to quit it without saving my changes. Usually I just go back in and overwrite my mistakes.

Today I did something though that wasn’t as easy to eliminate: instead of pasting an IP address, I accidentally pasted a 4000+ character stylesheet. Dang!

So how do we leave vi and NOT save our changes? Here’s how:

  • press ESC to exit editing mode (insert/append/whatever)
  • press : (the colon character)
  • enter q!

Now you’re back on the command line without any saved changes.

Remind me: how do we SAVE changes again?

There are several ways of doing this, but my personal favourite is this:

  • press ESC to exit editing mode (insert/append/whatever)
  • press SHIFT + Z twice

This will put you back on the command line and your changes are saved.

  • https://kb.iu.edu/d/afcz
  • https://wpguru.co.uk/2012/03/how-to-use-vi-to-edit-files-in-linux/