Logo

How to quit the Vi Editor and save your work

Try this guide with our instant dedicated server for as low as 40 Euros

How to quit the Vi Editor

For many people, writing code and scripts is all about the compilers, libraries, and IDEs. They often forget the role of text editors in the coding process.

However, ask expert Linux users, and they will tell you that they prefer a text editor over fancier tools, especially when writing scripts in a terminal.

Vi and its extension, Vim, are very versatile text editors. In fact, you can find Vim on all mainstream Linux distros. As a result, programmers, system administrators, and data scientists use it as a handy tool for quickly writing scripts and programs.

In this comprehensive guide, we will guide you on effectively saving files and gracefully exiting these editors. We understand many readers might not know much about Vi and Vim, let’s start with an introduction to these editors and then go into the details of how to save files in Vi/Vim.

Table Of Content

  1. Getting to Know Vi and Vim Editors
  2. An Overview of the Vi Editor
  3. An Overview of the Vim Editor
  4. Choosing Between Vi and Vim
  5. How to Save a File in Vi/Vim
    1. Prerequisites
    2. How Does Command and Insert Modes Work?
    3. Enter the Command Mode
    4. Transitioning Between Insert Mode and Command Mode
    5. Saving a File in Vi/Vim
    6. Saving Changes Without Exiting
    7. Saving Changes and Exiting
    8. Saving Files with another name
  6. How to Exit Vi/Vim Editor
  7. Conclusion

Getting to Know Vi and Vim Editors

Vi and Vim are two separate versions of a popular text editor. In a sense, they are different programs that deliver unique capabilities. However, both programs have a significant feature overlap.

Let’s take a quick look at these editors before we dive into the choosing between the two.

An Overview of the Vi Editor

The Vi editor, short for Visual editor, is a powerful  text-based editor that functions within a terminal environment. As one of the earliest screen-based text editors, Vi has historical significance and is present in nearly all Unix and Linux systems. Vi operates primarily in three modes: Normal, Insert, and Command-line.

  • Normal Mode: This is Vi’s default mode, where keystrokes are interpreted as commands rather than text input. Users can navigate through the file content, delete, copy, paste, and perform various other operations using specific key combinations.
  • Insert Mode: In this mode, users can directly input and edit text like a standard text editor. You can transition from Normal to Insert mode by hitting the I key and start entering text.
  • Command-line Mode: In this mode, users can enter specific commands to perform various actions, such as saving the file, searching and replacing text strings. You can transition from Normal mode to Command-line mode by pressing the : key.

An Overview of the Vim Editor

Vim (an acronym for Vi Improved) is an enhanced iteration of Vi. It introduced several advanced features and capabilities, such as syntax highlighting, code completion, and an extensive plugin system. As such, Vim provides an enriched editing experience for programmers and developers.

Here’s a quick look at Vim’s essential functionalities:

  • Vim Commands: Vim commands cover all essential operations for navigating, editing, and managing text in the editor. These basic commands are used in the normal mode, where users can search, replace, copy, and paste text.
  • Files in Vim: Vim facilitates efficient simultaneous handling of multiple files, enabling seamless navigation and efficient editing. Users can easily open, switch between, and save files. These capabilities lead to better productivity in the terminal.
  • The Update Command: Vim’s Update command (:w) saves file changes while keeping the file open in the editor. Users don’t have to wait until the end of the Vim session to save the changes.
  • Keyboard Shortcuts: Vim’s long list of keyboard shortcuts optimizes the editing workflows. These shortcuts, often in key combinations, expedite tasks and make Vim a highly efficient text editing tool.

To begin using Vi or its enhanced version, Vim (Vi Improved), open a terminal and type vi or vim followed by the name of the file you wish to edit. Once in Vi or Vim, you can edit and manage text using the available commands and options.

Choosing Between Vi and Vim

Before we go into the details of saving files in Vi and Vim and exiting the editor, it’s crucial to pick the editor that best suits your needs. The following feature comparison will help you make an informed decision. 

Feature Vi Vim (Vi Improved)
Basic Editor Yes Yes
Extensions No Yes
Visual Mode Yes Enhanced and more intuitive
Syntax Highlighting No Yes
Auto-Commands No Yes
Scripting Language Basic Vimscript. Vim also supports Python and Perl
Plugins No Yes (Extensive plugin ecosystem)
Customization Limited Extensive
Ease of Use Steeper learning curve Slightly more user-friendly
Community Support Limited Community-delivered features and fixes

Vim and Vi are excellent editors. However, they are not the only options on Linux systems. We have compiled a list of 7 best text editors for Linux in 2023 to help you pick a good fit for your requirements. 

How to Save a File in Vi/Vim

Now that you’ve picked your preferred editor, let’s go into the details of how you can save a file in Vi and Vim.

