Ubuntu: How to edit files with ‘nano’ text editor

Updated: January 28, 2024 By: Guest Contributor Post a comment

Getting Started with Nano

Nano is a simple, mode-less, WYSIWYG command-line text editor included in most Linux installations. It’s perfect for beginners and advanced users alike. In this tutorial, we will explore the basics of nano, use commands to perform editing tasks, and take advantage of some advanced features.

Opening and Creating Files

To open an existing file or to create a new one, type:

nano filename.txt

If the file does not exist, nano will create a new file with the specified name upon saving.

Basic Nano Commands

Here’s a quick rundown of the essential nano commands:

  • To move the cursor, use the arrow keys.
  • For searching text, use Ctrl+w.
  • To save changes, press Ctrl+o.
  • To exit nano, press Ctrl+x.

Editing Text

Inserting Text

Type to insert text. For new lines, use the Enter key.

Deleting Text

Use the Backspace key to delete text to the left, or Ctrl+k to cut the entire line.

Copy and Paste

Copy a line with Ctrl+k and then press Ctrl+u to paste it.

Undo and Redo

Undo changes with Alt+u and redo with Alt+e.

Searching and Replacing Text

Search for Text

Ctrl+w
Type your search query and press Enter

Search and Replace

Ctrl+\
Enter the search term, press Enter, type the replacement text, and press Enter again.

Advanced Features

Working with Multiple Files

nano -F file1.txt file2.txt

You can switch between the files using Alt+> and Alt+<.

Using Syntax Highlighting

Nano can highlight syntax for different languages. To enable it for a specific file type, you can add include statements to your .nanorc file. For example:

include "/usr/share/nano/html.nanorc"

Customizing Nano

Customize nano behavior by editing your .nanorc file in your home directory. Here’s an example to set smooth scrolling and auto-indenting:

set smooth
set autoindent

Saving and Closing Files

Saving Changes

Ctrl+o
Hit Enter to confirm

This command will write the changes to disk. You can also specify a different filename to save a copy.

Exiting Nano

Ctrl+x

If you have unsaved changes, nano will ask you whether to save them before closing.

Troubleshooting

If you experience issues, you can refer to the nano manual with man nano, or check the available settings in the .nanorc file.

Conclusion

Nano offers a straightforward and efficient way to quickly edit text files on Ubuntu. Whether you’re editing configuration files or writing code, nano has the features to assist you confidently. The more you use it, the more proficient you’ll become.