Mastering the Nano Text Editor on Linux

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.

nano-save

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.

nano-save-as

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.

nano-searcg

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.

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.

7 comments

  1. I love Nano, but having to switch back and forth between Nano and other programs can really mess with ya. “Cntrl-W” to search… Note to self: I need to remember to to change my key bindings on my servers.

  2. I love Nano, but having to switch back and forth between Nano and other programs can really mess with ya. “Cntrl-W” to search… I know you can change the key binding though, I’m just lazy.

  3. Sorry, I don’t quite understand… (I am a Linux newbie!)
    Why would I use something as primitive as Nano when I can use LibreOffice to create any text file that I want? Is this Nano something to be used only by those who use a command line and no GUI?

    • nano is particularly useful on a server environment, or even on a headless Raspberry Pi setup. It is lightweight and work on all Linux distros, regardless of the desktop environment.

    • Yes, nano is for the command line when you don’t have a GUI. If you are familiar with Windows then nano is like notepad (but for the command line) and LibreOffice is like Word. You don’t need to start a full blown word processor to edit a text file.

    • Libre/OpenOffice are a word processor that add fonts and other way of set how text in the finished print should look like. That means that it adds a lot of information that isn’t text in the file.

      nano is a text editor, which only edit text which isn’t suposed to be printed. It doens’t change fonts is the text. The text is then used by other programs, compilers etc to read the text to change its behaviour. They do not use and demands there is no extra information except the characters in the saved file.

      Try more on a .odt-file saved by OpenOffice and then on a .txt-file saved by nano and you will know the difference.

Comments are closed.

Sponsored Stories