Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Shell Scripting
  • Kali Linux
  • Ubuntu
  • Red Hat
  • CentOS
  • Docker in Linux
  • Kubernetes in Linux
  • Linux interview question
  • Python
  • R
  • Java
  • C
  • C++
  • JavaScript
  • DSA
Open In App
Next Article:
How to Save and Exit in Nano Editor in linux
Next article icon

How to Save and Exit in Nano Editor in linux

Last Updated : 29 Nov, 2023
Comments
Improve
Suggest changes
Like Article
Like
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.


Next Article
How to Save and Exit in Nano Editor in linux

O

owl0223
Improve
Article Tags :
  • Linux-Unix
  • Geeks Premier League
  • Geeks Premier League 2023

Similar Reads

    How to Delete Line in Nano Editor?
    In a terminal environment, Nano is a straightforward and widely used text editor for Linux and macOS systems. Deleting lines in Nano is a basic yet essential operation, allowing for efficient editing within files. By following simple keyboard shortcuts like "Ctrl+K," you can swiftly remove lines fro
    5 min read
    How to Save Output of Command in a File in Linux?
    Linux has a robust command-line interface (CLI) that makes system interaction efficient for users. Saving a command's output to a file is among the most frequent activities in Linux. When working with big outputs, generating logs, or needing to save command results for later use, this is quite helpf
    4 min read
    How to undo in Vim editor in Linux
    Vim (short for Vi Improvised) is a popular text editor mainly used in Linux and Unix-based operating systems but due to having a CLI (command line interface), it might be difficult for new users to make use of basic features such as undo in Vim. So in this article, we will learn different ways and t
    3 min read
    How to Install Atom Text Editor in Linux?
    "Atom - A Hackable Text Editor of 21st Century", as the line indicates Atom is an open-source, cross-platform, feature enrich text editor developed by Github. Atom has attractive features like cross-platform editing, built-in package manager, smart auto-completion, file-system browser, and many more
    3 min read
    Nano Text Editor in Linux
    In the vast world of Linux text editors, Nano stands out as a simple yet powerful tool for editing files directly from the command line interface. Whether you're a novice user or an experienced developer, Nano offers a straightforward and efficient editing experience. In this article, we'll delve in
    8 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences