In this video, I’ll provide an introduction to using vim on Oracle Linux.
vim is an advanced text editor. It is highly customizable and designed for
efficient text editing. But why use vim over other text editors?
The main reason is efficiency. You can do everything in vim using the keyboard,
which means you’re not wasting time moving your hand back and forth between the mouse
and the keyboard. vim has a ton of keyboard shortcuts for common
tasks, as well as the ability to customize and create new shortcuts.
It is also widely used and widely available on numerous platforms.
vim is terminal based, meaning it doesn't require a desktop environment to use it.
So it provides a way to edit files on systems that only have a terminal interface.
There is a steep learning curve with vim, but once you’re familiar with some of the
basics, I hope you’ll see the advantage of using it.
In this video, you’ll learn about Normal vs. Iinsert mode, how to open, save, and close
a file, move around a file efficiently and search for something, and then modify content
with delete, copy, and paste. I’ll be moving pretty quickly, but in the
last minute of the video I’ll provide a cheat sheet so you can easily look up any
commands that we cover. Let’s get started.
I’m in the terminal on an Oracle Linux 8 system.
vim comes pre-installed with Linux, so there’s no package to install.
To open a file, type vim, space, and then the filename.
If I just start typing, thinking it’s like a text editor, odds are it’s going to make
changes that I don’t want. So if you ever find yourself in trouble, just
remember esc : q a ! This will quit without saving changes and
now I’m back at the shell prompt. But, what does esc :qa! do?
Esc makes sure that you’re in command mode. The colon is the prefix for entering command
line. q is short for quit.
a stands for all buffers, meaning if you have multiple files open, it’s going to close
all of them. And the ! represents force, telling it to
quit even if there were changes made. So why didn’t typing like normal work?
Well, vim has two modes: Normal mode, sometimes referred to as command mode and Insert mode.
It launches in Normal mode, where all the keys have a different function similar to
shortcuts in other applications. Which is why if you just start typing, you’re
actually telling it to perform different functions with each key that you press.
The other mode is Insert mode, which is used to insert text.
There are several different ways to get into insert mode.
But you always use Esc to exit insert mode. Let’s open up that file again and take a
look at the modes. One of the most common ways to get into Insert
mode is i. So if I type i, you can see INSERT pop up
at the bottom, indicating I’m no longer in Normal mode, I’m now in Insert mode.
I can type as usual, so I can add text, use the arrow keys to move around, and delete
content. And once I’m done, to exit Insert mode,
I’ll hit esc. The INSERT indication goes away and now we’re
in Normal mode where all the keys have a different function.
A handy one is u, which will undo the last change.
So if I type u, it’ll bring back what I deleted, and u again will remove the text
that I added. I can use ctrl-r to redo and bring that text
back. Once I’m done, to save the file, I’ll
make sure I’m in Normal mode by hitting esc.
Then I’ll use : w for write and q for quit. Let’s open up a longer file and take a look
at how to move around. I can page forward with ctrl-f, backwards
with ctrl-b. I can use the arrow keys to move around, but
that’s not super efficient. Instead, I can use the hjkl keys.
Where h moves left, j moves down, k moves up, and l move to the right.
If I want to move several lines or characters at a time, I can add a number in front of
any of these. So to move 3 lines down, I’d type 3j.
Twenty characters to the right would be 20l. We can also move word by word.
w jumps to the beginning of the next word, w again moves to the next word, and so on.
b jumps to the previous word. And e jumps to the end of the current the
word Just like hjkl, I can add a number to jump
several words at a time. So 7w, would move me seven words forward.
I can use the $ to move to the end of a line, and 0 to move to the beginning.
I can move to the end of the file with shift-g and back to the beginning with gg.
Next, let’s take a look at searching the content.
To search for a word, I can use / then type whatever I’m looking for and hit enter.
It’s important to note that this is case sensitive.
Use n to move forward to the next instance and shift-n to move back.
To clear the highlights, type :noh. Next, let’s take a look at a few of the
ways to get into insert mode. Let’s go to the bottom of the file with
shift-g and add information on where this excerpt is from.
If I use i to get into Insert mode and start typing, I don’t want that.
I want it on the next line. So I’ll hit Esc and u to undo.
Then I’ll type o, which will insert a new line below the cursor.
And I’ll add the excerpt details here. Let’s add a divider above to separate this
information from the rest of the text. Just like o to insert a line below my cursor,
I can use shift-o to add a line above my cursor. I’ll add a bunch of dashes and then hit
esc. I should probably add the author after the
title. So I'll go down, then if I hit i for insert,
you’ll notice that it inserts before my cursor and I actually want it after.
So I’ll undo, and instead, I’ll use a to append after my cursor.
Now let’s look at how to delete content. You may have noticed I made a mistake in the
title, the m of madness should be capitalized. To correct it, I’ll use b a few times to
get back to the beginning of the word. To remove the character, I’ll hit x, then
i to enter insert mode, type the capital M, and then hit Esc.
Let’s say I don’t want this information down here, I want it at the beginning of the
file instead. I can use d to delete and then d again to
delete the entire line. And dd again will remove the line of dashes.
But like everything else, I can add a number before it to apply it to multiple lines.
So I’ll undo, then go up a line, and use 2dd to delete both of these lines.
The d command actually places the content in memory, so you can think of it more like
cutting, rather than deleting. So let’s look at pasting the content I just
cut. I'll go the beginning of the file with gg.
Then use p to paste, but as you can see, it places the content after my cursor, I want
it before. So I’ll undo and instead I’ll use shift-p
to paste it before my cursor. Now maybe I want to copy the divider and put
it below the excerpt information. I’ll go to the line of dashes and use y
for yank and then y again to yank the whole line.
I’ll move down and use p to paste after my cursor.
I think that looks good, so let’s save the file with :wq.
That pretty much covers some of the basics when using vim.
Here’s a summary of what we learned: There are two modes, normal and insert.
There are multiple ways to get into insert mode and you can use Esc to exit insert mode.
To save and quit, you can use :qa! to quit without saving anything and :wq to write and
quit. We learned how to move around a file efficiently
with the hjkl keys, paging forward or back, and jumping to the beginning or end of the
file. We learned how to move to the beginning and
end of a line and jump between words. We learned how to search for content and cycle
through the results. And finally, we learned how to modify text
with delete or cut, copy and paste, as well as undo and redo.
This really only scratches the surface of what you can do with vim, but I hope it gives
you a good jump start. Thanks for watching