2 months ago

How to edit text files on a Raspberry Pi

Learning how to edit text files in the console and from the Raspbian GUI is a rite of passage for new Raspberry Pi owners.

It’s all part of learning how computers really work. A secret that most computer makers don’t tell you is that underneath all the gloss and shine of modern operating systems, it’s all text. Every menu, every button, and every setting of every program started as a text document somewhere.

In Raspbian, which is based on Debian Linux, you are far more likely to need to edit text files than on computers running MacOS or Windows. This is a great thing. It lets you get under the hood and really see what’s going on.

How to edit text files on a Raspberry Pi

You may remember the days when computers had text-based interfaces, or you may have only pushed buttons and tapped icons until now. No matter – you will now need to edit text.

There are a lot of different ways to edit text, and everybody has their own favourites. Here were going to look at the two main options: Text Editor (also called Leafpad) and Nano.

STEP-01: Viewing text

We’ve downloaded the release notes for the latest version of Raspbian to view and edit. You can get them from magpi.cc/2ejN6sk. Right-click and save the file to your Pi folder. Open a Terminal window. Open the File Manager and double-click the release_notes.txt file to view it in Text Editor.

STEP-02: Using cat

Often you’ll be working with text files from the command line. The way to do this is using cat (short for ‘concatenate’). Open the Terminal and enter cat release_notes.txt. The text will fly up off the top of the screen. To view the text one page at a time, pipe it through more and press SPACE to move one page at a time.

cat release_notes.txt | more

STEP-03: Open text

You can open a text file in Text Editor from the command line using xdg-open (this will open a file in its default associated app).

xdg-open release_notes.txt

This is a handy compromise between working on the command line and text editing in a friendly graphical environment. It’s the best way to edit text files when you first start.

STEP-04: Nano

You won’t always have access to the GUI when working in the command line, and you should quickly learn to edit text files without heading to the GUI. The way to do this is using a text editing app called Nano.

nano release_notes.txt

STEP-05: Cut and paste

Nano works just like a word processor – although, without a cursor and interface, you may find it quirky. Move around with the arrows and enter and delete text. You can cut lines of text using CTRL+K and paste with CTRL+U.

STEP-06: Save and close

When you’ve finished editing a text file, you save it using the Writeout function. Press CTRL+O and enter Y to save the file. Use CTRL+X to exit and close Nano and return to the command line.

  • Anders Jackson

    No need to use “cat file.txt | more” to see the file content one page at a time.
    Just use “more file.txt” or even “less file.txt”

  • Anders Jackson

    And I prefer Emacs, as it has both text and graphical mode. No need to choose different tools depending if you use text or graphics. “apt install emacs” and you are done.