WEBVTT

00:00.110 --> 00:06.380
In this lesson, you will see how to edit files directly from the terminal with a program called nano.

00:06.380 --> 00:10.280
So every time we wrote files for now, we did it with Thonny.

00:10.280 --> 00:15.350
So you just open Thonny like this programming Thonny you can open a file, you can write to it, you

00:15.350 --> 00:16.190
can save.

00:16.190 --> 00:20.540
And then when you save it somewhere, for example, in documents and Python programs for the Python

00:20.540 --> 00:21.380
programs here.

00:21.380 --> 00:24.170
But all of that, we can also do it from the terminal.

00:24.170 --> 00:27.170
So still we're going to use Thonny to write Python programs.

00:27.170 --> 00:33.080
But for small files and small edits that you need to do, it could be quicker to edit files directly

00:33.080 --> 00:35.960
using the terminal and the program we are going to use.

00:35.960 --> 00:37.460
So I told you it's called Nano.

00:37.460 --> 00:39.320
So it's called Nano like this.

00:39.320 --> 00:42.950
And actually nano is a terminal command that you can write.

00:42.950 --> 00:45.500
So you can write it directly in the terminal.

00:45.500 --> 00:48.110
And so let's go to the home directory.

00:48.110 --> 00:51.290
So if you were not in the home directory you just type CD.

00:51.290 --> 00:53.300
And we are back to the home directory.

00:53.330 --> 00:55.580
Now let's see how to write to a file.

00:55.580 --> 00:59.090
So if I do ls well let's say there is a file here.

00:59.090 --> 01:01.950
I could do nano and then put the name of the file.

01:01.950 --> 01:06.510
If I want to create a new file, I just put Nano and the name of the new file.

01:06.510 --> 01:11.220
Let's just name it new file like this with an underscore.

01:11.220 --> 01:14.520
And note that I didn't put any extension here.

01:14.520 --> 01:17.880
So with Unix you don't need any extension for any file.

01:17.880 --> 01:19.650
Extensions are still going to be useful.

01:19.650 --> 01:23.940
For example, if you create a Python program it needs to end with pi.

01:23.970 --> 01:29.190
Then if you create a text file and you also want to open this text file on windows, maybe better to

01:29.220 --> 01:31.320
use the dot txt extension, right?

01:31.350 --> 01:35.880
But here let's just leave the file with no extension.

01:35.880 --> 01:39.780
So we have a new file called well new file.

01:39.780 --> 01:40.770
And we are here.

01:40.770 --> 01:43.020
You can see we are still in the terminal but it looks different.

01:43.020 --> 01:45.390
And it's actually a well a text editor.

01:45.390 --> 01:46.560
So you can right.

01:46.560 --> 01:48.660
So this is some text.

01:49.680 --> 01:53.790
So you just write the text that you want and you can press enter.

01:53.790 --> 01:57.840
You can use the arrow keys to navigate okay.

01:57.870 --> 02:00.000
For example hello Dot.

02:00.000 --> 02:02.660
You can just well you can write whatever you want.

02:02.690 --> 02:05.240
Now, how to exit from this file?

02:05.240 --> 02:05.540
Well.

02:05.570 --> 02:08.120
To exit from this file you're going to press.

02:08.120 --> 02:10.310
You can see you have some help at the bottom here.

02:10.310 --> 02:14.300
You're going to press Ctrl and X or Ctrl X.

02:14.300 --> 02:16.640
And now you see do you want to save.

02:16.640 --> 02:19.850
So modified buffer is do you want to save the file basically.

02:19.880 --> 02:21.320
And you have yes and no.

02:21.320 --> 02:24.920
So you can write you can type y.

02:24.950 --> 02:27.140
If you type y is going to say yes.

02:27.140 --> 02:33.770
And then you press enter and you see you are back in the terminal and we have exited and saved the file.

02:33.770 --> 02:37.130
So we have seen you can use nano to edit a file.

02:37.160 --> 02:41.180
How to actually just see what's in a file without having to edit it.

02:41.180 --> 02:42.860
Well this is the command called cat.

02:42.890 --> 02:45.800
So cat and then the name of the file.

02:45.800 --> 02:48.560
And note here I can use the autocompletion as well.

02:48.560 --> 02:51.500
So you can use autocompletion for anything in the terminal.

02:51.500 --> 02:57.650
So if I just put n and I press tab you see it's new file because we just have one file.

02:58.400 --> 03:04.120
And this is the content of the file I can also do RLS and if I do RLS, I have my file here.

03:04.120 --> 03:08.050
I can also see this from the file manager.

03:08.050 --> 03:12.160
You see the new file is here and if I double click I can open it.

03:12.610 --> 03:14.350
So this is the same thing.

03:14.770 --> 03:20.200
Now if I want to go back to this file to write to the file again with Nano, I just do Nano.

03:20.260 --> 03:24.970
And I put the name of the file again with Autocompletion because it's easier.

03:24.970 --> 03:27.250
And we're going to make less mistakes with the name.

03:27.250 --> 03:28.390
So I press enter.

03:28.390 --> 03:31.810
I am back to my text editor and that's pretty much it.

03:31.810 --> 03:36.970
The only thing I'm going to show you here is because if you're going to write some code, for example

03:36.970 --> 03:39.760
Python code, you're going to need to add some indentation.

03:39.760 --> 03:44.530
And for the indentation to work correctly with nano you need to do a bit of configuration.

03:44.530 --> 03:45.820
So first let's see.

03:45.820 --> 03:51.730
So let's imagine you're here and you have something like while true okay.

03:51.760 --> 03:53.650
And then you want to add an indentation.

03:53.650 --> 03:59.560
So by default you would use tab and let's say print or print something.

