How to open files with vim
Vim is the text editor of choice for users who frequently use the command line. Unlike other editors
Vim is the text editor of choice for users who frequently use the command line. Unlike other editors, Vim has several modes of operation, which can be a bit tricky for new users, so it's no surprise to give it a synonym for "Editor of God".
Vim or its predecessor Vi comes preinstalled on all MacOS/Linux distributions. Knowing the basics of Vim will help you in situations where your favorite editor isn't available.
Vim is a text editor developed from vi. Its code completion, compilation and error jumping and other convenient programming functions are particularly rich, and are widely used by programmers. It is tied with Emacs as the favorite editor for users of Unix-like systems.
If your computer is running a Debian based Linux distribution such as Ubuntu, Linux Mint etc. You can run sudo apt install vim
the command to install vim.
If you are using a Redhat based Linux distribution such as CentOS, Fedora etc. You can run sudo yum install vim -y
the command to install vim.
install vim
sudo apt install vim
sudo yum install -y vim
Vim mode
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.
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.
edit file
Now we know how to open files with vim. By default, when a file is opened with vim, the mode vim enters is command mode, in which the file is not allowed to be edited.
If you need to edit the file, you need to enter insert mode or replace mode. Usually we edit files in vim's insert mode.
To enter vim's insert mode, you need to press a i
key to enter insert mode, and then you can edit the file as you would with a normal editor.
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.
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.
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.