Terminal 101: Writing and Editing Docume
This week, we're teaching you how to write and edit text documents right from within Terminal, as well as how to navigate directories from within the command line.
Before you continue, you'll need to have a bit of knowledge about the basic syntax for navigating around the command line. We've got a handy little primer here.
There are many built-in command line text editors on the Mac, including Vi and Emacs, but we'll stick with an easy-to-use text editor: nano.
Creating a New Document
To create a new document, fire up the Terminal application and navigate to the directory where you want the document to be created; then, type in nano and press enter on your keyboard.
This command will open the nano text editor. With its simple interface, you can begin typing at the start of your document. Use the keyboard's arrow keys to move throughout the document, and use the enter key to create a new line.
Loading an Existing Document
After navigating to the location where a document currently exists, type in nano, followed by a space, and then the filename of the document you wish to open. So, for instance, if we had a document named "mydocument.txt," we would type the following command into Terminal:
Nano will then open the existing document for editing. You can make any changes you'd like to the document, and then re-save it using the following command.
To save your document in nano, press Control + O. Nano will then prompt you for the "Filename to Write." Type in your document's filename, and then press enter.
After a second or two, nano will let you know that the file has been written out, and will let you know how many lines were in the document. You will be able to find your saved document in the Finder.
Documents created within the nano program are sharable between Mac, Windows, and Linux systems because they are plaintext (.txt) filetypes.
After saving your document within nano, the file will be visible in the Finder, ready for you to share through email, upload to a website, or send through a file transfer in iChat. Anywhere you can share a plaintext file, you can share documents created within nano.
Cory Bohon is a freelance technology writer, indie Mac and iOS developer, and amateur photographer. Follow this article's author, Cory Bohon on Twitter.