How to save a file and exit in Vim/Vi
Vim is the text editor of choice for command line users. Unlike other editors, vim has no GUI, which may be a bit surprising to new users
Vim is the text editor of choice for command line users. Unlike other editors, Vim has several modes of operation. There's also no GUI, which might be a bit surprising to new users.
Vim or its earlier version Vi comes preinstalled on macOS and all Linux distributions. Knowing the basics of Vim will help you when you're stuck with your favorite editor.
When you start the Vim editor, you are in command mode. In command mode, you can use vim commands and browse files, set line numbers, perform search and replace, save files by pressing, and exit vim's commands.
If you need to enter text or modify the content of the file, you need to press the i
key to enter insert mode. In insert mode, you can insert and delete the contents and text of the file as you would in a common text editor.
There is also a mode in Vim called visual mode, which is rarely used by users who do not use vim often. Visual mode is mostly used when copying or pasting content. It shows where you copied or inserted files.
If you want to return to command mode from insert mode or visual mode, press the Esc
key . Here's a trick to share, if you Esc
don't return to command mode by pressing a key once, try Esc
pressing multiple times.
save the file and exit
The command to save a file in Vim and exit the editor is :wq
. While running the :wq
command vim will save the file and exit the vim editor at the same time.
If you need to save the file and exit vim, press Esc
to return to command mode, :
key , w
key, q
key , Enter
key.
Another command to save the file and exit the Vim editor is :x
. The difference from :wq
the command is that :x
the buffer is only written to the file when the changes are not saved, whereas :wq
the buffer is always written to the file and the modification time of the file is updated.
open a file
To open a file with Vim, use the vim
command followed by the name of the file you want to edit or create. vim allows opening a file that does not exist.
If the file does not exist, vim will create the file when saving. There is no need to pre-create the file with the touch command.
For example, we want to create a file file.txt
, and the file does not exist in the current directory. You can vim
open the file.txt
file by running:
vim file.txt
Another way to open a file is to start the vim editor with VIM in command mode and type :e file_name
where file_name
is the name of the file you want to open, which can be a nonexistent filename.
save document
The command to save a file in Vim is :w
. :w
To just save the file without exiting the vim editor, press the Esc
key to return to command mode and type :w
, then press Enter
enter.
If you need to save the file under a different name, press Esc
to return to command mode and then type :w new_filename
and press Enter
Enter again. At this point, the file will be saved in the current directory, and the vim editor is now in command mode.
The general and most detailed steps for vim to save a file are to press a Esc
key , then a :
key, then a w
key a key Enter
. It is also worth noting that these keys cannot be Chinese :
colons when pressed.
don't save file/force quit
To exit the editor without saving any changes/modifications made, Esc
switch to command mode by :
pressing , q
key, key, !
key , Enter
key. It is worth noting that the exclamation mark !
is an English character, the exclamation mark is not Chinese
To learn more about the basics of Vim, click the link below to read our vim tutorial.
Conclusion
In this tutorial, we showed you how to save a file in Vim and exit the editor. If you are new to Vim, visit the Open Vim website, where you can practice Vim with interactive tutorials. Feel free to comment if you have any questions.