Worksheet 2 - Nano commands
Command challenge exercise using nano on a Raspberry Pi
Each challenge introduces the basics of the nano editor and teaches students common tasks, helping them become comfortable with the command-line text editor.
Note, that if you outside the /home/pi (user home directory) you will need to preface each instruction with sudo so that you have superuser privileges.
For these examples create a testing folder in the /home/pi/nano_test so that you can practise these activities.
Open a File in nano
Instruction: Open an existing file using nano.
Command: nano filename.txt
Challenge: Open a file called welcome.txt and add the text "Welcome to the Raspberry Pi" at the beginning.
Save and Exit nano
Instruction: Learn how to save a file and exit.
Command: Press CTRL + O (to save) and CTRL + X (to exit).
Challenge: After modifying welcome.txt, save the changes and exit nano.
Search for Text
Instruction: Use nano's search feature to find specific words or phrases.
Command: Press CTRL + W and type the word you want to find.
Challenge: Search for the word "Raspberry" in welcome.txt and confirm its position.
Cut and Paste Text
Instruction: Use nano to cut and paste text.
Command: Move the cursor to the start of the line and press CTRL + K (cut) and CTRL + U (paste).
Challenge: Cut the line that contains "Welcome to the Raspberry Pi" and paste it at the end of the file.
Copy and Paste Text
Instruction: Use the copy and paste commands in nano.
Command: Press CTRL + Shift + 6 to start selecting, then CTRL + K to copy and CTRL + U to paste.
Challenge: Copy the first two lines of the file and paste them at the end.
Copy and Past Text using the Mouse
You can also copy and paste text using the Right-Mouse-Button on the mouse. This is useful when you need to copy code from a web page into nano.
Undo and Redo Changes
Instruction: Undo and redo changes in nano.
Command: Press ALT + U to undo and ALT + E to redo.
Challenge: Undo the last action you performed, and then redo it.
Jump to a Specific Line
Instruction: Jump to a specific line number in the file.
Command: Press CTRL + _ (underscore), then type the line number.
Challenge: Jump to line 5 in welcome.txt.
Insert the Current Date and Time
Instruction: Insert the current date and time in nano.
Command: Press CTRL + T (in some configurations) or manually type the date.
Challenge: Insert today's date and time at the end of the file.
Open nano in Read-Only Mode
Instruction: Learn how to open a file in read-only mode.
Command: nano -v filename.txt (use -v for read-only).
Challenge: Open the welcome.txt file in read-only mode and attempt to modify it. Note what happens.
Enable Syntax Highlighting
Instruction: Enable syntax highlighting in nano (for Python or other languages). To enable syntax highlighting in nano on a Raspberry Pi (or in most Linux environments), follow these steps to modify the ~/.nanorc configuration file:
Open the nano configuration file: You need to edit or create a file called .nanorc in your home directory to enable syntax highlighting.
nano ~/.nanorc
Include syntax definitions: Add the necessary syntax definitions for the languages you want highlighted. You can either manually write these or include existing files provided by nano. For instance, to enable Python highlighting, add the following line:
include /usr/share/nano/python.nanorc
You can include definitions for other languages too, like:
include /usr/share/nano/c.nanorc
include /usr/share/nano/sh.nanorc
include /usr/share/nano/html.nanorc
Save the file: Once you've added the syntax highlighting rules, press Ctrl + O to write the changes, then press Enter to confirm, and finally press Ctrl + X to exit nano.
Test your setup: Open a Python or another supported file with nano, and you should now see syntax highlighting applied automatically.
Challenge:
- Enable syntax highlighting for at least 3 different languages (e.g., Python, C, HTML).
- Create a Python script in nano and check if the syntax highlighting is working.