How To Save A File In Vi / Vim Editor & Quit - PhoenixNAP
Có thể bạn quan tâm
Introduction
Vim (Vi IMproved) is an open-source text editor for Unix and Linux systems. It is a powerful and stable tool for editing and writing text, both in the command-line interface and as an independent application in a GUI.
Take the first step in learning Vim with the basic save and exit commands. Follow this guide and learn how to save a file in Vi/Vim and exit the editor.
Prerequisites
- A system running Linux.
- A user with sudo privileges.
- Access to a terminal/command line.
- Vim installed (alternatively, use the default Vi).
Using Vim Modes
When Vim launches, files are opened in command mode by default. This means that you can move around and edit the file, but cannot insert new text.
All alphanumeric keys in this mode are equal to commands, and pressing them will not display their value on the screen. For example, pressing the letter w will move the cursor one word forward.
To type text, you must be in insert mode. To switch to insert mode, press the i key. Now you can type text into a file.
To switch back to command mode, press the Esc button.
How to Save Changes in Vim
Vim offers different commands to save changes to a file, depending on whether you need to exit the text editor or not. Below are examples demonstrating how to save and exit, save without exiting, and exit without saving.
Start by opening a new text file using Vim:
vim example_file.txtNext, press i to enter insert mode and add some text.
Vim Save and Quit Command
To save a file and exit Vim, do the following:
1. Switch to command mode by pressing the Esc key.
2. Press : (colon) to open the prompt bar in the bottom left corner of the window.
3. Type x after the colon and hit Enter. This will save the changes and exit.
Alternatively, a command that will also save a file and exit the text editor is:
:wqIn which case the w stands for write (save) and q stands for quit.
How to Save a File in Vi / Vim Without Exiting
To save a file without exiting in Vim:
1. Switch to command mode by pressing the Esc key.
2. Type : (colon). This will open the prompt bar in the bottom left corner of the window.
3. Type w after the colon and hit Enter. This will save in Vim the changes made to the file, without exiting.
You can also rename an existing file by adding the new name after the commands.
:w [newfilename]In the picture below, we renamed the existing example1.txt into newexample.
Exit Without Saving Changes in Vi / Vim
To exit Vim without saving changes:
1. Switch to command mode by pressing the Esc key.
2. Press : (colon) to open the prompt bar in the bottom left corner of the window.
3. Type q! after the colon and hit Enter to exit without saving the changes.
The q! quits the text editor.
Conclusion
This article explained how to save files in Vi / Vim, with multiple command options.
The more comfortable you feel using keyboard shortcuts, the more you will understand why Vim earned its worldwide reputation. Next learn how to copy and paste in Vim, and how to go to the end of the file!
Từ khóa » Vi In Linux Save
-
An Introduction To The Vi Editor | Enable Sysadmin - Red Hat
-
How To: Vi / Vim Save And Quit The Editor Command - NixCraft
-
How To Save A File In Vi / Vim Text Editor - NixCraft
-
How To Save A File In Vim / Vi And Quit The Editor - Linuxize
-
Using Vi? Here's How To Open A File Then Save And Quit - MakeUseOf
-
How To Save A File In Vi / Vim Editor In Linux - Tecmint
-
Saving Changes And Quitting Vi
-
How To Save & Quit In VIM Or VI | OSXDaily
-
Saving Files And Quitting Vi
-
How To Save A File In Vim/Vi? - LinuxTect
-
VI Editor With Commands In Linux/Unix Tutorial - Guru99
-
How To Save A File In Vi / Vim And Quit Editor - TecAdmin
-
How To Exit (quit) Linux Vi Editor With Or Without Saving Changes Step ...
-
How To Save A File In Linux After Using The Vi Command