03:59.560 --> 04:01.910
Well this is not real code I'm going gonna execute.

04:01.940 --> 04:03.950
Okay, it's just some text and just to show you.

04:03.950 --> 04:05.840
And you can see that here.

04:05.840 --> 04:14.390
If I go with the arrow keys, I have one tab that corresponds to it seems it's about eight spaces.

04:14.390 --> 04:17.090
So 1234, five, six, seven, eight.

04:17.090 --> 04:20.330
So we have one tab that corresponds to eight spaces.

04:20.330 --> 04:22.370
But it's not really spaces, it's just a tab okay.

04:22.400 --> 04:28.550
Because I just go from the beginning to the end and this kind of stuff is not gonna work with Python.

04:28.550 --> 04:29.810
So let's fix that.

04:29.810 --> 04:31.160
So you can exit the file.

04:31.160 --> 04:37.970
And actually, instead of just exiting and then asking to save, you can directly save with Ctrl and

04:38.000 --> 04:38.540
S.

04:38.540 --> 04:41.600
So it's quite classic command Ctrl S to save.

04:41.630 --> 04:46.610
You can see here you have some comments and then Ctrl X to exit.

04:46.610 --> 04:50.870
And this time you don't have anything to ask you to save because it was already saved.

04:50.870 --> 04:54.140
And to add configuration for nano we will need to.

04:54.170 --> 04:58.700
So first make sure you are in the home directory because you could create this file here anywhere.

04:58.700 --> 05:02.760
But for the configuration it needs to be in the home directory.

05:02.760 --> 05:04.380
So CD and is going to be.

05:04.380 --> 05:07.560
So if I do PWD it's going to be home slash Pi.

05:07.560 --> 05:11.640
And we are going to create a new hidden file named.

05:11.640 --> 05:19.020
So I'm going to use Nano actually to create this file named Dot nano RC.

05:19.260 --> 05:19.620
Okay.

05:19.650 --> 05:23.160
So exactly like this dot nano RC.

05:23.250 --> 05:27.720
So you do this, you enter the file and then we're gonna write two commands.

05:27.720 --> 05:35.730
So you just type this you can type set then space tab size and then space for.

05:36.210 --> 05:37.440
And then you go back to a new line.

05:37.440 --> 05:42.420
And you do set tabs to spaces.

05:42.420 --> 05:47.070
And you see that the color should change which means that the keyword is recognized.

05:47.070 --> 05:51.660
So this is simply going to say, well, instead of using tabs you use spaces.

05:51.660 --> 05:53.970
And then for one tab you have four spaces.

05:53.970 --> 05:59.070
If you want to know more commands to write in the nano SC, you can simply type nano SC on Google and

05:59.070 --> 06:00.600
you will find a lot of comments.

06:00.600 --> 06:02.650
I'm just going to use those two right now.

06:02.650 --> 06:07.870
So I will save the file with Ctrl and S and then exit with Ctrl and X.

06:07.900 --> 06:08.080
Okay.

06:08.110 --> 06:10.000
And so now where is the file.

06:10.030 --> 06:13.450
Well you can see the file with LZ Dash eight.

06:13.570 --> 06:18.400
And you see we have our new file here nano RC.

06:18.550 --> 06:24.550
I can also see what's inside with cat dot nano RC.

06:24.580 --> 06:26.590
I can press tab here for the Autocompletion.

06:26.590 --> 06:29.050
And you see I have the content here.

06:29.080 --> 06:29.500
Great.

06:29.500 --> 06:31.300
And what this file is going to do.

06:31.300 --> 06:36.850
Well when you open Nano it's going to check this file and apply the different commands that we have

06:36.850 --> 06:37.450
written here.

06:37.450 --> 06:40.000
So now if I go back to new file here.

06:40.000 --> 06:45.610
So if I do Nano new file and I press enter.

06:45.640 --> 06:47.320
Now the comments have been run.

06:47.320 --> 06:52.360
And you can see already that here instead of eight spaces we have four spaces.

06:52.360 --> 06:54.970
But as you can see the tab is still a tab.

06:54.970 --> 06:55.930
So it's four spaces.

06:55.930 --> 07:01.810
Yes, but it's still a tab because we have created this tab before we actually set the configuration.

07:01.810 --> 07:06.820
So what you can do is just do a you can remove this and then you can do another tab.

07:06.820 --> 07:10.960
And now you can see that the tab should be actually four spaces.

07:11.590 --> 07:12.100
Okay.

07:12.100 --> 07:18.760
So now for any new thing that you type and any new file that you edit with this configuration that we

07:18.760 --> 07:22.720
did, the tabs will be correct specially here for Python code.

07:22.720 --> 07:25.030
And now you can save the file with Ctrl S.

07:25.030 --> 07:28.150
And if you want you can exit with Ctrl X.

07:28.180 --> 07:33.010
All right so in this lesson you have seen that you can actually edit some files.

07:33.010 --> 07:35.830
And you can even create some files from the terminal.

07:35.860 --> 07:39.550
And so it's going to be the same as if you were creating a file with Sony.

07:39.550 --> 07:42.190
And then writing into it and then saving the file.

07:42.220 --> 07:42.400
Okay.

07:42.430 --> 07:44.110
You can do the exact same thing.

07:44.110 --> 07:49.720
Now of course with Sony is going to be much easier to write Python files, okay, because it has syntax

07:49.720 --> 07:53.140
highlighting, autocompletion and additional features.

07:53.140 --> 07:54.940
So that's why we use programs like this.

07:54.940 --> 07:59.710
But for small files you need to edit like configuration files or even small edits.

07:59.710 --> 08:02.290
Then you can use nano in the terminal.
