Open a File for Editing in Nano
In the first chapter of the Nano series, learn to open existing or new files with Nano editor.
This is the first thing you would do with Nano editor, open a file.
And that is actually quite simple.
nano filename
: It will open the filename irrespective of whether it exists or not (I'll explain it in a bit)nano
: Just typing nano will open the editor, like opening a new document for writing
Open an existing file with Nano
You can open an existing file in Nano editor by just providing its name in this fashion:
nano filename
For example, I have this file test.sh
in my home directory and I opened it in Nano:
nano test.sh
![Opening a file in nano editor](https://linuxhandbook.com/content/images/2024/06/nano-open-file-for-editing-1.png)
Notice the bottom part of the editor? That's the beauty of Nano editor. It gives you hints on keyboard shortcuts to use. And these suggestions change based on what you are trying to do.
^ means control key. ^G means you have to press Ctrl+G keys together.
Unlike Vim, there are no modes in Nano. You can start editing it immediately.
You can also open a file that is located in some other directory by specifying its absolute or relative path:
nano /path_to_file
Nano opens a file even if it doesn't exist
Weird statement? Not really.
I hope you have some idea about absolute and relative paths. If the system cannot find the filename (either it doesn't exist or it is not in your present directory), Nano will open the editor with the give filename but nothing in it.
The file is not created at this stage. It is only in the buffer. If you do not make any changes or discard them, the file creation doesn't take place. If you save it, a new file with the given filename will be created.
So, I open a file named non-existing-file.txt
which doesn't exist.
nano non-existing-file.txt
And Nano editor opens it. If you look at the file name, you can see the provided name. At the same time, it says 'New File' at the bottom indicating that it is a brand new file.
![Open a new file in Nano by giving its name](https://linuxhandbook.com/content/images/2024/06/open-new-file-with-name-nano.png)
Open a new file in Nano editor
You can open a new file in Nano with its name (the scenario discussed in the above section):
nano filename
Or, you can just enter nano and make changes and give the file a name when you save and exit:
nano
![Open a new file without name in Nano](https://linuxhandbook.com/content/images/2024/06/nano-open-new-file-1.png)
π‘Bonus tip: Open a file in view only mode
There are several ways to view the file content in Linux command line. The cat and less commands are the most common tools for this purpose.
You can also use Nano to read a file. Nano has a view-only mode, which prevents accidental changes when you just want to read the content.
nano -v filename
If you try making any changes by pressing any keys other than the movement ones, you'll see a warning
Key is invalid in view mode
![View only mode in Nano](https://linuxhandbook.com/content/images/2024/06/nano-view-only-mode.png)
You press the same Ctrl+X keys to exist the view mode.
Conclusion
Alright! So you now know a little bit about opening file in the Nano editor. Next, you should learn about exiting the nano editor in detail. The next chapter tells you how you can just save the changes, save and exit or discard and exit the editor.
Creator of Linux Handbook and It's FOSS. An ardent Linux user & open source promoter. Huge fan of classic detective mysteries from Agatha Christie and Sherlock Holmes to Columbo & Ellery Queen.