Let me show you a few more useful shortcuts. If you want to move to a specific line in the last line 1
mode, type the line number and press enter, for example, I'm moving to line 100. 2
This is useful when you get an error in the shell script or configuration file and it says that the error is 3
on a specific line, like on line 250 and you want to move there to see what's all about. To move to the end 4
of the file type capital G. 5
OK, I'm at the end! To move to the beginning of the file type double g, lowercase g or move to the first 6
line as I've already shown you. 7
I'm typing g two times, I'm at the beginning; or :1 and I'm hitting enter, it's the same. 8
Let's see how to work with multiple files at once, this is useful when you want to copy and paste between 9
files or to visually compare the file contents. You can open more files like this vim and the file names. 10
I'm quickly creating two files using a redirection: ifconfig > a and who -a > b 11
so I've redirected the output off ifconfig to A and the output of who-a to b; this 12
is a 13
and this is b and I'm opening both files. 14
So vim a and b 15
It's displaying the first file, but I can move to the next one by typingn or next in the last line mode. 16
So :n I'm hitting enter, this is the next file. To access the previous file type `prev` or `N` 17
in the last line mode. 18
So : prev and I'm hitting enter, this is the previous file. 19
Another way to work with multiple files at the same time is to use the `-o` option 20
which will split the window and open a window for each file. 21
This is known as stacked windows. 22
I'm exiting this section of vim and I'm writing: vim -o a and b 23
You can move between windows by pressing control + W. 24
I'm in the second file and now I'm in the first one; this is a convenient way to copy and paste or 25
to do other operations on both files. 26
For example, I'm pressing shift and V, I have selected the line. 27
Now I'm pressing y, I've copied to the clipboard, control and double u to move to the next window 28
and p to paste the line. 29
OK, sometimes it's useful to view to files side by side and see the differences between them. 30
This is especially useful when you change some configuration options of a service and after a while, 31
you want to compare the file with a baseline, with your original one. 32
I am exiting vim. 33
Let's take a look at an example! I'm copying the configuration file of the ssh server again in the current 34
working directory to have the original file. 35
And I'll do some changes, I'm opening the file. 36
and change port 22 to port 29, I'm saving the file. 37
Now I want to open both files to see the changes. 38
To do that, I'll use the -d option: vim -d the original file 39
and a copy. 40
You see both files opened in stacked windows and the lines that are different are highlighted. Now you 41
can move between files by using control and W. 42
I'm exiting. 43
There’s also a wrapper called vimdiff that calls vim with the required options: so vimdiff and the files 44
It's the same as running vim - d 45
I am exiting without saving. 46
One last thing, if you exit vim without properly closing it, so without using a command or a shortcut, 47
like closing the terminal or pressing control +z, the next time when you open the same file, you'll be 48
presented with a message. I am opening a file, this on. And 49
instead of exiting using a shortcut, I am pressing control +z. 50
This isn't a vim shortcut; or I can close the terminal, the entire terminal. 51
I'm pressing control and z. 52
Now I want to open the file again. 53
And I'm presented with this message. what I can do, 54
I can try to recover the file by pressing `r` and then saving it normally or you can simply delete 55
the swap file that was created, this one. 56
So I am typing enter, I am exiting and I'm removing the swap file. 57
This one. 58
And next time I can open the file normally.