Vim is a commonly used text editor. I use it everyday to work with programs, scripts, and especially, configuration files. The following video gives a very basic introduction to Vim. Read on for more information about Vim.
Why Vim and not another editor like nano?
Well, nano is a great editor and it is installed on many distributions of Linux by default. However, you will find that Vim is extremely configurable. If you want an editor that you can customize to do exactly what you want, Vim could be it. Just be ready to spend some time learning exactly how to customize it!
How to Install Vim
If it's not already loaded on your system, you can install Vim from source (link below) or from package manager:
apt install vim
dnf install vim
zypper install vim
pacman -S vim
brew install vim(Homebrew required)
choco install vim(Chocolatey required)
- While in Vim, type
:helpfor the online help system.
- While in the terminal, type
vimtutorfor the lesson-based learning tool.
How to start a new file with Vim:
vim and that will open the editor and you can start typing. (See below for instructions on how to save the new file.)
How to open an existing file in Vim
This will open that file if it already exists. Or, it will create a new file named "newfile.txt" if it doesn't exist yet.
- Command Mode: The default mode. Designed to type commands in the bottom left corner. These commands can modify how the program works and how the file is displayed, among other things.
- Insert Mode: This is where you type your content. Press
ito get into insert mode.
- Visual Mode: Change how things are oriented, how they look, cut/copy & paste, etc... Press
vto get into visual mode.
If you are in insert or visual modes and you want to go back to command mode, simply press the Esc key.
Exiting Vim and Saving Files
:q is used to exit.
:w is used to write (or save) the file.
:wq writes and quits.
When you are done typing in a file, press Esc to go back to command mode. Then select one of the following:
- Exit Vim without saving:
- Write (save) the file without exiting:
- Write an already existing file:
- Exit Vim and write a new file:
- Exit Vim and write an existing file:
Note: You can also use keyboard shortcuts. To exit, press
Shift + ZQ. To save, press
Shift + Z + Z.
Note: If you need it, there are plenty of good Vim cheat sheets on the Internet.
There is so much more to Vim!
We have only shown the tip of the iceberg so to speak. There is seemlingly endless configuration options for Vim. For example, to add line numbers to your code enter:
:set number. To turn that off:
:set nonumber. That's just one example of a million!
Also, you can make your settings permanent if you modify the .vimrc file, which is normally stored in the user's home directory.
Really, it just goes on and on. Watch out for the Vim rabbit hole!
And also... Neovim!
Yes, there are several forks of Vim. One popular one is Neovim, (run as
nvim). Some people consider it to be more extensible and usable than Vim. That debate I leave to you.