In this lesson, you will see how to edit files directly from the terminal with a program called nano.
So every time we wrote files for now, we did it with Thonny.
So you just open Thonny like this programming Thonny you can open a file, you can write to it, you
can save.
And then when you save it somewhere, for example, in documents and Python programs for the Python
programs here.
But all of that, we can also do it from the terminal.
So still we're going to use Thonny to write Python programs.
But for small files and small edits that you need to do, it could be quicker to edit files directly
using the terminal and the program we are going to use.
So I told you it's called Nano.
So it's called Nano like this.
And actually nano is a terminal command that you can write.
So you can write it directly in the terminal.
And so let's go to the home directory.
So if you were not in the home directory you just type CD.
And we are back to the home directory.
Now let's see how to write to a file.
So if I do ls well let's say there is a file here.
I could do nano and then put the name of the file.
If I want to create a new file, I just put Nano and the name of the new file.
Let's just name it new file like this with an underscore.
And note that I didn't put any extension here.
So with Unix you don't need any extension for any file.
Extensions are still going to be useful.
For example, if you create a Python program it needs to end with pi.
Then if you create a text file and you also want to open this text file on windows, maybe better to
use the dot txt extension, right?
But here let's just leave the file with no extension.
So we have a new file called well new file.
And we are here.
You can see we are still in the terminal but it looks different.
And it's actually a well a text editor.
So you can right.
So this is some text.
So you just write the text that you want and you can press enter.
You can use the arrow keys to navigate okay.
For example hello Dot.
You can just well you can write whatever you want.
Now, how to exit from this file?
Well.
To exit from this file you're going to press.
You can see you have some help at the bottom here.
You're going to press Ctrl and X or Ctrl X.
And now you see do you want to save.
So modified buffer is do you want to save the file basically.
And you have yes and no.
So you can write you can type y.
If you type y is going to say yes.
And then you press enter and you see you are back in the terminal and we have exited and saved the file.
So we have seen you can use nano to edit a file.
How to actually just see what's in a file without having to edit it.
Well this is the command called cat.
So cat and then the name of the file.
And note here I can use the autocompletion as well.
So you can use autocompletion for anything in the terminal.
So if I just put n and I press tab you see it's new file because we just have one file.
And this is the content of the file I can also do RLS and if I do RLS, I have my file here.
I can also see this from the file manager.
You see the new file is here and if I double click I can open it.
So this is the same thing.
Now if I want to go back to this file to write to the file again with Nano, I just do Nano.
And I put the name of the file again with Autocompletion because it's easier.
And we're going to make less mistakes with the name.
So I press enter.
I am back to my text editor and that's pretty much it.
The only thing I'm going to show you here is because if you're going to write some code, for example
Python code, you're going to need to add some indentation.
And for the indentation to work correctly with nano you need to do a bit of configuration.
So first let's see.
So let's imagine you're here and you have something like while true okay.
And then you want to add an indentation.
So by default you would use tab and let's say print or print something.
Well this is not real code I'm going gonna execute.
Okay, it's just some text and just to show you.
And you can see that here.
If I go with the arrow keys, I have one tab that corresponds to it seems it's about eight spaces.
So 1234, five, six, seven, eight.
So we have one tab that corresponds to eight spaces.
But it's not really spaces, it's just a tab okay.
Because I just go from the beginning to the end and this kind of stuff is not gonna work with Python.
So let's fix that.
So you can exit the file.
And actually, instead of just exiting and then asking to save, you can directly save with Ctrl and
S.
So it's quite classic command Ctrl S to save.
You can see here you have some comments and then Ctrl X to exit.
And this time you don't have anything to ask you to save because it was already saved.
And to add configuration for nano we will need to.
So first make sure you are in the home directory because you could create this file here anywhere.
But for the configuration it needs to be in the home directory.
So CD and is going to be.
So if I do PWD it's going to be home slash Pi.
And we are going to create a new hidden file named.
So I'm going to use Nano actually to create this file named Dot nano RC.
Okay.
So exactly like this dot nano RC.
So you do this, you enter the file and then we're gonna write two commands.
So you just type this you can type set then space tab size and then space for.
And then you go back to a new line.
And you do set tabs to spaces.
And you see that the color should change which means that the keyword is recognized.
So this is simply going to say, well, instead of using tabs you use spaces.
And then for one tab you have four spaces.
If you want to know more commands to write in the nano SC, you can simply type nano SC on Google and
you will find a lot of comments.
I'm just going to use those two right now.
So I will save the file with Ctrl and S and then exit with Ctrl and X.
Okay.
And so now where is the file.
Well you can see the file with LZ Dash eight.
And you see we have our new file here nano RC.
I can also see what's inside with cat dot nano RC.
I can press tab here for the Autocompletion.
And you see I have the content here.
Great.
And what this file is going to do.
Well when you open Nano it's going to check this file and apply the different commands that we have
written here.
So now if I go back to new file here.
So if I do Nano new file and I press enter.
Now the comments have been run.
And you can see already that here instead of eight spaces we have four spaces.
But as you can see the tab is still a tab.
So it's four spaces.
Yes, but it's still a tab because we have created this tab before we actually set the configuration.
So what you can do is just do a you can remove this and then you can do another tab.
And now you can see that the tab should be actually four spaces.
Okay.
So now for any new thing that you type and any new file that you edit with this configuration that we
did, the tabs will be correct specially here for Python code.
And now you can save the file with Ctrl S.
And if you want you can exit with Ctrl X.
All right so in this lesson you have seen that you can actually edit some files.
And you can even create some files from the terminal.
And so it's going to be the same as if you were creating a file with Sony.
And then writing into it and then saving the file.
Okay.
You can do the exact same thing.
Now of course with Sony is going to be much easier to write Python files, okay, because it has syntax
highlighting, autocompletion and additional features.
So that's why we use programs like this.
But for small files you need to edit like configuration files or even small edits.
Then you can use nano in the terminal.