Here is the video version, if you prefer it:
In this tutorial, we will be learning how to use the vi editor. As I said in one of the earlier posts, on some computers, there is no alternative other than to use vi. So you must know the basic stuff. Let’s see what the basics are. I modeled this after (Shotts, 2019), but I pruned it even more. This is intended as a follow-along guide – follow the instructions in this article and gain a basic understanding of vi.
I have a file called aba.txt
. Let me first show you the contents of the file, so you can replicate it on your computer:
mislav@mislavovo-racunalo:~/Linux_folder$ cat aba.txt
Abba Money
Money Money
It's the Money
In the Rich Man's World
I will open it with vi using the following:
vi aba.txt
I could also open multiple files at the same time by typing something like:
vi file1.txt file2.txt
When I open up aba.txt
with vi, I get something like this:
Abba Money
Money Money
It's the Money
In the Rich Man's World
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
"aba.txt" 4 lines, 62 characters
The first thing to know about vi is how to quit it. To quit vi, type :q
(with the colon in front) and press Enter (Enter on your keyboard). That will exit vi.
Let’s reopen vi.
vi aba.txt
OK. The first thing to realize about vi is that it has two modes – command mode and insert mode. By default, you are in command mode. That means that any characters you type are not going to be interpreted as symbols to be inserted, but rather as commands. Also, note that cursor movement is done in the command mode. You can move your cursor with the arrows on your keyboard. But don’t move your cursor just yet. To enter insert mode, press i
on your keyboard. Do it now.
You can type in something. A word of caution: Backspace won’t be delete the characters you mistype. I typed in “Some stuff “. To exit insert mode, press ESC
(escape on your keyboard).
My file looks like this now:
Some stuff Abba Money
Money Money
It's the Money
In the Rich Man's World
~
~
~
~
…
where …
denotes the continuation of the file.
Since we pressed Escape, now we are in the command mode again. Now we have to write the changes to the file. To write changes we just made to the file, type in :w
and press Enter.
Now let’s learn how to append to a file. While in command mode (and from this point on it is assumed you are in the command mode when a particular instruction is given for you to do, unless explicitly stated otherwise) type capital A
(on your keyboard – I assume this from this point on). That will allow you to append after the line where the cursor is currently located and it automatically puts you in insert mode. If you haven’t moved your cursor around, you will append just below the first line. So let’s add the line “Money is how we transfer wealth”. Feel free to press Enter on your keyboard, and write “It is an IOU”. Then press Escape to exit the insert mode. Your file should look like this now:
Some stuff Abba Money
Money is how we transfer wealth
It is an IOU
Money Money
It's the Money
In the Rich Man's World
~
~
...
So A
(capital A) appends to the file by adding lines after the line where the cursor is located.
Now let’s talk about inserting blank lines and undoing our changes. If you want to insert a blank line below the current line, press o
(lowercase o). If you want to insert a blank line above the current line, press O
(capital O). Note that both of these commands put you in insert mode, so you will need to exit them by pressing ESC
. Press o now. Uh-oh. Let’s say you didn’t want this. First, exit the insert mode by pressing ESC
. Now, press u
(lowercase u). It will undo the change you did. Try it with both o
and O
and then undo the changes.
Now let’s learn how to delete characters. You do this by pressing x
. Let’s do an exercise. First, place your cursor on the end of the line which says “It is an IOU”. Press x
. Press x
again. Press x
until you delete the entire line. Note that no matter how much you press x
, the line won’t “merge” with the previous line. That’s OK. We will cover how to do that later.
Your file should look like this:
Some stuff Abba Money
Money is how we transfer wealth
Money Money
It's the Money
In the Rich Man's World
~
...
Let us now discuss deleting stuff other than just characters. Here is a list on how to delete various chunks of text:
dd
– deletes the current lined$
– deletes from the current cursor location to the end of the current linedG
– deletes from the current line to the end of the file
Now, a revelation – the d
command (the first letter in the above commands) doesn’t just delete text, it “cuts” text. You can paste it with p. Try placing your cursor on the line “Some stuff Abba Money”, type dd
, then type p
. That should cut and paste the line. But what just happened? Well, the line “Some stuff Abba Money” is below the line “Money is how we transfer wealth”. Why is that so? p
pastes the line below the line where the cursor is, while P
(capital P) pastes the line on the line above the cursor.
Now, let’s talk copying (also called yanking):
yy
– copies the current liney$
– copies from the current cursor location to the end of the current lineyG
– copies from the current line to the end of the file
Try this: type yy
while your cursor is on the line “Money Money”, then press P
(capital P). To remind ourselves, this pastes the copied line above the line the cursor is currently in. Great! More money! We are rich!
Here is how our file looks like now:
Money is how we transfer wealth
Some stuff Abba Money
Money Money
Money Money
It's the Money
In the Rich Man's World
~
~
...
Now one last touch – let’s remove the blank line in the middle of our file. We do that by placing our cursor on that line and typing capital J.
Aaah! Perfect! Now let’s write the changes to our file. Type :w
and press Enter. Then type :q
and press Enter to quit vi. A caveat: if you type :w <newFilename>
where <newFilename>
is the new filename you want for your file, a new file will be created, but in vi you will still be editing the old file.
Voila! You now know the very basics of vi. Now, there are some things we didn’t cover, such as multiple files and search-and-replace, but we will cover that in the next article. These are the very basics. If you wanted to search and replace some word with some other word, you would have to do it manually and you would have to have separate Terminals for each of the files to be opened in vi. Then, you would manually search and replace. That is tedious, but, you have the basics.
Feel free to skip the next article, if you wanted to learn only the essentials of vi. The next article covers multiple files in vi, search and search-and-replace functionality in vi. It will make you a more efficient vi-er, but if you just wanted to know the very rudimentary basics, off you can go.
Hope you learned something useful!
References
Shotts, W. (2019). The Linux Command Line, Fifth Internet Edition. Retrieved from http://linuxcommand.org/tlcl.php. Pages 165-176
Subscribe to my newsletter to keep abreast of the interesting things I'm doing. I will send you the newsletter only when there is something interesting. This means 0% spam, 100% interesting content.