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.
This will open the test.txt file in the nano editor, which shall look like this.
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.
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
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.
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:
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.
How to Save and Exit in Nano Editor in linux
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.
Contact Us