Creating, editing, and saving files on the BeagleBone Black

Now that you can log in and move easily between directories and see which files are in your directories, 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 in Microsoft Windows, you probably have used a program like Microsoft Notepad, Wordpad, or Word to do this. As you might imagine, these are not available in Linux. There are several choices, all of which are free. I am going to show you how to use an editor program called Emacs. Other possibilities are programs like nano, vi, vim, and gedit. Programmers have strong preferences about which editor to use, so if you already have a favorite, you can skip this section.

Prepare for lift off

If you want to use Emacs, then download and install Emacs by typing sudo apt-get install emacs.

Engage thrusters

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, then Emacs will create the file. Here is what you will see if you type emacs example.py at the prompt:

Engage thrusters

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

However, if you are running Emacs from SSH, you won't have the mouse available, so you'll need to move around 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 you would normally use the mouse to select. For example, when you are ready to save the file you'll use Ctrl + X then Ctrl +S, and that will save the file under the current filename. When you want to quit Emacs you'll use Ctrl + X then Ctrl + C. This will stop Emacs and return you to the command prompt. If you are going to use Emacs inside of Emacs, here are a number of keystroke commands you might find useful:

emacs command:

What it does

Ctrl + X and Ctrl + S

Save: Saves the current file.

Ctrl + X and Ctrl + C

Quit: Exits Emacs and returns to the command prompt.

Ctrl + K

Kill: Erases the current line.

Ctrl + U

Undo: Reverts the last action.

Left mouse button: text selection

Cursor: right mouse button

Cut and paste: If you select the text you want to paste with the mouse using the left mouse button, then move the cursor to where you want to paste the code, then hit the right mouse button, the code will be pasted to that location.

Objective complete – mini debriefing

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
18.118.0.248