Alex Kaloostian

Apple Certified Master Trainer | Systems Integrator | Video Editor | Motion Graphics Artist

Command Line Basics 9 – reading and editing text

Leave a comment

Everything you do in the command line is text-based, of course, but did you know you can also create and edit text files right in the command line? It’s true, there are a whole slew of text editors built right into the command line.

This is by no means  complete list, but some of the tools I like best.

Reading text files:

less filename.txt
more filename.txt
tail filename.txt
cat filename.txt

Less will open and display the first screen-full of a text file. You can use the down arrow to scroll down and see more, or the up arrow to scroll back. Press q to quit and return to the command line.

More will do the same, with a difference: more will load the whole file into memory first. This makes scrolling smoother, but opening a BIG file can take longer. On modern computers though, it makes little difference.

Cat will open multiple files and concatenate them together, end to end.

Tail will open a text file and immediately jump to the bottom. You can use the mouse to scroll back up.

Tail is useful for reading log files: usually you’re only interested in the stuff that just happened, the stuff at the end. If you use

tail -f filename.txt

Tail will stay running, and the log will scroll across your screen in real-time. Cool! Press q to quit, as always.

For creating and editing files, three common tools are

vi filename.txt
nano filename.txt
pico filename.txt

They’re all pretty similar, basic word processors. They all have their fans. Personally, I like nano, and I’m going to show you why. Type

nano newfile.txt

and press return. You will create a new file and the nano interface will appear on your screen:

See how all of the commands are displayed for you at the bottom of the screen? That’s why I like nano. The ^ symbols stand for the control key; to Exit, you press control-X, to cut text, Control-K, and so on. Type a few lines:

To save, use the Write Out command, Control-O:

And press return. Then to exit, press Control-X. If you try to exit without saving first, it will ask you if you want to save, and you can type Y or N for yes or no.

That’s about it, now you’ve got yourself a brand-spankin’ new text file, which you can do with as you wish.

If you type

nano filename.txt

And the file already exists, it will be opened. if you type a file name that does NOT exist, the file will be created. Spellcheck? Tables? Graphics? Psh. Hemingway didn’t need any of those girly tools, and neither do you!

Beyond just feeling geeky though, there are some very good reasons for using the command line to write text. Config files and scripts need to be plain text, but word processors such as Microsoft Word and Pages and even TextEdit will save your file as .doc or .rtf by default, then you have too remember to convert them. If you don’t do it right, you could corrupt the config file you’re trying to edit! Best to just leave them in plain text from the beginning, by using one of these command line tools.

Author: alexkaloostian

I'm a video editor, motion graphics designer and Mac IT consultant in the Boston area.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s