DevOps

How to Save a File in Vim/Vi Editor?

Introduction

A free text editor for Linux and Unix operating systems get referred to as Vim or Vi Improved. This is a highly effective and reliable tool for text editing as well as writing. It functions best as a standalone application in a GUI or through a command-line interface.

When saving a file in Vim/Vi editor, many users regularly don’t understand the basic steps. But don’t worry, you will discover everything you need in this article. With the essential save and exit commands, you can begin mastering Vim. This blog post will teach you the proper way how to save a file in Vim/Vi and close the editor.

System Requirements

There are certain system requirements that should get fulfilled as part of any technology. Your system requires the following for the Vim editor:

  • A Linux-powered computer.
  • A user who has sudo access.
  • Easy access to a command line or terminal.
  • You can use the default Vi if you don’t have Vim installed.

Vim Modes

You are in standard mode when you start the Vim editor. Learning to navigate the file is possible in this mode by using Vim commands.

To get started typing, tap the i key to open the insert mode. In this mode, you can add and erase characters just like you would in a typical text editor.

Simply hit the Esc key to return to the standard mode from a different mode.

Also Read: How to Install Pip on Windows? (Step-by-Step Tutorial)

Basic Commands for Saving a File in Vim/Vi Editor

Basic Commands for Saving a File in Vim/Vi Editor

A. Entering Command Mode

You can launch your terminal and enter the name of the file you would like to edit or create. You can follow this step to save file in vim with command.

  • vim filename
Entering Command Mode

To open a file there is an additional way. You can launch the editor and enter:e file_name, in this the file_name will be the name of the file you want to open.

B. Saving a File with w Command

When you want to save a file in Vi you can use the command prompt:w. 

Also, when you want to save a file without quitting the editor, you can go back to the standard mode. You can accomplish this by hitting Esc, typing:w, and pressing enter.

  • Press the Esc key.
  • Type :w
  • Hit enter
Saving a File with w Command

Additionally, there is an update command: up that only writes the buffer to the file if there are still unsaved changes.

To rename the file, type: w new_filename and press Enter.

C. Saving a File and Exiting Vim with wq Command

Using the Vim editor, you can save a file and close it by typing: wq.

To save the file and quit the editor at once, Press Esc to return to normal mode, type: wq!, and press Enter 

  • Press the Esc key.
  • Type :wq
  • Hit enter 
Saving a File and Exiting Vim with wq Command

Vim can also get terminated with the:x command after saving a file.

The disparity between these two commands is that:x only saves changes that haven’t been saved before writing the buffer to the file. wq, on the other hand, always writes the buffer to a file and updates the file conversion time.

D. Exiting Vim without Saving Changes with q! Command

By pressing Esc, entering the command q!, and clicking Enter, you can leave the editor without saving your changes.

  • Press the Esc key.
  • Type :q!
  • Hit enter
Exiting Vim without Saving Changes with q! Command

Also Read: How to Delete/Remove a Directory in Linux?

Advanced Commands to Save Files in Vi/Vim

Advanced Commands to Save Files in Vi/Vim

A. Saving a File with a Different Name using w filename Command

The following is the approach for saving a file in vim/vi and exiting the editor:

  • Launch the terminal program in Linux
  • Then, when a file is open in Vim or Vi, type: vim filename
  • You can next press the Esc key, type :w, and hit the Enter key to save a file 
  • You can also save a file and quit Vim / Vi by pushing the Esc key, typing :x, and hitting the Enter key
Saving a File with a Different Name using w filename Command

B. Saving a File in Read-Only Mode with view Command

Saving a File in Read-Only Mode with view Command

C. Saving a File Automatically with wq! Command

Once you’ve saved any changes you’ve made, you can exit and save file in vi or vim editor. To do this you can follow:

  • Press the Esc key if you are now within the insert or append mode.
  • Hit the: (colon key). On the screen’s lower left side, next to a colon prompt, you can view the cursor.

You can enter the following command (just type:x and hit Enter key):

Saving a File Automatically with wq! Command

OR

type :wq!

Push the ENTER key.

Saving a File Automatically with wq! Command

By doing this it will close the editor and save any changes you’ve made to the document.

Summing Up

In this article, we have aimed to highlight the key process of saving files in Vim/Vi with a different set of approaches. We understand that to gain access to the information you will need to precisely utilize certain actions yourself. When you become more comfortable with keyboard shortcuts, you will more easily recognize the importance of Vim. Globally, many users are now taking advantage of this effective tool to make things simpler.

Arpit Saini

He is the Chief Technology Officer at Hostbillo Hosting Solution and also follows a passion to break complex tech topics into practical and easy-to-understand articles. He loves to write about Web Hosting, Software, Virtualization, Cloud Computing, and much more.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *