vim

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

3 min read
By myfreax
How to save a file and exit in Vim/Vi
save a file and exit in Vim/Vi

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 ikey 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 Esckey . Here's a trick to share, if you Escdon't return to command mode by pressing a key once, try Escpressing multiple times.

save the file and exit

The command to save a file in Vim and exit the editor is :wq. While running the :wqcommand 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 Escto return to command mode, :key , wkey, qkey , Enterkey.

Another command to save the file and exit the Vim editor is :x. The difference from :wqthe command is that :xthe buffer is only written to the file when the changes are not saved, whereas :wqthe 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 vimcommand 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 vimopen the file.txtfile 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_namewhere file_nameis 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. :wTo just save the file without exiting the vim editor, press the Esckey to return to command mode and type :w, then press Enterenter.

If you need to save the file under a different name, press Escto return to command mode and then type :w new_filenameand press EnterEnter 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 Esckey , then a :key, then a wkey 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, Escswitch to command mode by :pressing , qkey, key, !key , Enterkey. 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.

vim - myfreax

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.

Related Articles