In this small tutorial, we as beginners, who are not programmers (yet), will like to use a simpler text editor that does the job pretty well. And Nano is a default editor for DietPi and comes as pre-installed in many Linux distributions like Debian.
There are advanced editors like vi (correctly pronounced as vee-eye, while some say it as vy, as in why, and definitely not as in roman six), and GUI based gedit, but for us Nano, a small/compact works the best.
Everything in Linux is a file, so all configuration/setup of all services and utilities is written as file, which we may need to either edit or create one from time to time.
You may need to append sudo in front, if you are logged in as a normal, but power user (the one that has the authority to become a root when need be).
And it is good idea to first not edit any configuration file, but use some new file name to create and edit / play with it.
nano , will create a new file, if it did not exist before. So make a note to use correct spellings and correct path, else it may not edit the desired file and rather create a new one.
Also, it is good idea to take a backup of a production file, just in case, the editing results into unintended results, we can just revert back to original by renaming the backed up file.
Speaking privately with my friend @MichaIng, I was advised that flag B used with Nano, creates the backup file for us. And adding flag C, allows us to specify the backup directory location. Note flags are specified by appending a single dash in front. We can have here both B and C either as -BC or -B -C.
So opening the file with -BC will make a backup at designated location and will also open the file for us to work on.
nano -BC backuppath filename
If we don’t need a backup, having made one earlier, or creating a new file, then we can
This is then going to either open a blank page (if a new file is created) or will list the contents of existing file, wherein, we can then edit the text.
Unlike our windows notepad or notepad ++, we have to learnt few things to navigate our way around.
We can use up / down and sideways arrow keys to go around to the place where we need to edit or add the text and then can also then use backspace to delete and then type in.
Note that most of what I am writing here, is in fact written at the very bottom of the nano session, but I am adding some notes to make it easy for beginners.
If we need to delete a full line, we can move our cursor inside the line anywhere and then simply press Control plus K keys at the same time (in short Ctrl+K or also written as ^K) and this will delete the line. We can thus quickly delete one or more lines this way.
If we need to delete all the text in a file (if we think we messed up big time and need to start over), there is nothing like select all and then hit delete. In such cases, we quit out of our messed up file, and then we can do something like this on the prompt. Suitably add the path to the file, if you are not in the directory itself.
With this we are essentially directing nothingness to the filename, which thus keeps the file and its name, but empties the contents. Some will also use echo command with empty string, like
echo “” > filename
but then why type more than needed. We can also use this above method to empty the log files etc., when troubleshooting.
Now while editing, we may need to go to the end of the line or beginning of the line and we can use ^E and ^A respectively.
Note these control commands are not case sensitive, so you are not pressing control+shift+E etc.
If we need to go to the end of the file, we use ^WV.The key combination is press control and W at the same time, keep both pressed and then press V. Similarly if we use ^WY, it will take us to beginning of the file.
To find out what line our cursor is in, simply press ^C.
And sometimes we have error messages reported which state error at a line number and we need to then go to that line to fix our configuration. We can use ^Shift_ and that is pressing control+shift+underscore sign at the same time and then let go, and then type in the number for our cursor to move to. If we simply pressed ^_, it is going to use control and negative to lower the screen fonts small.
We can make it easy and open nano with -l (this is lower L), like
nano -l filename
and this will open the file with line numbers. There is a way to make it always open with line numbers but we only need it once in a while and in that case, just opening it with parameter of -l will meet our needs.
And if we need to copy the text out of nano and paste into say windows notepad, we can select text while being in nano, by dragging the mouse sideways and up / down, and then it is copied into clipboard and we can then simply paste into notepad by control V.
Similarly selected text under nano, can then be pasted into current cursor location within same nano session by ^U or right mouse clicking.
If we are working on a long file and want to save while we continue working, we can use ^O
Once we are satisfied with our changes, we just do ^x and this will ask us to confirm saving by pressing Yes (or yes or even y) and then configuration file is saved and we are brought back to the prompt.
We will normally need to edit files for things like assigning static IP addresses, persistent default and static routes, persistent DNS servers etc, or for setting up cron jobs (which are reoccurring / scheduled jobs or on reboot activities) ssh configuration, firewalling / IPTables, or webserver/email relay server(MTA) etc, which we will cover under networking tutorial.
This should get us start typing and editing and progress us on our way to learn Linux.