This guide will get you started using Vim, one of the most powerful and popular command line text editors around.
Vim was initially only available for the Linux and UNIX platforms, but was then ported to Windows also. The interface is clean and simple, and you use a combination of keys to perform tasks such as copy-paste, search and replace, delete a bunch of lines, and much more.
Vim comes pre-installed with most distributions of Linux and on macOS. In case you don’t have it or want to install it on another platform you can get the installer from here. For Windows users you need to go hereand download the latest release of Vim. Download the zip file for your version of Windows and extract it. Then run the installer. The only thing different between the Linux/Mac version of Vim and the Windows version is that the Windows version runs in its own graphical window. Let’s take a closer look at its usage.
Before we get to the actual usage of Vim let me briefly explain to you the two modes that Vim runs in – command and insert mode. In the command mode you give instructions to Vim to take some action, such as delete a line, or search for a word. When you are in the insert mode what you type goes in as text into the document you are editing. When you launch Vim you are in the command mode. To switch to insert mode hit the ‘i’ key on your keyboard. You will see a notification at the bottom left of your screen like this:
— INSERT —
This means you can now begin editing the file. To switch back to the command mode just hit the Escape key. You will find that the “– INSERT –” notification at the bottom will disappear. You are now in the command mode and can instruct Vim to delete a line, or to save the file, or whatever you may want to do with the document.
To open an existing file or a to create a new one with Vim use the following syntax:
# vim helloworld.txt
If you opened an existing file you should see its content, or a blank page if you created a new one. Now before we proceed with using Vim, let’s see how to exit Vim. This is important because, unlike your standard graphical text editor, you cannot close the window to shut the application. To exit Vim and return to the command line you need to enter the following key combination:
ESC :q ENTER
In plain English the above command goes something like this, “Escape key, then a colon, followed immediately by a ‘q’, and then the Enter key to finish”. The “q” here stands for “quit”. What Vim does here is that it exits without saving any changes you may have made to the file. To save the file and quit add the ‘w’ or “write” option:
ESC :wq ENTER
You can also use the ‘w’ option without the ‘q’, if you just want to save the file while editing it and not quit Vim.
Now that we know how to get in an out of Vim let’s see how to use it. Let’s create a new document with Vim and begin writing:
# vim newdocument.txt
Now hit ‘i’ on your keyboard to get into the insert mode. Write a few lines of text in the file. Once you are done hit the Escape key to go into the command mode, and then “:wq” to save and exit the file. Now let’s see if the file was written correctly. Windows users can open the document we created using Notepad. If you are using Linux or a Mac run the following command to check out the contents of the file:
# cat newdocument.txt
You should see the contents of the file you just created. Now open the same file using Vim and let’s see what more Vim do do. Let’s delete a few words and lines. While in command mode navigate you cursor to he beginning of a word and hit the key combination “dw” followed by Enter. This should delete the word. Now let’s delete a whole line. Pick a line you want to delete and take the cursor to any location in that line and hit the key combination “dd” followed by Enter. Line deleted.
Sometimes you make a mistake that you want to undo, say you want to bring back the line you just deleted. To undo your last action go to the command mode and hit the key “u”. You can continue hitting “u” to undo more changes.
These commands will get you started with Vim.
1. Save file – “:w”
2. Quit file without saving – “:q”
3. Save and quit file – “:wq”
4. Go into insert mode – “i”
5. Go into command mode – ESC
6. Delete word – “dw”
7. Delete line – “dd”
8. Undo change – “u”
1 thought on “Get Started Using the Vim Text Editor”
compliments for a very useful start