Site icon Hostbillo Blog – Web Hosting, Devops, Tech, and more

How to Save a File in Vim/Vi Editor?

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:

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

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.

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.

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 

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.

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

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:

B. 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:

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

OR

type :wq!

Push the ENTER key.

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.

Exit mobile version