Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
BeagleBone Black Cookbook

You're reading from   BeagleBone Black Cookbook Over 60 recipes and solutions for inventors, makers, and budding engineers to create projects using the BeagleBone Black

Arrow left icon
Product type Paperback
Published in Nov 2015
Publisher Packt
ISBN-13 9781783982929
Length 346 pages
Edition 1st Edition
Tools
Concepts
Arrow right icon
Toc

Table of Contents (11) Chapters Close

Preface 1. Setting Up for the First Time FREE CHAPTER 2. Basic Programming Recipes 3. Physical Computing Recipes Using JavaScript, the BoneScript Library, and Python 4. Exploring GNU/Linux Recipes Using Bash, Autotools, Debugger, and systemd 5. Basic Programming Recipes with the Linux Kernel 6. Run Faster, Run Real Time 7. Applied Recipes – Sound, Picture, and Video 8. The Internet of Things 9. The Black in Outer Space Index

Edit a text file from the command shell

There is nothing pretty about the text editing tools in Linux. After all, it is a command shell environment; how pretty can it be? But if you are a minimalist at heart, you will feel right at home.

Two editors mostly dominate Developerland: nano and vim. The latter is the most powerful and fully featured. However, the vim command is not a good starting point for neophytes as it is more complex. Instead, our text editing tool of choice for the rest of this book will be nano. In this section, you will learn your way around the editor and then create and edit a text file.

How it works...

When you're using nano, take note of all the options at the bottom of the screen: Exit, Get Help, Read File, and so on. You access these functions with the keyboard shortcuts shown next to each option. Beware that these shortcuts actually may change their function or mode when you navigate to another screen or choose an option. So, remember what the bottom menu says before assuming that the shortcut you just used remains in the same mode.

In the main (default) window, here's a quick rundown on the most commonly used functions and their shortcuts:

  • Ctrl + X: Closes the file.
  • Ctrl + V: Takes you to the next page.
  • Ctrl + Y: Takes you back to the previous page.
  • Ctrl + K: Cuts text (deletes the line of text where your cursor is).
  • Ctrl + U: Uncuts text (undoes the line deletion from the last line that you cut. Repeating this command will keep adding the same line of text that you cut).
  • Ctrl + G: Help menu.
  • Ctrl + C: Shows the current position (gives you line, column, and character coordinates).
  • Ctrl + R: Inserts a file into the file you have open. Now that's a time saver, huh?

I recommend using Ctrl + G periodically when you're just beginning as a reminder for the additional functions available, many of which are powerful and useful. Overall, you will find that even a simple Linux-based text editor such as nano comes loaded with features that most Wintel/Mac desktop versions don't typically include.

How to do it...

This function can be performed through the following steps:

  1. Let's begin by opening nano and simultaneously creating a file named major_tom:
    ~$ nano major_tom
    

    Pow! A window opens. That's the nano interface in all its glory. All right, not so sexy. But very functional, as you can see in the following image:

    How to do it...
  2. On the first line, which is where your cursor should be by default, type (or paste) the following:
    I love my Beaglebone Black so much I want to send it into orbit with Major Tom.
    

    Quickly looking around the window, you will notice the following:

    At the top of the window, you will see the version of nano that you're using and the file name, major_tom.

    At the bottom of the window is an assortment of options.

    If you've played around with any of the options in the menu, ensure that you're back in the main screen where we began with our typed line about Major Tom.

  3. Next, save the file by pressing Ctrl + x; when prompted, type y for yes:

    Error! What a bummer that we couldn't save our work. This is because we didn't first execute nano with the right user privileges. So, try again by closing the file by pressing Ctrl + x; when prompted, type n for no.

We lost our work, but it was only one line of text. We would now have popped back into the command line, and we will have another go; this time, we need a few more steps:

  1. Let's make a custom directory; run this command:
    mkdir bbb_recipe_book
    
  2. Go into this new directory and make a subdirectory; then, make a subdirectory of this one. This may sound overly complicated, but we're just trying to set up some files and directories by organizing principles early. Use the following commands for this:
    cd bbb_recipe_book
    mkdir projects
    cd projects
    
  3. Check your work to ensure that you've set up the directory structure properly. Use the following command:
    pwd
    
  4. Now, let's type sudo nano major_tom.txt. This time, we appended the filename with a text file type. If you don't do this, nano—and Linux—won't know with which application type to associate the file.
  5. In the nano window again, type I love my Beaglebone Black so much I want to send it into orbit with Major Tom., as shown in this image:
    How to do it...
  6. Then, press Ctrl + x; when prompted type y for yes.
  7. Now you will see that the bottom menu has a new prompt, which reads as follows:
    File name to write: major_tom.txt
    
  8. Hit return (Enter) on your keyboard. The file is saved to the new directory we're working in (/home/debian/bbb_recipe_book) and closed simultaneously.

You've now made a very exciting .txt file that you can go back and read whenever you want. Okay, that wasn't terribly challenging. But don't worry; we will be adding more complexity as we proceed in upcoming chapters.

See also

For those who find nano a bit Spartan in functionality and prefer a more powerful editor, vim is a popular choice. Although challenging to learn at first, you may find it to be a more rewarding and flexible choice as a tool. Learn how to use it at http://www.openvim.com/tutorial.html.

You have been reading a chapter from
BeagleBone Black Cookbook
Published in: Nov 2015
Publisher: Packt
ISBN-13: 9781783982929
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image