Okay, for the many reasons that we discussed in the previous video, we will now talk about vim.
So what is there to know about vim? Well, after starting vim, it opens in Command mode,
and command mode is called Command mode because you can only type commands in that mode.
So if you want to enter text, the first thing you need to do is press either I or O to
get into insert or input mode. And once you're done typing text, you press ESC to get back to Command mode.
Now, in Command mode, there are a couple of important commands that you need to know about.
Well, in fact, there are so many commands in VIM that it easily will give you a headache.
If I may give you an advice, don't try to memorize all of these commands because
you will notice that sometimes there are five different commands to do the exact same thing.
Focus on a handful of commands, learn how to work with VIM in a comfortable
way based on that handful of commands, and then you can learn about other commands.
So the first relevant command is wq.
The command starts with a colon, because it does.
Believe me, you don't want me to tell you about X mode, which was relevant in 1984 when it was first invented.
W is for write, Q is for quit, and exclamation mark is for don't complain about anything, just do it.
If ever you deleted something by accident, you are going to like U. U for undo,
undo the last change and DD is what allows you to delete the current line.
You will notice that in Linux, editing text files is very often line oriented. So
it's convenient if you can easily delete an entire line if you don't need it.
And one that I really like is the column percent s, which is substitute.
Old is the text that it is looking for, new is the text that it's substituting for.
And the G makes it a global substitute without the G, it would only substitute the first
time it finds the text on the line and it would leave the other times alone.
And by making it a global substitute, you replace it all the time.
Now what I would recommend is you use Vim Tutor. Vim Tutor provides an easy to
use on system Tutor that guides you through the essentials of using VI and Vim.
It will take you about 20 minutes and I would say go for it, go use it, because
you'll learn a lot from it, even these keys that you probably never want to work with.
So let me use VIM regex. I'm going to need this later.
And now I'm in A new file with the name regex. You can see that in the lower left corner, it indicates new.
And I'm going to use I for input. And here you can see that I'm in insert or input mode.
And I'm going to use B N n and B O N
N E n and B O o n and B o o
N E n and BT and bot and boot and boat and
B O, A, B, C, D, F, G, A T.
That sounds like a weird file, but hey, I need it for a later demo and I kind of like it.
So I'm done now.
Now what do I need to do first? Well, we need this command to write and quit.
First I'm using escape to get back to command mode, and now
I'm using WQ to write quit and don't ask any smart questions.
Now that we know about the basics of working with vim,
let's talk about a couple of more useful VIM commands.
I tried to make some reference slides, and on the reference slides I'm trying to go beyond the useful commands.
What do we see on this slide?
Q.
That's your desperate mode. Quit without saving.
U is for undo and Control R is for redo.
You know, sometimes you want to undo something and oops, one undo too
many. Then you can use Control R to undo the last undo.
V is for visual mode. It uses arrow keys to select a block, and
D is to delete the current selection, which is useful after selecting a block.
Then you can use Y to copy or yank the current selection.
And a command like 3dd which deletes 3 lines.
Text will search for text.
GG will move the cursor to the top of the document, and uppercase G will move the cursor
to the end of the document before going to the next slide. Let's do a quick demo.
I'm copying regex to vim trial and I'm
going to use vim on vim trial.
Let's imagine that I'm using this V for visual, uppercase, D to
go to the bottom, and D for delete. Oh my goodness. What
happened? Well, I just accidentally deleted all of my contents.
You know what I can do? I can use Escape, I'm making sure that
I'm in the command mode, and colon Q which is quit without writing anything.
That's doing so much better.
What else can I show you? Well, let's get back
to this VIM trial and let me use 3dd.
3dd will delete three lines. Oh, I regret. So I'm using u for undo.
Now, uppercase g is convenient, lowercase g two times is convenient. Did you
see that? That moves the cursor up and down in the current configuration.
And if ever you want to search for text, then you use slash followed by
the text that you are looking for. And there you can see it's automatically highlighted.
And that allows you to do whatever you want to do with it. Well, what I
want to do with it right now I want to Q to quit without saving anything.
I have a couple more useful VIM commands for you.
Let's talk about them.
So we have W which allows you to go to the next word, or B which allows you to go to the previous word.
Caret moves to the start of the current line, and dollar moves to the end of the current line.
And dw is for deleting the current word.
Uppercase o opens a line above the current position and
lowercase o opens a line after the cursor position.
And I like senumber, that will show line numbers. Very convenient in programming.
And R which will replace only one single character. Using VIM While working with vi,
it's very important that you realize that there's command mode and there's input mode.
So command mode, that's what you start in. It's for entering commands.
And input mode, also known as insert mode, is where you type your text.
The first challenge that you will have while working with vi is how do you get into input mode? Well,
different keys are available. The I, lowercase o, uppercase o, and even more, I'm not going to list all of
them because when you want to work with vi, definitely don't try to memorize all of these different commands.
Now, once you're in input mode, you can type your text and at a certain moment you're done. And
then in order to save your text, you need to press escape to get back to command mode.
When you're back in command mode, you can do whatever you want to do with your document. Some of the
most obvious commands are colon w, q exclamation mark for instance for write, quit and don't ask any questions.
Or q exclamation mark if you want to make sure that nothing is changed, or U if you want to
undo, or dd if you want to delete a line. And many more commands are available in command mode.
I'm not going to write them all here on the lightboard. I've listed the most important commands on the next couple of slides.
Try to get used to them because really these are the most essential commands and don't
let these VI cheat sheets drive yourself crazy. Because it's way too many commands. You don't
need to memorize all of them in order to be successful with the VI Editor.