El vim text editor is an old acquaintance of every Linux user. especially those programadores. Its use is not easy and it has a learning curve, but once you master it, it will be your faithful companion when typing code. Hence, if you want to get started, we are going to give you a small guide to the main Vim commands that you will have to use in your files.
Vim is an improved version of the Vi editor that appeared on the scene in the 80s. Therefore, although Vim is an improved version of the original and which is extended due to its great versatility and low resource consumption. However, it should be noted that to move through it, the use of the mouse is limited -not to say in a null way-. Therefore, the use of the keyboard is essential in this text editor.
Table of Contents
Install Vim on your computer
The first thing you should do is install the famous editor to be able to use it. One of the pleasant surprises of Vim is that it is multiplatform, so you can use it on all operating systems. Although we already told you that it is designed to be used in Linux environments. Its installation on Linux is as follows:
sudo apt-get install vim
Instead, if you want to install it on Windows or MacOS, here we leave you the following links to its official page, where you will have both the version with Terminal and the version with a GUI interface. The most recommended in both cases is that you use the version for Terminal.
Once the text editor is installed on your computer, we proceed to explain the most common Vim commands that will serve as a basis for moving through the different files that you open from it. For open a file to edit, you will have to write the following command:
And pressing the ENTER key, we will already be inside the Vim editor with the document open and ready for you to treat it.
Essential Vim commands for your foray into the popular text editor
The first thing we want to tell you is that although in its original version, many of the keyboards of the time did not have directional keys, the displacements had to be done with other keys -It is case sensitive. And they are the following:
- Right: l
- Left: h
- Down: j
- Above: k
- Go to the beginning of the document that we have open: gg
- Go to the beginning of a line: ^
- Go to the end of a line: $
- Go to the end of the document that we have open: G
- Scroll to the last change we made: ;
Vim commands to edit texts
Once we know how to move through the files that we open in Vim, it is time to edit these texts. And then we leave you with the Vim commands that you will use most frequently. Vim has three different modes: command mode -the one that opens by default-, insert mode and advanced commands mode.
Well, once the file is open, Vim is waiting for you to start typing the first few commands. And what are these? We present them to you below:
- Enter insert mode -place new text-: i (to exit this mode you just have to press ESC)
- Enter insert and place a new character right after the course: a
- Enter insert and write to the end of the current line: A
- Insert a new line below the course: o
- Insert a new line above the course: O
- Replace the character that is right in the course: r (you must immediately press the new character to enter)
- Print with the printer that you have configured by default: Ha!
Vim commands to cut, delete and paste in a document open with the text editor
We continue editing the texts that we have opened with the popular text editor born in 1991. And in this case we are going to leave you with the list of Vim commands that you will need to be able to delete, cut, copy and paste text inside the file.
- Cut the current line the cursor is on: dd (single line) or xd (if you change the 'x' to a number -3dd, for example-, the lines that you have indicated up to the cursor will be cut)
- Paste the text that we have copied or cut in the place where we press the command: p
- Delete a character under the cursor: x
- Delete the entire word where the cursor is located: Daw
- Delete the entire word where the cursor is located and enter insert mode: cw
- Delete from cursor to end of line and enter insert mode: c$
- Copy a whole line: yy
- Copy from where we have the cursor to the end of the line: y$
- Copy the complete word where we have the cursor: yiw
- Copy the line number from where we have placed the cursor: 2yy, 3yy, ... (2 or 3 lines from where we have the cursor)
Undo changes made with Vim commands
Finally, in this small Vim command guide we leave you what you should do if you want to undo changes made with the previous commands executed in the text editor.
- Undo the last command entered – :u
- Undo the last commands indicating number – :xu (replace 'x' with the specific number)
- Redo the last change made – :redo
- To redo all changes from the last hour (or several hours) – :earlier 1h
- To redo the changes of the last few minutes – :later 20m (in this case it would be from the last 20 minutes)
As you can see, Vim commands are plentiful. And we have only stayed in a very small part but maybe it can help you in the introduction of this popular text editor and be able to handle the first files you open from it. Similarly, There is a lot of documentation on the internet about it. and you can go deeper, if you wish.
On the other hand, Vim has a fairly active community which is adding functions and new viewing modes. In addition, to make its use much easier -especially for novices-, there are alternatives with a friendlier user interface that has nothing to do with everything we have explained to you. If you want to discover them, some examples are the following: