Open a File for Editing in Nano

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
Existing file opened in Nano editor

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.

💡
To exit nano editor, you have to press Ctrl+X. It will ask if you if you want to save changes or not. Press N to discard any changes you made and exit the editor.

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.

💡
If you do not make any changes to a file, new or otherwise, pressing Ctrl+X will close the nano editor immediately, without asking to save the file. That makes sense, right? When there are no changes, why ask to save them?

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

💡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

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.