Quickstart: How to save in Vim

Guide last updated: May 2026

Quickstart: How to chown recursive and non-recursive

Guide last updated: May 2026

  1. To save and quit in Vim: Press Esc, type :wq, and press Enter.
  2. To save in Vim: Press Esc, type :w, then press Enter
  3. To save as in Vim:Press Esc, type :w filename.txt and press Enter.
  4. To exit Vim without saving: Press Esc, type :q! and press Enter.

Vim is one of the most powerful and popular command-line text editors on Ubuntu or Linux, but it can also be a little intimidating. With its multiple modes and sometimes unintuitive commands, it’s easy to get lost starting out. One particular struggle for new Vim users is knowing how to save a file.

Using Vim modes

Vim has two primary modes: command mode and insert mode. The command mode, which is the default when you open the file, allows you to navigate, delete text, copy, and save. The insert mode, meanwhile, allows you to add new text to the file.

How to switch between modes in Vim

  • To go from insert mode to command mode, press Esc.
  • To go from command mode to insert mode, press i.

For the examples in this article, we'll assume that users are already in insert mode.

How to save changes in Vim

There are three ways to save in Vim: save, save as, and save and quit.

How to Save and Quit in Vim in a single command


To save yourself some time, you can save and exit Vim with one command. Just use :wq instead of :w:

  1. Press Esc.
  2. Type :wq or :wq filename.txt.
  3. Press Enter.

Alternatively, you can use the :x command. This will only save the file if there are unsaved changes, whereas :wq will always save and update the modification time in the file’s metadata.

How to save in Vim

Without too much preamble, the command to save in Vim is :w. However, you may have noticed that if you just type this into your document nothing happens. This is because to use a command in Vim, you first have to switch to “normal mode”, which will allow you to run commands. So, full process looks like this:

  1. Press Esc on your keyboard to enter normal mode.
  2. Type :w.
  3. Press Enter to run the command.

How to Save As in Vim

As with most text editors, Vim also has a “Save As” function, allowing you to write the changes you made to a document to a separate file. To use it, follow these steps:

  1. Press Esc to enter normal mode.
  2. Type :w yourfilename.txt.
  3. Press Enter.

Using ZZ to save and quit Vim


Another, arguably more convenient way to quit Vim is using the ZZ command. It's easier to type, easier to remember, and faster. Keep in mind, however, that ZZ has slightly different behavior than :wq. ZZ saves only if the file has been modified, while :wq saves regardless.

That said, to use ZZ:

  1. Press Esc to enter command mode.
  2. Type ZZ
  3. Press Enter.

Exit Vim without saving

The final scenario that you might run into is the need to quit a file in Vim without saving at all. If :w writes and :wq writes and quits, it feels natural to use :q to quit. However, Vim will not let you quit with this command if you have unsaved changes to a file. Instead, you’ll have to follow these steps:

  1. Press Esc
  2. Type :q!
  3. Press Enter

The ! forces Vim to quit even if there are unsaved changes to the file.

For more command-line text editing tutorials, see:

FAQs

How can I use vimtutor to learn Vim basics?

Vim ships with vimtutor. You can launch it directly from your command line using vimtutor for an interactive lesson on the basics of how to use the text editing tool.

What's the difference between edit and insert mode in Vim?

Edit mode, also known as normal/command mode, allows you to navigate, delete, copy, and run commands. Insert mode is required for adding new text.

How do I undo and redo changes in Vim?

Press u in normal mode to undo your last change. To redo (reverse an undo), press Ctrl+r. You can press u repeatedly to step back through your edit history.

How do I copy and paste text in Vim?

In normal mode, press yy to yank (copy) the current line, or yiw to yank the word under the cursor. Move to where you want the text and press p to paste after the cursor, or P to paste before it. To copy to your system clipboard instead, use "+y.

How do I delete a line in Vim?

Press dd in normal mode to delete (cut) the entire current line. To delete multiple lines, put the number of lines in front. For example, 5dd deletes five lines, moving down from the current line.