1. vi – Part 1
This lesson is about VI, which I have used many times throughout the course, but without going into detail. VI is a text editor for the console. Of course, VI cannot be compared with text editors on graphical interfaces in which you can use the mouse type in bold and or underlying underlying text. You can’t do something like that with Bi, but since it is only a console text editor, we can’t do anything with bold letters or something similar. Nevertheless, bi is very extensive. It is the standard text editor under linux, even if the operation takes a lot of getting used to at first. So first of all, I recommend you to install the editor bim. This is an improved version of the VI. You can install it with pseudo apt install vim and it’s already installed for me.
So first of all, you can start VI in two different ways. Once by entering VI only and once by entering VI with an appropriate file name, the letter ensures that the corresponding file is opened directly, or a new file is created. If there is no one with a corresponding name, we opened it simply with VI without a file name and we started VI. In my case, the Vim is open, which is the short form of VI improved. Now, at the beginning we are in a kind of overview in which we also get a few help hints. And this is the so called command mode with colon and Q. You can see it here on the bottom left we can exit the editor again with VI and the file name we start VI and at the same time open a corresponding file, in this case VI testvi one txt.
This is the file I have prepared before, and now I’m in the appropriate file with escape, colon and q again. Here at the bottom left, I can leave this file again. If we now, for example, use the following command vipestvi two txt, we get a blank screen. This is because the test VI two. txt file does not exist yet. By telling VI, however, a file name, it creates an empty file for us that we could now fill. However, if we exit VI without saving, this file will not be created either. We can exit VI without saving it or saving the file without saving the file by using escape, colon and Q for quit.And then we are out of the editor again. So let’s open Bi again with the file that also has something in it.
So with the test VI one dot, the VI uses different operating modes. First of all, there is the normal mode. This is the mode we are in right now, and the VI will always start in the normal mode when we pass a file. If we start VI without a file, then we are always in the command mode, which can be recognized by the heading that we just had in the middle in Normal mode, where we are in now, we can move through the lines, cut and paste lines or words, or delete entire lines. However, you can’t enter any text. So I’ll try to write here, so I hit a few numbers 65789 or maybe a B over a T. So as you can see, nothing happens here, sorry, in the corresponding text. We can of course move with the arrow keys up and down to left, to the right.
But this is only possible because we are currently using Vim. The arrow keys don’t work with VI, at least not with older VI versions. This is due to the fact that in terminal environments the arrow keys, which are then connected remotely, did not always work properly, and accordingly you should get used to the keys hjk and L instead of the arrow keys. They do exactly the same as the arrow keys. So I will press H now and the cursor will go left, I press J, the cursor goes down, with K it goes up, and with L the cursor goes right. As I said, I can’t just enter text here that doesn’t work because VI doesn’t see certain keys as letters, but as commands. In order to enter text, we have to switch from Normal Mode to the Input mode or to the Insert mode, another name for the same mode.
We have different options to switch to Input mode.These possibilities differ in where the cursor should be. We can, for example, switch to Input mode with the I key. The key I for Input mode ensures that or ensures two things at the same time, namely that we get into the Input mode and can then also write text, and secondly that the cursor is to the left of the current cursor position. I am now with the cursor on the V here, on the V of the VI. Now I press I and we can see down here it has changed to Insert. So we are now in the Insert mode. And if I now enter, for example, the word test and a space, then you can see that the test is inserted before the V of VI. When the text has been typed in, we return to Normal mode with the Escape key.
So I press Escape now and the word Insert here on the top bottom left is gone down here and we are back in the Normal mode. Instead of I, we can also press A. So we are with the cursor on maybe on the I. And if I press I again to get into the Insert mode and I now enter, for example, tests, then it all happens before the eye. You can see it here. So I take that away again and press Escape to get into the Normal mode. You see here insert is gone away and now I press I go back to the eye here and now I press A and you see the cursor jumps one to the right and I can continue typing here. So test. So with Escape it goes back to normal mode and I can no longer write anything.
You can see here the word Insert has gone away and now I try to insert tests. It does not work. Of course, this is not a dramatic difference, but there are, of course, other keyboard shortcuts that make working in VI much easier. For example, the capital A key ensures that the cursor jumps to the end of the current line. So now I press the capital a and you see here I’m at the end of the line, I’m in the Insert mode and I can put in text. With Escape I switch back to the normal mode. So, for example, the capital G. I press the capital G now jumps to the last line. In the first place I press so I press Shift and G for the capital G, of course. And the cursor is now down here with double G, we come back to the top left.
Note that capital G or gg does not indicate that we are in the input mode. You can see it here, top left. Again, shift G for capital G. The cursor is here in the last line on the left, but you can see here no Insert mode. With a gg I jump to the first place, but again no Insert mode. The key O ensures that a blank line is inserted below the current line. Try this. I press O and you see here there is a blank line, and in this case I’m in the input mode. In the Insert mode, I press Escape to switch back to the normal mode. Maybe I go a little bit down to this position here and the capital O key. So Shift O ensures that a blank line is inserted above the current line. So I press capital O now.
And here you see we have a blank line above this line and again here in the input mode. With Escape I switch back to the normal mode. There are, of course, various other options, but these are so huge in VI that going through all of them would be counterproductive. So I will limit myself to the most important ones here. Otherwise, I can only recommend entering VI cheat sheet in a search engine, where you will find huge tables with corresponding VI commands and you can deal with them for longer. So let’s make a change to the file now, but we do this in the next video.
2. vi – Part 2
Let’s go to the corresponding file again with VI and the test file and make any change. For example, let’s make a new line here, or two new lines here. This is a new test. For example and now I want to quit. I press Escape to switch to the normal mode, then colon and Q. And now I got a query here. We see no right since last change. So in principle I am informed that we still have unsaved data. In this case I don’t want to save it. So we add an exclamation mark after the queue. So I press escape again, colon Q and then an exclamation mark. And so we can exit the file without having to save it. Let’s open the file again and make any changes again, doesn’t matter what for what changes. And usually we exit the file with colon Q and in that case we use colon.
You can see it here, the bottom left wq. The W stands for right. And so the file is saved and we are right out of the file. So if we execute wq without Q so again, let me change something here. Escape for the normal mode and then colon and then W without a queue, then the change is saved, but we do not leave the file. We can continue working in it as normal. There are two other ways to save a file with escape and then uppercase z twice. So escape and then shift and that then the file is also saved and you exit directly. Let’s open the file again. Let’s do some changes. And the other way is first escape, then colon and then an X. You can see it here x. Exit the file as well and the content will be saved as well.
If you want to search for a word in a file, there are two search options that you can use. Once slash for the forward search and once the question mark for the backward search. Open the file again. The search only works in normal mode and not in input mode. So as a reminder, with escape we always return to the normal mode. If I now for example, want to search for the word test file, then I press Shift and seven. And as always you can see it here at the bottom left. And then I write in test file here you can see it is marked now. And now I have to press Enter one time. And when I press then the N for next, then I will jump to the next found Word text file. When I want to search backwards, I don’t use slash but the question mark.
So I enter here question mark and then again test file. See, here it is mark. Now I press Enter one time and now again N for next. And then I get to the next fount test file, but backwards. Okay, suppose we don’t like an entire line. How can we delete it. Of course we can switch to the input mode and delete the line with back space, but it is easier in normal mode to move the cursor to the corresponding line and press the D twice. So I go to this line here and dd. And as you see, the line is deleted. So now I want to leave this line dd and this line. So I have deleted three lines. But we can also say that we want to delete three numbers from the cursor position. Maybe it’s better here.
And then we simply select we press the three and then the double D. So three wd and now three lines are deleted. This is how it works with most of the commands in bi. If you copy lines or words, you can use that as well. So let’s assume we don’t just want to delete a line, but simply copy it and paste it into another place. We go to the corresponding line and we use the double Y. So here double Y and now the line should be copied. We don’t get any indication that the line was copied. We go to this place, for example and now I press P for paste and we can see that the line is inserted below the current cursor position. So let me go up, I copy again with ayy and go back to the lower end and I can insert it again with but this time with the capital P.
So now I press capital P. And so you see, this time the text was copied to a line above the cursor position. Now I want to copy six lines from the cursor location. Then I press six and double Y and I’m going to the end here. I could have use capital G to go to the end and then I use P and the text block is successfully inserted. If we don’t really like a line, we just want to keep the first word here. For example, we put the cursor here at this place and I press the capital D and the capital D will delete the line from the cursor position on. In principle we would have gone through all the aberrations or all keyboard entries that Epic would like to have. As examples, here are two others that are perhaps very important.
With the key W, you can always get to the beginning of the next word. So now I press. Www The E key ensures that the cursor jumps to the end of a word. So I press E and so on. The capital B always goes back one word to the beginning of the word. So I press Capital bb. So one word back and at the beginning and if we just want to delete individual characters, we take the key x and then the individual character is deleted accordingly. So I press X. Now x, x and so on. That should have been the most important terms. I think but as I said again, the node look for VI cheat sheet on the internet there are very extensive tables that contain all VI functions you are guaranteed not to get all of them for the exam but if you would like to deal with it, you can definitely do so there.