How To Save A File In Vim / Vi And Quit The Editor - Linuxize
Có thể bạn quan tâm
Vim is the text editor of choice for many users that spend a lot of time on the command line. Unlike other editors, Vim has several modes of operation, which can be a little intimidating for new users.
Vim or its precursor Vi comes preinstalled on macOS and almost all Linux distributions. Knowing the basics of Vim will help you when you encounter a situation where your favorite editor is not available.
This guide explains how to save a file in Vim / Vi and quit the editor.
Vim Modes #
When you launch the Vim editor, you’re in normal mode. In this mode, you can use vim commands and navigate through the file.
To type a text, you need to enter the insert mode by pressing the i key. This mode allows you to insert and delete characters in the same way you do in a regular text editor .
To go back to the normal mode from any other mode, just press the Esc key.
Open a File in Vim / Vi #
To open a file using Vim, launch your terminal and type vim followed by the name of the file you want to edit or create:
vim file.textAnother way to open a file is to start the editor and type :e file_name, where file_name is the name of the file you want to open.
Save a File in Vim / Vi #
The command to save a file in Vim is :w.
To save the file without exiting the editor, switch back to normal mode by pressing Esc, type :w and hit Enter.
- Press Esc
- Type :w
- Press Enter
There is also an update command :up, which writes the buffer to the file only if there are unsaved changes.
To save the file under a different name, type :w new_filename and hit Enter.
Save a File and Quit Vim / Vi #
The command to save a file in Vim and quit the editor is :wq.
To save the file and exit the editor simultaneously, press Esc to switch to normal mode, type :wq and hit Enter.
- Press Esc
- Type :wq
- Press Enter
Another command to save a file and quit Vim is :x.
The difference between these two commands is that :x writes the buffer to the file only if there are unsaved changes, whereas :wq always writes the buffer to file and updates the file modification time.
Quit Vim / Vi without Saving the File #
To exit the editor, without saving the changes, switch to normal mode by pressing Esc, type :q! and hit Enter.
- Press Esc
- Type :q!
- Press Enter
Conclusion #
In this guide, we have shown you how to save a file in Vim and exit the editor. If you are new to Vim, visit the Open Vim site where you can practice Vim with an interactive tutorial.
Feel free to leave a comment if you have any questions.
terminal vimTừ 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 Vi / Vim Editor & Quit - PhoenixNAP
-
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