Experienced Linux users very often use the terminal, because it allows them to do a lot of things more efficiently. For example, we are constantly editing configuration files when we need to configure something in the system or any service. It may be application settings, system settings, any data, or regular text files.
There are multiple text editors for Linux that can work in the terminal. Most often, beginners use the nano editor. In most articles on this website I recommend using Vim editor. Nano editor is not convenient and has less functionality. I specifically do not use Nano in my articles.
There is a much better editor, and it is Vim. It supports fast text navigation, convenient editing, commands that can help to change editor and appearance settings, execution CLI commands in the editor, and plugins that can help extend functionality. But it is a bit difficult for beginners and very unusual.
In this article, we will dive into how to use Vim text editor, look at the basics of working with this editor, and its main commands.
Table of Contents
How to Edit Text Files in Vim
I prefer to use Vim for editing files on this website, but a lot of users have trouble with it. This article was written to help them find out how to use this editor. You can read all that you need above, but if you here find how to quickly edit a file and quit, I will show it in this section without any theory. Use this command to open a required file:
sudo vi /path/to/file
Then, press i for switching into Edit mode and edit what you want as well as you do this in other editors. After you done this, press Esc button on your keyboard for switching into command mode and type :wq for saving and quit. That's it!
Minimal Basics
At the moment there are two versions of the editor - vi and vim. Vim stands for Vi Improved. It is a new version that brings a lot of improvements. This version is used in most Linux distributions. So when I write vi, I assume that you use vim.
Vim text editor can work in two different modes. This is its main feature. The first mode that is used by default when you open the editor is the command mode. In this mode, you can enter commands and use symbol keys to control the editor, for example, move the cursor or copy/paste text. The second mode - is regular text editing. This mode works as well as text editing in Nano. You can use Esc key to switch between command modes. There are several ways to switch to edit mode. You can press i, o, or a key. If you want only to know how to save and exit in Vim, scroll down. If you want to know how to use Vim - continue reading.
Vim has a short tutorial that is embedded in the editor. All tasks will take about half an hour to complete. You can take this tutorial after reading the article, it will help you put your knowledge into practice. Vim has numerous commands and hotkeys and it is impossible to remember them all without practice. Use this command to run the tutorial:
vimtutor
You don't have to do it now. The article contains all the required basic information. You will be able to use Vim after you have read it, so you can take the tutorial later.
How to Use Vim Editor
Let's have a look at the options of the program and its syntax. The syntax is very simple:
vim options /path/to/file
Or:
vi options /path/to/file
If you have installed the vim package vi command will be automatically aliased to vim. So there is no difference between them. If you run vim without specifying a file, the program will create an empty file.
Here is the most useful options that you can use:
- +number - move the cursor to the specified line;
- +/template - search by template and move the cursor to the first result;
- "+command" - execute the command after start;
- -b - enable binary mode;
- -d - enable searching differences mode, you have to specify multiple files;
- -g - enable graphical mode, will run gVim if it is installed;
- -n - disable autosaving feature, makes unable to recover changes after failure;
- -R - enable read-only mode;
- -w - save a log of all your actions into a file;
- -x - encrypt the file before saving;
- -C - run program in Vi-compatibility mode;
Options are interesting, but the command mode has more features. Let's have look at it.
1. Cursor Moving
In the command mode, you can move the cursor across the text and perform actions using letter keys. This mode is enabled after the editor starts. Most commands consist single symbol, usually a letter. But many commands can have parameters. For example, for moving commands you can type a number in front of it to specify how many times the command must be repeated**.** It may be difficult to understand for beginners that numbers are used as arguments for commands. Here are commands that you can use to move the cursor:
- h - one symbol to the left;
- l - one symbol to the right;
- j - one line down;
- k - one line up;
- w - one word to the right;
- b - one word to the left;
- H - go to the end of the screen;
- G - go to the end of the file;
- gg - go to the start of the file;
- <number>G - go to the specific line.
You can run the editor and try how it works. Start practicing from h, l, j, and k commands. Many users use them permanently, not only in Vim, because it allows you to navigate through the file without taking your hands off the keyboard. Other commands are not so useful, so you can learn them later if you will need them.
Very often we need to move the cursor to the start of the current line or the end of it. Vim also has commands that can help do this. But these are not letters. You can use ^ command for moving the cursor to the start of the line and $ for moving to the end. Notice that the commands require two keys to be pressed 4 or 6 and Shift.
2. Switching to Edit Mode
Let's move to the editing feature. You can use these commands for switching the editor to edit mode:
- i - insert text at the cursor position, a new text will be inserted before the current character;
- I - start editing from the start of the current line;
- a - append text at the cursor position, but a new text will be inserted after the current character;
- o - insert a new line after the current line and start editing;
- O - insert a new line before the current line and start editing;
These commands support numeric parameters that you can use to specify how many times the command should be repeated.
When you are done, you can switch back to the command mode using Esc button.
3. Delete Chars and Lines
You can delete text in the command mode. It may be even more convenient than in the edit mode. Here are commands that can help do this:
- d - delete the symbol under the cursor;
- dd - delete an entire line;
- D - delete symbols from the start of the line to the cursor position.
These commands work a little differently. They also support parameters for repeating, but if you press the command button nothing will happen. You also should specify the direction using cursor moving buttons and the number of symbols or lines that should be deleted. For example, if you want to delete one char to the right, type dl. If you want to delete two chars after the cursor, type d3l. For deleting three lines down, use d3j.
4. Replacing Chars
You can also replace chars in the command mode. Two commands allow to do this:
- r - replace the current symbol;
- R - replace multiple symbols.
The first command will replace one char and return to the command mode. The second command allows replacing all chars unless you press Esc button on your keyboard.
5. Undo and Redo
In regular editors, we often use Ctrl+Z to revert changes. Vim editor has an internal history of changes. it is available in the commands mode. If you want to undo the last change, just press u. Note, that each next pressing of this button will undo each change that was made in command mode. For example, if you have deleted a char using d command, it will bring it back. But if you switched into edit mode and make a lot of changes and then switch back to the command mode it will be considered as one change. If you suddenly reverting something important, you can redo your undo. Just press Ctrl+r.
Also, you can reapply the latest action using . command. Here few commands for undo and redo actions:
- u - undo the latest action;
- U - undo the latest action into the current line;
- . - redo the latest action;
- Ctrl+r - cancel the latest action.
6. Command Line
Vim has a powerful command line that can be accessed in the command mode by typing ":" symbol. Here are available commands for saving files, quitting, searching, appearance settings, and interaction with the external shell. Let's have a look at the most used commands:
- :w - save changes;
- :saveas - save into a new file;
- :q - quit;
- :q! - quit without saving;
- -e file - read the contents of the file into the cursor position;
- -r file - paste the file contents into the next line;
- :r! - execute a shell command and paste output into the editor;
- :set variable=value - set the value of the variable;
- :buffers - show files that are opened;
- :reg - show clipboards content.
At this moment you know all the basic information that is required to edit files. But there are a lot of more interesting things. Next, I will show how to work with a clipboard, copy and paste text and use search features.
7. Selection and Clipboard
Vim supports text selection without using a mouse. So you can copy and paste text even in TTY environments without GUI. When you are in the command mode press v button to enter selection mode. Now you can use cursor moving buttons (h,j,k,l) to select required chars or lines:
After this, you can apply any actions to the selected text. For example, delete it using d command. To reset the selection press Esc button. But that is not it. Vim has an internal clipboard, that can be used for cutting and copying text. The clipboard consists of a few registers. By default, all commands will work with the default register. Here are commands for working with the clipboard:
- y - copy into the clipboard;
- yy or Y - copy a whole line into the clipboard;
- x - cut into the clipboard;
- p - paste from the clipboard.
Commands for copying and cutting work as well as commands for deleting text. You can specify the number of symbols and the direction, or just select the text. Note, that each instance of Vim has its own dedicated default clipboard register and it is not connected to the system clipboard. So you can't copy chunks of text between programs without additional actions or configuration.
If you have to copy data into the system clipboard, you should ensure that your Vim installation supports it. Just execute this command:
vim --version | grep clip
If this ability is supported, you will see +clipboard or +xterm_clipboard. Let's come back to registers. Vim has a lot of registers, and one of them is linked to the system clipboard. It is "+ register. You can see what is in the registers with this command:
:reg
The register with the name "" is used by default. If you want to copy something into the system register, type the register name before pressing y key. For example, select text, press "+ and then press y. Note, that you should press Shift+' and Shift+= to achieve it.
As you can see it is pretty difficult. There is simpler way. You can copy the data in the default register as usual, and then move its contents into the system clipboard using Vim's command line. Just copy the text as usual, and then use the following command:
:let @+=@"
After this, you can paste copied text in any place in the system.
8. Searching and Replacing
Very often we need to search for some word or sentence in the text. Vim can help with this. If you want to find any char in the string, you can press f and type the required symbol. The cursor will be moved to its first occurrence:
If you want to search under a whole file, use command /. Type the required word or sentence after it. Vim will move the cursor to the first occurrence. Then, you can press n to find the next, or N to come back to the previous occurrence.
Here are a few commands for searching in Vim:
- f - find one char;
- / - find multiple chars;
- n - find the next occurrence;
- N - find the previous occurrence.
Also, Vim has a command for replacing strings. Here it's syntax:
:%s/<search>/<replace>/g
For example, search all occurrences of "Alice" and replace them with "Robert":
:%s/Alice/Robert/g
The colon symbol opens the command line, then the s command is used for replacing. The symbol % means that you want to replace occurrences in a whole file. The g option works here as well as in regular expressions and specifies that it is necessary to process all occurrences, not just the first.
9. Edit Multiple Files
You can open multiple files. Just pass paths to them in the command parameters. For example:
vim /etc/default/grub /etc/default/cron
Vim will remember all selected files, but open the first of them. You can switch to the next file using :n command. Use :N command to switch back. Also, you can go to a specific file using command :buffer. For example, to go to the first file use :buffer 1. Use the :buffers command to show all files that are opened:
Wrapping Up
In this article, I have explained how to use Vim editor. It has numerous features and it is impossible to cover them all in one article. But the information in this article is enough to use the editor without issues and forget about Nano. Do you use Vim? Or you prefer another editor? Tell in the comments section below!