A routine part of managing a Linux system, including the Raspberry Pi, is editing text files. Many of the different services and administrative options are configured using text files. From setting the IP address to configuring a service like Samba, tweaking a configuration file is almost always needed.

There are many different text editors available for Linux with the most popular being nano, vi and emacs. Each has its own strengths and its own weaknesses. Emacs is a powerful editor with a steep learning curve. Vi (or vim, a vi clone with various improvements) is available on almost every Linux and Unix-type installation (including FreeBSD and Solaris), often when no other editors are installed. However, Vi isn’t intuitive until you learn some of the basic ideas behind its construction. Vi doesn’t behave like a “normal” text editor. For example, to insert text, you must first press ‘i’ and so on. Nano is a simple text editor that works just as you would expect. Without much training, you can edit simple files and save them. It has a range of different editing features including searching and cut/paste.

To edit an existing file or create a new file, start nano and pass the name of the file as the first parameter:

Inside nano, the basic features are very much as you would expect. Type text to insert it into the file, press ENTER to get a new line, use backspace to delete text and use the cursor keys to move around.

To save and exit, press the Control key (CTRL) together with the X key (i.e. CTRL+X). You will be asked if you wish to save the modified buffer. Press Y. You will then be asked to confirm the file name. If you want to perform a “Save as,” this is where you should enter the new name. Press ENTER to accept the current filename.

While editing the file, you can save it to disk and continue editing by pressing CTRL+O. As with CTRL+X, you can perform a “Save as” by entering a new name or press Enter to accept the current filename.

To find text in the current file, press CTRL+W, enter the wanted text and press ENTER. To see the next occurrence of that text, press ALT+W. If you want to perform a find and replace, press ALT+R, enter the text to find and press ENTER. Now enter the replacement text and Press ENTER again. You then have the option to replace all occurrences of the text (press A) or to just replace the current instance (press Y). You can use CTRL+C at any time to cancel the find and replace.

To move a line from one place in the file to another (i.e. cut and paste), press CTRL+K to cut the line. Move the cursor and then press CTRL+U to paste it. If you want to move several lines, then press CTRL+K repeatedly until all the lines you desire have been cut. Move the cursor and then press CTRL+U to paste back all the lines you cut.

The procedure is the same for copy and paste, but you use ALT+6 to copy the lines and then CTRL+U to paste them.

To jump to the first line in the file, press ALT+\ and to jump to the last line, press ALT+/. To go to a specific line, press CTRL+_ and then type the desired line number followed by ENTER.

Summary

Here is a brief summary of the most useful operations.

  • CTRL+X – Save and exit
  • CTRL+O – Save file
  • CTRL+W, ALT+W – Find and repeat find
  • ALT+R – Find and replace
  • CTRL+K, CTRL+U – Cut and paste
  • ALT+6, CTRL+U – Copy and paste
  • ALT+, ALT+/ – Top and bottom of file
  • CTRL+_ – Go to a specific line

Nano also has a built-in help page which you can access by pressing CTRL+G or F1. If you can’t remember the key combination needed for any particular operation, then the help page will provide you with the information you need.

Gary has been a technical writer, author and blogger since 2003. He is an expert in open source systems (including Linux), system administration, system security and networking protocols. He also knows several programming languages, as he was previously a software engineer for 10 years. He has a Bachelor of Science in business information systems from a UK University.

Our latest tutorials delivered straight to your inbox