To configure or change anything on Linux, you need to edit a specific configuration file. To write 1
or edit files, you need a text editor. 2
There are text editors that work in graphical mode and text editors that work in the terminal, without 3
a graphical interface. Since most of the administration work is done in the terminal 4
you need to learn how to use a text editor 5
that doesn't require a graphical interface. 6
They are called CLI, or Command Line Interface editors. 7
There are a lot of such editors and among them we can mention: vi, vim, nano, pico or emacs. In this 8
course we are going to use a text editor called vim, which is, by the way, the most used editor 9
and the default one for many Linux distributions. Vim stands for VI improved, as vim is based on vi and 10
vi is the original text editor on Unix , released more than 40 years ago. 11
Everything in vi is available in vim, plus some extra features. 12
If vi is always available on any flavour of Linux or Unix, depending on your distribution, VIM should 13
be installed or not. To install or update vim on ubuntu 14
I’m running: sudo apt install vim 15
And it's installing it. 16
And on CentOS: sudo dnf install vim 17
Vim is a very powerful text editor and it offers extensive text editing options. However, in this 18
introduction, we are going to focus on exploring some of the more basic functions and commands, the 19
ones that are required for basic file editing. 20
Keep in mind, that vim is not so easy at first sight, but once you learn how to use it, you'll 21
love it. 22
If you try to open a file using vi like vi and the filename, it just opens it in vim because vi 23
is an alias or a sibling to vim, both on Ubuntu and on CentOS. 24
In this example, the file was opened with vim. 25
I'm exiting the file typing :wq 26
As you notice, vi is an alias to vim. 27
Let's get started with vim. To use vim on a file 28
you type in vim and the name of the file using a correct, absolute or relative path. 29
If the file already exists, wim will open it for editing. 30
But if it doesn't exist, then wim will create the file. 31
I'll make a copy of an existing file to show you the basics of vim. I'm copying the ssh server configuration 32
file from /etc/ssh to the current directory so cp 33
/etc/ssh/sshd_config and the destination, 34
the current directory. 35
And I'm opening it with vim; vim and the path to the file. 36
Vim is a model editor, and that means it has three basic modes of operation: command, insert and last 37
line modes. When you open a file in vim, you are in the command mode. 38
In this mode, characters you type get interpreted as commands, like x will delete the character currently 39
under the cursor. 40
or r will replace the character under the cursor. 41
I'm pressingr and 6. 42
And it replaced 1 with 6. To get back to this mode from any other mode, just press ESC one or more 43
times. 44
I'll tell you a secret: when you are in a mode and get stuck press a few times the escape key and it 45
will take you to the command mode. To start writing text, as in a normal text editor, you need to enter 46
into another mode called insert. 47
To enter into insert mode from command mode press lowercase `i` and it will insert the text 48
before the cursor until you press ESC. 49
So I'm pressing I, I'm in the insert mode and I can write like in any other text editor until 50
I press ESC. 51
Remember that when you hit esc, it enters the command mode. 52
Now, I'm in the comment mode because I've pressed esc. If I press the uppercase I it will start inserting 53
the text at the beginning of the current line until esc is hit. 54
I'm in the command mode again. 55
If I press a, the lowercase a, vim will append the text after the cursor until esc is hit. 56
An upper case A will make it append the text to the end of the current line. 57
And the last two keys are lower case o, to write the text on a new line below the current line, and uppercase 58
O to write on a new line above the current line. 59
So in a nutshell, to enter the insert mood from command mode type i, a and o, both lower and upper case. 60
The 3rd VIM mode is known as vim last line mode; you can only get to the last line mode from command 61
mode and you get into last line by pressing the colon key like this. 62
I'm in the last line mode; after pressing these key, you'll see a colon character appearing at the 63
beginning of the last line of your vim editor window and the cursor will be moved to that position. From 64
vim last line mode 65
you can do all sorts of things like saving the file, quitting the file without saving it and so 66
on. 67
Let's quit vim without saving the file. 68
I'm writing q and an exclamation mark and I'm hitting enter. 69
I quit without saving; I'm opening the file again and making some changes. 70
I'm typing o to write on the next line. 71
When I'm done, I'm pressing ESC to go to the command mode again; from the command mode, I am pressing 72
colon and I'm in the last line mode. To save the file without closing it 73
I write W and an exclamation mark. 74
And I'm hitting enter. 75
The file was saved. If you want to save the file and then close it 76
In the last line mode type WQ and an exclamation mark. 77
I'm opening it again. 78
One of my preferred shortcuts or commands while in the command mode is to save and quit the file quickly 79
and easily by typing shift and zz. 80
So I am pressing Shift and the Z two times. That was the basics of vim; you can now perform basic file 81
editing. To go deeper into vim or just practice it, run vim tutor ad vim will open a tutorial 82
file. 83
You can freely work on it because it's a copy. Next time you run vim tutor , it will open the 84
same original file without any of the changes you have done. 85
I'm making some changes. 86
And I'm saving and exiting the file. 87
I'm running vim tutor again and I'm seeing the original file. In the next lecture will dive deeper into 88
it and I'll show you many other useful vim features. 89
See you in a second!