Worksheet 2 - Nano commands: Difference between revisions

From Sensors in Schools
Jump to navigation Jump to search
 
(5 intermediate revisions by the same user not shown)
Line 3: Line 3:
Each challenge introduces the basics of the nano editor and teaches students common tasks, helping them become comfortable with the command-line text editor.
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.
Note, that if you are 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.
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==
==Open a File in nano==
Line 46: Line 46:
Instruction: Use the copy and paste commands in nano.
Instruction: Use the copy and paste commands in nano.


<syntaxhighlight lang="bash">
* CTRL + 6 to mark the start of the selection.
Command: Press CTRL + Shift + 6 to start selecting, then CTRL + K to copy and CTRL + U to paste.
* Once you have made the selection, you can use ALT + 6 to copy it.
</syntaxhighlight>
* You can paste all your copied data within the nano text editor by pressing CTRL + U  


Challenge: Copy the first two lines of the file and paste them at the end.
Challenge: Copy the first two lines of the file and paste them at the end.


==Copy and Past Text into Nano 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==
==Undo and Redo Changes==
Line 71: Line 73:


Challenge: Jump to line 5 in welcome.txt.
Challenge: Jump to line 5 in welcome.txt.
==Insert the Current Date and Time==
Instruction: Insert the current date and time in nano.
<syntaxhighlight lang="bash">
Command: Press CTRL + T (in some configurations) or manually type the date.
</syntaxhighlight>
Challenge: Insert today's date and time at the end of the file.





Latest revision as of 10:21, 22 October 2024

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 are 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.

  • CTRL + 6 to mark the start of the selection.
  • Once you have made the selection, you can use ALT + 6 to copy it.
  • You can paste all your copied data within the nano text editor by pressing CTRL + U

Challenge: Copy the first two lines of the file and paste them at the end.

Copy and Past Text into Nano 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.


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.