Kahibaro
Discord Login Register

2.3.2 Editing with nano

Getting Started with nano

Nano is a simple text editor that runs in the terminal. It is designed to be easy for beginners, with most commands visible at the bottom of the screen. In this chapter you will learn how to start nano, edit files, and save your changes, all from the command line.

Opening nano and basic layout

You start nano followed by a filename. If the file exists, nano opens it. If it does not exist, nano creates a new one when you save.

nano notes.txt

When nano opens, you see the file contents in the main area. At the very bottom there are two lines of shortcuts. The ^ symbol means the Control key. For example ^X means hold Control and press X.

The status line just above the shortcut list shows information such as the filename and whether the file has been modified.

Typing, moving, and basic editing

Using nano feels similar to a simple text editor on a desktop. You type to insert text at the cursor position. Backspace deletes characters before the cursor, and the Delete key removes characters after the cursor.

You move around using the arrow keys. You can also use Page Up and Page Down to scroll through the file. Home moves to the start of the line, and End moves to the end of the line.

To insert a new blank line, press Enter. To join two lines together, delete the newline between them using Backspace at the start of the second line.

Saving changes and exiting

The most important actions in nano are saving and exiting. You will use them often.

To save your changes, use:

To write (save) the file in nano, press Ctrl+O, then press Enter to confirm the filename.

After pressing Ctrl+O, nano shows the current filename at the bottom. You can change it if you want to save as a new file. Press Enter to finish saving.

To exit nano, use:

To exit nano, press Ctrl+X. If there are unsaved changes, nano asks whether to save them first.

If nano asks to save, press Y to save or N to discard changes. If you choose Y, nano will then ask for a filename, and you confirm it with Enter.

Creating and editing new files

If you start nano with a filename that does not exist, you get an empty buffer and the filename appears in the status line. For example:

nano todo.txt

You can now type your content. When you press Ctrl+O and then Enter, nano creates the file on disk. This is how you create simple text files directly from the terminal.

You can also open nano without a filename:

nano

In this case, nano uses a placeholder name until you save. When you press Ctrl+O, nano asks you to type a filename. After you enter one and press Enter, nano writes the file.

Cutting, copying, and pasting text

Nano uses its own shortcuts for cutting, copying, and pasting text. In nano, “cut” both removes text from the document and stores it in a buffer, and “paste” inserts that buffered text at the cursor position.

To cut a single line, place the cursor anywhere on that line and press Ctrl+K. The line disappears and is stored in the cut buffer.

To paste what you have cut, move the cursor to the desired location and press Ctrl+U. Nano inserts the text from the cut buffer.

You can cut multiple lines by pressing Ctrl+K several times in a row. Each cut line is added to the buffer, and when you paste with Ctrl+U, nano pastes all cut lines together.

For more precise selections, nano can mark text. Press Ctrl+^ (Control and caret, often typed as Control and Shift and 6) to set the start of a selection. Move the cursor to highlight text. Once selected, you can cut it with Ctrl+K and later paste it with Ctrl+U.

Searching within a file

Nano includes a simple search feature to help you find text in a file. This is useful in configuration files or longer documents.

To search for text in nano, press Ctrl+W, type your search term, then press Enter.

Nano moves the cursor to the next match. To repeat the search for the next occurrence, press Ctrl+W again and then press Enter immediately. Nano uses the previous search term and jumps to the next match.

By default, nano searches forward from the current cursor position. If nano reaches the end of the file without finding a match, it informs you on the status line. You can move to the top of the file with Ctrl+Y (Page Up several times) and search again from there.

Common useful shortcuts

The shortcut list at the bottom of nano shows only some of the available commands. Here are a few that are especially useful for beginners.

To get help inside nano itself, press Ctrl+G. This opens nano’s built in help text with an overview of commands. Scroll through it with Page Up and Page Down, and exit help with Ctrl+X to return to your file.

If you want to go to a specific line and column, for example when following instructions that mention “line 25,” press Ctrl+_ (Control and underscore). Nano will ask for the line and column in the form line,column. You can leave out the column and just press Enter after typing the line number.

If you make a mistake, nano usually provides an undo feature in many distributions. Press Alt+U to undo the last action, and Alt+E to redo it. The Alt key is sometimes labeled as Meta on older documentation, but on most keyboards the Alt key works.

Handling errors and accidental exits

Sometimes you might try to exit with Ctrl+X and worry about losing work. Nano protects you from this by asking whether to save. If you choose N for no, the changes are discarded. If you choose Y, nano saves before exiting.

If you accidentally cut a line with Ctrl+K, you can undo it with Alt+U, or you can paste it back with Ctrl+U if you have not cut anything else since.

If nano reports that it cannot write the file, usually you do not have permission to save in that location. In such cases, exit with Ctrl+X, answer Y to save, then give a filename in a directory where you do have write permission, for example in your home directory.

Working with system files using sudo

System configuration files typically require administrative permissions to edit. You usually combine nano with sudo to edit these files. For example:

sudo nano /etc/hosts

When you use sudo in this way, nano runs with elevated permissions and can write the file. The editing experience inside nano remains the same. Always be careful when changing system files, and make small, clear edits so it is easier to undo changes later if needed.

Practicing nano on your system

To become comfortable with nano, it helps to practice small tasks. Create a simple text file with a list, modify it, and save your changes several times. Experiment with cutting and pasting lines, searching for words, and using undo and redo. With a bit of practice, nano becomes a quick and reliable tool for working with text files directly from the command line.

Views: 6

Comments

Please login to add a comment.

Don't have an account? Register now!