Open In App

How to Save and Exit in Nano Editor in linux

Last Updated : 29 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Saving and exiting in the Nano text editor in Linux is a fundamental skill for anyone working with text files in the terminal. Nano is a user-friendly and straightforward text editor, making it an excellent choice for both beginners and experienced Linux users. Whether you’re editing configuration files, writing scripts, or taking notes, knowing how to save and exit in Nano is crucial. In this guide, we’ll walk you through the steps to save your work and exit the Nano editor gracefully.

Pre-requisites in Nano Editor

Access to a Linux machine. (In this article, we are using a Debian-based Linux machine, you are free to use any of your choice but, make sure to use the package manager accordingly)

Installing the Nano editor in Linux

Generally, Nano Editor is installed in most Linux machines by default. However, if you do not have it, you can install it with the following simple command:

sudo apt install nano

This will install the Nano editor on your machine. Now, there are two ways we can save and exit in Nano editor in Linux, both of which are explained below.

Method 1: Saving and Exiting in Nano Editor in Linux

Step 1: Opening a file in the Nano editor in Linux

Now, to open a file in the nano editor, you have to type the nano command followed by the absolute/relative path of the file you want to open. If the filename passed does not exist then, nano will create a new file in the target directory. The syntax is as follows:

nano <path to the file you want to open>

In this example, we are using a file called test.txt and we open it using nano as given below.

Using nano to open a file

Using Nano to open a file

This will open the test.txt file in the nano editor, which shall look like this.

Picture2

nano editor interface

Now, we have this dummy content already stored in the file. What we now intend to do is that we write some new content to this file and then, save it and exit it successfully.

Step 2: Saving file and exiting the nano editor in Linux

We will add these lines in the file used above.

This is dummy line. 
Added after original text.

Once the file is opened, write the above lines after the original lines as you would type in any field. Nano does not require special inputs to allow writing to file. Once, the lines are added you simply click Ctrl+S, saving your buffer to a file.

Picture4-(2)

Saving buffer ta o file.

Once the content is successfully saved, you will the message [Wrote x lines] this indicates that all the written lines are saved into the file and you can now exit the editor. To exit the editor, you simply press the following key combination:

cntrl + X

The same is listed in the left bottom of the editor. Now to verify whether the data has been saved, you can use the cat command to get its contents.

cat test.txt
Picture5

Verifying the saved data

As can be seen, the data has been saved in the file successfully.

Method 2: An Alternative Way to Save and Exit in Nano Editor

We saw one way of saving and exiting files in the nano editor in the previous method now, we shall look at the other way.

Firstly, we shall open a file as explained in step 1 of method 1 then, we shall add some new data to the file. The new data is as follows:

This is second method's data.
Keep Geeking!

Now, type in the above data. Once, the data is typed in, we need to press the key combination to exit:

Ctrl+X

After this, the editor will not exit straightforwardly but, will ask the user whether to save the entered data (buffer) or not. You click:

  • Y – For saving.
  • N – For not saving.
  • Ctrl+C – For canceling the exit operation and returning to the editor.
Picture6

Choosing whether to save the buffer or not

Now, our goal is to save the data and then, exit, we will click ‘Y’ and the nano editor will give us another prompt to confirm the name of the file we want to save the edited data to:

Picture7

Confirming the file name to save data into.

Now, in the bottom highlighted line, you get the option to change the file you want to save the new data to. If you keep the same name as the original file, the data will be edited to the same file else, if you wish to change the file name then, nano will create a new file in the same directory and save the data to it; the original file shall remain unchanged.

We do not want to change the file name so, we will just press Enter and our data will be saved and the nano will exit.

Again to verify this, we can use the same cat command as mentioned in method 1.

Picture8

Verifying file after saving data.

Conclusion

In this article, we learned how to install the Nano editor in Linux. Then, we explained how to open a file and edit it in the Nano editor. After that, we saw two different methods to save data to the file and then, exit the nano editor. We also verified the data saved using the cat command. For any further assistance with the nano editor, you can use the key combination Ctrl+G inside the nano editor, this displays the documentation of nano.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads