How to Open, Save a File in Vim and Quit the Editor

Vim is a highly configurable text editor for the command line which is designed to make editing text efficient, offering a lot of functionality on top of its predecessor Vi.

 

Pretty much all Linux distributions and other Unix based systems including macOS come with Vim preinstalled, so there is a high probability you already have it on your system.

 

While Vim is feature-rich and available on many systems it does require some extra knowledge before you can open and save files, which is what we will be looking at here.

 

Opening a File in Vim

 

From your command line type "vim" followed by the name of a file that exists or one you wish to create.

 

vim my_file.txt

 

You are now inside the Vim editor. Initially, you will be in the default mode, which is used for navigating the file using Vim commands. When you want to add or remove text press the i key, this will put you in editor mode just like in Nano or another text editor. Press ESC to exit insert mode. 

 

Opening VIM

 

  1. type: vim my_file.txt
  2. press: i
  3. press: ESC

 

Saving a file in Vim / Vi

 

To save a file in Vim type :w (colon followed by w and hit ENTER.)

 

Saving a file in VIM

 

If you are in editor mode you will need to press ESC followed by :w and ENTER to save the file.

 

You can also save the file with a new name by adding its name after the save command:

 

:w new_file.txt

 

 

Inserting text

 

  1. press: ESC
  2. type: :w new_file.txt
  3. press: ENTER

 

Save File and Quit Vim / Vi

 

You can save changes and exit Vim by pressing ESC to go back to normal mode and then :wq followed by ENTER

 

Save and quit

 

  1. press: ESC
  2. type: :wq
  3. press: ENTER

 

 

Quit Vim / Vi without Saving

 

To quit Vim without saving press ESC and then type :qa! and press ENTER to abandon all changes and exit Vim.

 

Quit without saving

 

  1. press: ESC
  2. type: :qa!
  3. press: ENTER

 

 

Here are the basic commands we have covered:

 

  • vim - starts a new Vim instance
  • i - enter insert mode
  • ESC - exit insert mode
  • :w - save file only
  • :w new_file - save file under a new name
  • :wq - save file and quit Vim
  • :qa! - quit Vim without saving

 

Today we have just learned the basics of opening and saving files. Soon I will be writing a post about useful Vim commands for editing files effectively.

terminal command line editor