Creating, editing, and saving files on Raspberry Pi

Now that you can log in and move easily between directories and see which files are in them, you'll want to be able to edit those files. To do this, you'll need a program that allows you to edit the characters in a file. If you are used to working on Microsoft Windows, you have probably used programs such as Microsoft Notepad, WordPad, or Word to do this. As you can imagine, these programs are not available in Linux. There are several choices for editors, all of which are free. In this chapter, we will use an editor program called Emacs. Other possibilities are programs such as nano, vi, vim, and gedit. Programmers have strong feelings about which editor to use, so if you already have a favorite, you can skip this section.

If you want to use Emacs, download and install it by typing sudo apt-get install emacs. Once installed, you can run Emacs simply by typing emacs filename, where filename is the name of the file you want to edit. If the file does not exist, Emacs will create it. The following screenshot shows what you will see if you type emacs example.py in the prompt:

Creating, editing, and saving files on Raspberry Pi

Notice that unlike Windows, Linux doesn't automatically assign file extensions; it is up to us to specify the kind of file we want to create. Notice that the Emacs editor has indicated, in the lower-left corner, that you have opened a new file. Now, if you are using Emacs in the LXDE windows interface, either because you have a monitor, keyboard, and mouse hooked up or because you are running vncserver, you can use the mouse in much the same way as you do in Microsoft Word.

However, if you are running Emacs from SSH, you won't have the pointer available. So you'll need to navigate the file using the cursor keys. You'll also have to use some keystroke commands to save your file as well as accomplish a number of other tasks that you would normally use the mouse to select. For example, when you are ready to save the file, you must press Ctrl + X and Ctrl + S, and that will save the file under the current filename. When you want to quit Emacs, you must press Ctrl + X and Ctrl + C. This will stop Emacs and return you to the command prompt. If you are going to use Emacs, the following are a number of keystroke commands you might find useful:

The Emacs command

What it does

Ctrl + X Ctrl + S

Save: This command saves the current file

Ctrl + X Ctrl + C

Quit: This command causes you to exit Emacs and return to the command prompt

Ctrl + K

Kill: This command erases the current line

Ctrl + _

Undo: This command undoes the last action

Left-click and text selection followed by cursor placement and right-click

Cut and paste: If you select the text you want to paste by clicking the mouse, move the cursor to where you want to paste the code and then right-click on it; the code will be pasted in that location

Now that you have the capability to edit files, in the next section, you'll use this capability to create programs.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset