The Wayback Machine - https://web.archive.org/web/20240926215310/https://www.geeksforgeeks.org/how-to-save-a-file-in-vi-vim-editor-quit/
Open In App

How to Save a File in Vi / Vim Editor & Quit

Last Updated : 25 Sep, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In this article, we will cover how to save a file in vi editor. First, we look at Linux and Vi editors why we use them, and what their are features then we deep dive into the topic of saving a file and quitting the editor along with the other options, we understand the different commands with an example.

What is Linux?

Linux was developed by Linus Torvalds in 1991 as a hobby project. It is an open-source (source code that can be used by anyone freely) kernel that is most popular and widely used in the industry as well as in personal systems. There are various operating systems based on the Linux kernel, some of the popular Linux distributions are Ubuntu, CentOS, Red Hat, Debian, and Kali Linux.

What is Vi Editor

Vi Editor is a widely used text editor in Unix/Linux systems and is known for its efficiency and flexibility. Vi editor was developed in 1976 by Bill Joy and later in 1991, an improved version of Vi editor was released which is known as VI Improved (VIM).

Modes in Vi Editor

There are two modes in Vi Editor:

  1. Insert Mode: In this mode, you can directly input and edit text. When in Insert Mode, you can type characters, and add, modify, or delete text content as needed. You can initiate Insert Mode by pressing keys such as ‘i‘ (to insert before the cursor) or ‘a‘ (to insert after the cursor). There are also variations like ‘I‘ (to insert at the beginning of the line) or ‘A‘ (to insert at the end of the line). To exit Insert Mode and return to Command Mode, simply press the ‘Esc‘ key.
  2. Command Mode: In Command Mode, you navigate and manipulate text without directly typing characters. This mode is designed for controlling the editor’s behavior and performing various editing tasks efficiently. You can move the cursor, delete text, copy and paste, search for text, and execute a wide range of other editing operations using keyboard commands. To transition from Insert Mode to Command Mode, press the ‘Esc‘ key. The clear distinction between these two modes in Vi is a foundational feature that enables users to edit text swiftly and effectively while minimizing the need for complex keyboard shortcuts.

How to Save a file in Vi/Vim Editor & Quit?

To execute any command you must have to be in command mode to enter command mode if you are in insert mode press Esc.

1. Save a file in Vi/Vim Editor

There are 3 ways to save a file in the vi/vim editor which are mentioned below:

  1. :w – Save changes but do not exit.
  2. :wa: – Save all open multiple files without quitting.
  3. :w filename – Save the file with a different name and continue editing the current file.

Example:

Step 1: Open a file or create using the vi editor

Step 2: Enter in command mode by pressing Esc if you are in insert mode

Step 3: Use any of the above commands (w, wa, w [filename]) to save the file, in the below screenshot we have used :w [filename] to save the file using a new name in the newFile.txt file.

Screenshot-from-2023-09-20-12-08-46

2. Quit Vi/Vim Editor

There are four ways to quit the editor:

  1. :q – To quit the editor
  2. :q! – To force quit useful when changes are made to a file but the user doesn’t want to save them
  3. :qa! – Quit all open files without saving changes (force-quit)
  4. ZQ (Shift + ZQ) – Quit without saving changes

Example:

Step 1: Open a file or create using the vi editor

Step 2: Enter in command mode by pressing Esc if you are in insert mode

Step 3: Use any of the above commands (q, q!, qa or ZQ) to quit the editor in the below screenshot we have used:q to quit the editor

Screenshot-from-2023-09-20-12-04-12

3. Save the file and Quit Editor

There are 3 ways to save a file and quit the vi/vim editor which are mentioned below:

  1. :wq – Save changes and exit the editor
  2. ðŸ˜Ą – Same as :wq used for save changes and quit
  3. ZZ (Shift + ZZ) – Save changes and exit

Example:

Step 1: Open a file or create using the vi editor

Step 2: Enter in command mode by pressing Esc if you are in insert mode

Step 3: Use any of the above commands (wq, x, ZZ) to save the file and quit the editor in the below screenshot we have used :wq command to save the file and quit the editor

Screenshot-from-2023-09-20-00-52-10

All the above-mentioned commands are mentioned below in a tabular format:

Commands 

Description

:q

Quit

:q!

ZQ (Shift + ZQ)

Quit without saving changes i.e. discard changes

:qa

Quit all open files without saving changes (force-quit)

:w

Save Changes

:wa

Save changes to all the open multiple files

:w [filename]

Save the file with a different name

:wq

ZZ (Shift+ZZ)

ðŸ˜Ą

Save changes and exit the editor

Conclusion

In this article we covered different ways to save a file, to quit the editor, and commands that do both save and quit along with shortcut keys. Apart from that a basic vi editor guide is also appended so that new users get familiar with the editor.



Similar Reads