Prerequisites

  • A machine running a Linux operating system.
  • A user account with sudo privileges.
  • Access to terminal or command line interface.
  • Vim (or the default Vi) installed and active on the system.

Understanding how the modes work is an essential aspect of working with Vi and Vim editors. Let’s discuss the technicalities of the Command and Insert modes before moving on to the process of saving files.  

How Does Command and Insert Modes Work?

Here’s a short overview of these modes

Command Mode

  • Command Mode is the default mode you see when launching Vi or Vim.
  • In Command Mode, you issue commands for tasks such as navigating the file, searching, replacing, deleting, copying, and pasting text.
  • You use various key combinations to move the cursor, delete characters, and copy or paste text.
  • For instance, press x to delete a character and dd to delete an entire line. Similarly, press yy to copy a line and p to paste.
  • You can also use Command Mode to save and quit the editor or switch to other modes.

Insert Mode

  • You can directly insert and edit text in Insert Mode, similar to most text editors.
  • To enter Insert Mode from Command Mode, press the i key (lowercase ‘i’).
  • To exit Insert Mode and return to Command Mode, press the Esc key.

Understanding how to save files is vital to making the most of the Vi and Vim editors. Here’s a step-by-step guide to help you navigate this challenge:

Enter the Command Mode

Launch Vi and Vim, and make sure you are in the Command mode by pressing the Esc key. This mode allows you to enter various commands to manipulate the file. 

Begin by initiating a new text document using Vim:

# vim testexample_file.txt

How to quit the Vi Editor vim test file

Transitioning Between Insert Mode and Command Mode

  • Insert Mode to Command Mode: Press Esc to exit the Insert mode and return to Command mode. Any changes made in the Insert mode are now accessible for further editing or saving.
  • Command Mode to Insert Mode: Press i (lowercase ‘i’) in Command mode to switch to Insert mode at the current cursor position. This allows you to start typing and editing the text.

Saving a File in Vi/Vim

Now, let’s get to the actual process of saving files. We have covered the three most common scenarios that users encounter while working with files:

Saving Changes Without Exiting

Use the following command sequence to save the changes without exiting the editor.

  • Press Esc to enter the Command mode. 
  • Type : to launch the prompt bar.
  • Enter w to save changes to the file without exiting the editor.

Saving Changes and Exiting

Use the following command sequence to save the changes and exit the editor

  • Press Esc to enter the Command mode. 
  • Type : to launch the prompt bar.
  • Now enter wq (write and quit) or x to save changes to the file and exit the Vim editor. 

How to quit the Vi Editor

Saving Files with another name

Use the following command sequence to save the current file with a new name. For the purpose of this demonstration, we’ll save the current file with a new name (testfile2).

  • Press Esc to enter the Command mode. 
  • Type : to launch the prompt bar.
  • Type w and the new name (testfile2)

How to quit the Vi Editor

If you’re a beginner, we don’t recommend using Vi/Vim as your primary code editor. We recommend going through our list of the 10 best Linux IDE you should try out in 2023.

How to Exit Vi/Vim Editor

You might want to exit Vi and Vim without saving the changes to the file. The process is really simple and has the following steps:

  • Press Esc to enter the Command mode. 
  • Type : to launch the prompt bar.
  • Type q! (quit without saving) to exit without saving changes to the file.

How to quit the Vi Editor

how to save exit from vi or vim editor

Conclusion

Saving and exiting files in the Vi and Vim editors can initially appear complex, especially for beginners. However, with the proper commands and a bit of practice, you’ll find that managing files in these popular text editors is indeed a breeze. 

If you’re looking for a robust server for your Linux-powered projects, RedSwitches offers the best dedicated server pricing and delivers instant dedicated servers, usually on the same day the order gets approved. Whether you need a dedicated server, a traffic-friendly 10Gbps dedicated server, or a powerful bare metal server, we are your trusted hosting partner.

FAQs

Q. How do I quit the Vi editor and save files in Vim efficiently?

To quit the Vi editor and save files in Vim, you can use the following command sequence:

Esc >> : >> wq

Q. How can I save a file in Vim?

To save a file in Vim, use the command :w or: write

Q. What is the command to save and quit in Vim?

The command to save and quit in Vim is :wq or :x.

Q. How do I quit the editor without saving the file in Vim?

To quit the editor without saving the file in Vim, you can use the command :q! or :quit!.

Q. How do I open a file in Vim?

To open a file in Vim, you can use the command : vim filename

Q. What are the different modes in Vim?

Vim has different modes, such as Normal mode, Insert mode, Visual mode, and Command-line mode.

Q. How do I quit the Vim editor?

To quit the Vim editor, you can use the command :q or :quit.

Try this guide with our instant dedicated server for as low as 40 Euros