Introduction
In this lab, you will learn the fundamentals of text editing in Linux using the nano editor. The nano editor is a simple, user-friendly text editor that is available by default on most Linux distributions. It provides an intuitive interface for creating and modifying text files directly from the command line.
Text editing is an essential skill for Linux users, as many configuration files and scripts need to be created or modified through a text editor. By mastering nano, you will be able to efficiently edit text files, make changes to configurations, and create scripts to automate tasks in Linux.
Creating and Opening Files with Nano
In this step, you will learn how to create a new text file and open it with the nano editor. The nano editor provides a simple interface for text editing in the terminal.
First, navigate to your project directory:
cd ~/project
You should now be in the /home/labex/project directory. Let's create a new file named notes.txt using the touch command:
touch notes.txt
The touch command creates an empty file if it doesn't exist. Now, let's open this file with the nano editor:
nano notes.txt
After running this command, you should see the nano editor interface in your terminal. It looks like this:
GNU nano 6.2 notes.txt
^G Help ^O Write Out ^W Where Is ^K Cut ^J Justify
^X Exit ^R Read File ^\ Replace ^U Paste ^T To Spell
The nano interface displays the filename at the top and a list of available commands at the bottom. The ^ symbol represents the Ctrl key. For example, ^X means pressing Ctrl + X to exit the editor.
Type the following text in the editor:
This is my first file using nano editor.
Linux text editing is straightforward!
Now, let's save the file and exit the editor:
- Press
Ctrl + Xto exit - You will be asked if you want to save the modified buffer. Press
Yto confirm. - Press
Enterto confirm the filename and save the file.
You have successfully created and edited a file using nano. Let's verify the content of the file using the cat command:
cat notes.txt
This should display the text you just entered.
Editing and Formatting Text in Nano
In this step, you will learn how to edit and format text in an existing file using nano. Proper formatting with comments and blank lines makes your text files more readable and organized.
Let's open the file we created in the previous step:
nano ~/project/notes.txt
The file should still contain the text you entered previously. Now let's modify it to include comments and improve its organization. In many configuration files and scripts, lines starting with # are considered comments.
Use the arrow keys to navigate to the beginning of the file and add the following content:
## Notes on Linux Text Editing
## Created: Current Date
This is my first file using nano editor.
Linux text editing is straightforward!
## End of Notes
The cursor in nano can be moved using the arrow keys. You can add new lines by pressing Enter and delete characters using Backspace or Delete.
Now, let's save the changes and exit nano:
- Press
Ctrl + Xto exit - Press
Yto save the modified buffer - Press
Enterto confirm the filename
Let's check our file again to see the changes:
cat ~/project/notes.txt
You should see the formatted content with comments and blank lines. Adding comments and organizing your text with blank lines makes the file more readable and helps others understand the purpose of the file.
Navigating and Editing in Nano
In this step, you will learn more advanced navigation and editing techniques in the nano editor. These skills will help you edit files more efficiently.
Let's create a new file with more content to practice these techniques:
nano ~/project/practice.txt
Type or copy the following content into the file:
Line 1: This is the first line of text.
Line 2: This is the second line of text.
Line 3: This line has a typo that we wil fix.
Line 4: This line will be deleted.
Line 5: This is the last line of text.
Now, let's practice some navigation and editing commands:
Moving the cursor:
- Use arrow keys to move up, down, left, and right
- Press
Ctrl + Ato move to the beginning of the current line - Press
Ctrl + Eto move to the end of the current line - Press
Ctrl + Vto move down one page - Press
Ctrl + Yto move up one page
Editing text:
- Navigate to "Line 3" where it says "wil" (missing an 'l')
- Use the arrow keys to place the cursor after the 'i' in "wil"
- Type the missing 'l' to correct "wil" to "will"
Deleting text:
- Navigate to "Line 4"
- Press
Ctrl + Kto cut (delete) the entire line
Adding text:
- Navigate to the end of the file (after "Line 5")
- Press
Enterto create a new line - Type:
Line 6: This is a new line I added.
After making these changes, save the file and exit nano:
- Press
Ctrl + Xto exit - Press
Yto save changes - Press
Enterto confirm the filename
Let's check our edited file:
cat ~/project/practice.txt
You should see the corrected text without Line 4 and with the new Line 6 added.
Searching and Replacing Text in Nano
In this step, you will learn how to search for text and replace it in the nano editor. These features are useful when you need to find specific information or make consistent changes throughout a file.
Let's create a new file to practice searching and replacing:
nano ~/project/config.txt
Enter the following content:
## Server Configuration
server_name = myserver
port = 8080
max_connections = 100
timeout = 30
log_level = info
server_path = /var/www/html
backup_path = /var/backups
Now, let's learn how to search for text:
- Press
Ctrl + Wto open the search function - Type
serverin the search prompt and pressEnter - Nano will move the cursor to the first occurrence of "server"
- To find the next occurrence, press
Ctrl + Wagain and then pressEnterwithout typing anything
Next, let's try replacing text:
- Press
Ctrl + \(backslash) to open the replace function - Type
infoas the text to search for and pressEnter - Type
debugas the replacement text and pressEnter - When asked "Replace this instance?", press
Yto confirm
Now, let's change all occurrences of a specific text:
- Press
Ctrl + \again - Type
8080as the text to search for and pressEnter - Type
9090as the replacement text and pressEnter - When asked "Replace this instance?", press
Yto confirm
Save the file and exit nano:
- Press
Ctrl + Xto exit - Press
Yto save changes - Press
Enterto confirm the filename
Let's check our edited file:
cat ~/project/config.txt
You should see that "log_level = info" has been changed to "log_level = debug" and "port = 8080" has been changed to "port = 9090".
Summary
In this lab, you have learned the fundamentals of text editing in Linux using the nano editor. Here are the key skills you have acquired:
- Creating and opening files with nano
- Basic text editing and formatting with comments and blank lines
- Navigating through text using keyboard shortcuts
- Editing techniques like inserting, deleting, and modifying text
- Searching for specific text within a file
- Replacing text occurrences
These text editing skills are essential for working with Linux systems, as many configuration files, scripts, and documentation need to be created or modified through a text editor. The nano editor provides a simple and accessible way to perform these tasks directly from the command line.
As you continue your Linux journey, you will find these text editing skills valuable for tasks such as:
- Editing configuration files for various applications
- Creating and modifying shell scripts
- Taking notes or documenting your work
- Making quick changes to text files without using a graphical interface
Now that you have mastered the basics of the nano editor, you can confidently work with text files in a Linux environment.