Nano vs VIM editor – What’s the difference between nano and vim editors?
In this article, we will understand the difference between nano and vim editors in Unix/Linux. Before diving into this topic, we will first understand what exactly nano and vim editor is. Nano Editor in the Linux System. Nano is a simple, user-friendly text editor that improves user experience. Its GUI (Graphical User Interface) makes it easy to us
3 min read
Quit VI Editor Without Saving Changes in Linux
In this article, we will cover how to quit the vi editor without saving your changes in Linux. First, we look at what Linux and VI editor and why we use them, what are its features and how can we use them. LinuxLinux was developed by Linus Torvalds in 1991 as a hobby project. It is an open-source (source code that can be used by anyone freely) kern
4 min read
Encrypting Files Using vim editor in Linux
Vim, the popular text editor comes with a built-in feature to encrypt files with a password. Vim uses algorithms like Blowfish to encrypt files. It is faster and convenient than some other utilities available to do so. To get started, all you need is a system with a full version of vim installed. Some Linux distributions are shipped with a minimal
3 min read
How to comment multiple lines in Vim Editor in Linux
Vim Editor in Linux is one of the most used text editors for writing scripts, codes, and textual data. There are different types of features offered by the Vim Editor through which we can create the text more comfortably. Various types of shortcuts, modes, and bindings are available in Vim Editor. While developing a code, we usually need to comment
5 min read
How to undo in Vim editor in Linux
Vim (short for Vi Improvised) is a popular text editor mainly used in Linux and Unix-based operating systems but due to having a CLI (command line interface), it might be difficult for new users to make use of basic features such as undo in Vim. So in this article, we will learn different ways and types to undo changes in Vim Editor in Linux. Like
3 min read
How to Display Line Number in vim editor in Linux
In this article, we will cover how to make the Vim editor display or hide line numbers. First, we look at what Linux and VIM editors and why we use them, are its features and how can we use them Follow the basic VIM editor guide to get familiar with the editor followed by various options to make the vim editor display or hide line numbers and how w
5 min read
Searching and Replacing in Vim Editor
Vim is an excellent version of the legacy Vi editor for terminals. It is a complete command line-based text editor that has been the go-to choice for IT professionals for decades. Vim is the Vi-IMproved version of the original text editor which contains many modern features. In this article, we shall explore one such feature, the functionality to s
7 min read
How to Hide Line Numbers in Vim Editor
In this article, we will cover how to hide line numbers in the Vim editor. First, we look at what Linux and VIM editors and why we use them, what are its features and how can we use them followed by various options to make the Vim editor hide line numbers and how we can change the default settings of Vim editor to hide line numbers. LinuxLinux was
3 min read
Vi vs. Vim: Choosing the First Right Text Editor
Vi and Vim are both text editors used primarily in Unix-like operating systems, although they are available on various platforms. They both are powerful text editors known for their efficient text manipulation and keyboard-driven interface. Whether you should learn Vi before Vim depends entirely on you, your requirements, and your specific goals an
3 min read
Enabling Syntax Highlighting in Vim Editor
In this article, we will cover how to highlight syntax in Vim editor. First, we look at what Linux and VIM editors followed by syntax highlighting in the Vim editor with examples and screenshots we will also look at different color schemes available and how we can change the default settings of Vim editor to enable syntax highlighting by default. V
3 min read
The Six Modes of Vim Editor
Vim, short for "Vi Improved," is a highly efficient and powerful text editor that is favored by many developers and system administrators. One of Vim's distinguishing features is its unique approach to editing, which involves six distinct modes. Understanding these modes is crucial for mastering Vim and unleashing its full potential. In this articl
6 min read
How to Highlight Text in Vim Editor in Linux
In this article, we will cover how to highlight text or syntax in Vim editor. First, we look at what Linux and VIM editors followed by syntax highlighting in the Vim editor with examples and screenshots we will also look at different color schemes available and how we can change the default settings of Vim editor to enable syntax highlighting by de
3 min read
How to Delete Multiple Lines in vim Editor in Linux
Vim Editor is a Linux terminal-based editor that has various advanced features for editing, but due to its terminal behavior, it is quite difficult to handle some of the operations in Vim. While making a text file, we often need to delete some unwanted lines. If the document consists of more unwanted lines, then deleting them one by one will be a t
7 min read
Editing Mode | Insert Mode in Vim Editor
This article will cover how to enter editing mode in Vim editor. First, we look at what Linux and VIM editors are and why we use them, followed by commands to enter editing mode in Vim editor along and examples and screenshots. LinuxLinux was developed by Linus Torvalds in 1991 as a hobby project. It is an open-source (source code that can be used
5 min read
How to Edit Multiple Files in Vim Editor in Linux
Vim, short for "Vi Improved," is a highly configurable and powerful text editor built upon the foundation of Vi. Like its predecessor, Vim offers robust support for editing multiple files simultaneously, making it an indispensable tool for developers, system administrators, and anyone working extensively with text files in Linux environments. In th
4 min read
Vim Editor in Linux
If you’re new to the world of Linux, one of the first things you’ll encounter is the command line interface. And if you’re going to be working in the command line, you’ll inevitably come across various text editors. Among them, Vim stands out as a powerful, yet notoriously complex, text editor. In this guide, we’ll take you through everything you n
7 min read
How to Copy Paste in Vim Editor
Copying and pasting in Vim Editor is an essential operation for efficient text editing. Vim offers several methods to copy and paste content, including visual mode selection, yanking, and putting. Understanding these techniques can significantly enhance your productivity when working with Vim. In this article, we'll explore these methods in detail,
3 min read
How to Save and Exit in Nano Editor in linux
Saving and exiting in the Nano text editor in Linux is a fundamental skill for anyone working with text files in the terminal. Nano is a user-friendly and straightforward text editor, making it an excellent choice for both beginners and experienced Linux users. Whether you're editing configuration files, writing scripts, or taking notes, knowing ho
5 min read
Vim - Go to End of File
In this article you will get to know many ways to go to the end of any file opened using Vim (Vi Improved) which is a open source text editor. You will get to know what vim is and many shortcuts and key combinations in the entire article to navigate to the end of the file and after that, you can decide which is best suitable for you and which will
5 min read
AMP Text Editor for Linux Terminal
Amp is a fully-featured and lightweight text editor inspired by the Vi/Vim text editor. Amp editor is a terminal-based editor, and it is written in the Rust language. It provides the all fundamental features required for a modern text editor. Amp does not need any configuration it comes with all configuration, and it does not use any plugins. And i
3 min read
Installing and Configure vim Plugin in Ubuntu
There are several plugin managers like Vundle, Pathogen, Vim-plug, VAM which are used to install vim plugin, These plugin increase speed, productivity, and look and feel of vim text-editor but here we will use the Pathogen plugin manager. Prerequisites : VIM editor This is the command to install vim editor in the ubuntu. sudo apt-get install vim Pl
1 min read
Customising vim From Scratch Without Plug-Ins
Prerequisite: Vi/Vim text editor Vim is a modal(mode based), hyper-efficient text editor. Customization of vim can happen at two levels: System-level: Vim will be customized for every user. To customize vim at system-level changes are made in /etc/vim/vimrc file.User level: Vim will be customized for a unique user. To customize vim at user level ch
2 min read
How to Convert DOS line endings to Linux line endings using Vim
DOS and Unix/Linux file formats differ slightly such that the ASCII characters used at the line endings are different. Unix file format uses an LF character to end a line, LF stands for LineFeed, which means moving the cursor down. Unix-based systems and Mac OS X & later use this format. While the DOS file format uses a CR character before the
2 min read
Difference Between Vim VS Nano VS Emacs
Vim Text Editor Vim is the default text editor that is installed already in most Linux distributions. It is one of the most powerful and lightweight text editors that deals with a lot of work purposes, from basic file editing to software development by providing an Integrated Development Environment(IDE). Vim was derived from the Vi text editor whi
5 min read
Splitting Vim Screen Horizontally and Vertically in Linux
Vim, short for "Vi IMproved," is an open-source text editor which is considered to be one of the most powerful and popular text editors for Linux. Vim is a powerful and highly configurable text editor which is known for its extensive feature set. It is available for free download and use across various platforms, including MacOS, Linux, and Windows
5 min read
Creating a C++ template in vim in Linux
Vim allows users to create specific templates for files ending with certain extensions. General steps to create a template: step 1: Create a template in ~/.vim/templates/ directory.A template is a skeleton content that can be fit into all the files ending with a certain specific extension. step 2: Add commands to ~/.vimrc file that directs vim to p
2 min read
Vim Text Buffers in Linux
When it comes to text editing we all have heard of the VIM editor which is widely used in Linux systems. Vim is powerful and versatile which has made it the go-to choice for many linux users and programmers as well. Vim offers many features that set Vim apart and one of them is the text buffer system, which allows the user to work with multiple pie
3 min read
How to Search in Vim in Linux
For many Linux users and developers, Vim is one of the most efficient and versatile text editors. Among its strong points is its search function, enabling users quickly to locate and re-arrange text within files. Here we will look at different ways and commands to search smartly in Vim on a Linux system. Creating a Vim text File:Step 1: Open your t
5 min read
VI (Vim) for Windows Users
The vi editor, or the original Vi, is a text editor that was developed primarily for Unix-like operating systems. The orginal Vi editor is not available for Windows operating system out of the box so as an alternative, the most commonly used Vi implementation for Windows is called as "Vim", short for "Vi IMproved". Vim is a powerful and highly conf
3 min read
How to Exit Vim in Linux?
Vim, short for Vi Improved, is a powerful and ubiquitous text editor commonly found on Linux systems. Renowned for its efficiency and versatility, Vim boasts a wide array of features, making it a favorite among developers, system administrators, and power users alike. However, for the uninitiated, Vim's modal editing system and unconventional inter
3 min read