vim

How to open files with vim

Vim is the text editor of choice for users who frequently use the command line. Unlike other editors

4 min read
By myfreax
How to open files with vim
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 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 vimthe 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 -ythe 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 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.

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.

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 ikey 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. :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.

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.

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.

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