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:
- Let's begin by opening
nano
and simultaneously creating a file namedmajor_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: - 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
. - 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, typen
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:
- Let's make a custom directory; run this command:
mkdir bbb_recipe_book
- 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
- Check your work to ensure that you've set up the directory structure properly. Use the following command:
pwd
- 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. - In the
nano
window again, typeI love my Beaglebone Black so much I want to send it into orbit with Major Tom.
, as shown in this image: - Then, press Ctrl + x; when prompted type
y
for yes. - Now you will see that the bottom menu has a new prompt, which reads as follows:
File name to write: major_tom.txt
- 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.