Creating, editing, and saving files
Now that you can log in and move easily between directories and see your files, 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. These programs are not available in Linux. There are several other choices for editors, all of which are free. In this chapter, you will use an editing 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.
Note
If you want to use nano as an editor, it is already available on the Raspbian distribution. For more information on nano, see http://www.nano-editor.org/.
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
on the prompt:
Note that, unlike Windows, Linux doesn't automatically assign file extensions; it is up to you to specify the kind of file you want to create. The Emacs editor also indicates, in the lower-left corner of the screen, that you have opened a new file. Now, if you are using Emacs in the Raspbian GUI 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 mouse 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.
The following are some Emacs keystroke commands that you might find useful:
Now that you have the capability to edit files, you'll use this capability to create programs in the next section.