Terminal 101: Manipulating Text
Terminal 101: Manipulating Text
Every Monday, we'll show you how to do something new and simple with Apple's built-in command line application. You don't need any fancy software, or a knowledge of coding to do any of these. All you need is a keyboard to type 'em out!
Sometimes, we take all the fantastic things we can do with text in a word processor for granted. Clicking, dragging, copying, pasting--these are all things that utilize the visual interface on your Mac, but you can do them in Terminal without having to launch an application. You can the pbpaste and pbcopy commands to count the number of lines or words in the text, reverse it, tidy up HTML code, and create a new text file on your computer. Read on to learn how. Continue reading, and weâll show you how to do this and more using the pbpaste and pbcopy commands.
1. Print the Pasteboard Contents
To view the current contents of the pasteboard, all you need to do is enter the following command into the terminal:
pbpaste
The contents will be printed to the terminal for your viewing pleasure. This will work with any text that has been copied to the OS X pasteboard (either from an app or the terminal).
2. Count the Bytes, Lines, and Words
Using the piping technique that we covered earlier, you can pipe the contents of the pbpaste command to the wc command so that you can perform a word and line count on the contents of the clipboard. To do this, type the following command into the terminal:
pbpaste | wc
The output that is printed to the screen will contain the number of bytes in the first column (0 because this isnât an actual file), the number of lines in the second column, and the number of words in the final column.
3. Reverse the Text
While it doesnât really have any real purpose, unless youâre trying to make a brain teaser for your co-workers or write in code, you can reverse the contents of the clipboard so that each line and word in a line appears backwards. To do this, copy something to your clipboard and type the following command:
pbpaste | rev | pbcopy
This command will use piping to reverse the contents of the clipboard and copy them back to the clipboard, ready for you to prank someone with reversed text.
4. Tidy Up HTML
If you write HTML, then you know how important it can be to ensure that each line of the text is perfectly formatted. The command will automatically fix the following errors (and more):
- Missing or mismatched end tags
- End tags in the wrong order
- Problem with heading emphasis
- Mixed up tags
- Missing â/â in end tags
- Missing end tags in lists
- Missing quotes around attribute values
- Tags lacking a terminating â>â
To run this command and have it automatically correct HTML code that is copied to the clipboard, use the following command:
pbpaste | tidy | pbcopy
This will run the clipboard contents through the tidy command using the piping technique, and will then copy the results to the clipboard, ready for you to paste into your HTML editor. Any errors found will be printed to the screen for you to see.
5. File Handling
So, you have something in the clipboard and you wish to place the text into a new text file? No problem. You can use another piping technique to do this automatically for you. Simply open the terminal and type in the following command:
pbpaste > file.txt
This command will take then contents of the clipboard and create a new file with the text called âfile.txt.â If you did not change directories in the terminal, then the file will be located in your userâs home directory.
What if the reverse is true? Letâs say you have a file whose contents needs to be copied to the clipboard. Well, you can use the pbcopy command to do that:
pbcopy < file.txt
Reversing the piping for âfile.txtâ and using the pbcopy command will cause the contents of file.txt to be copied to the clipboard, ready to be pasted into any application you choose.
Cory Bohon is a freelance technology writer, indie Mac and iOS developer, and amateur photographer. Follow this article's author, Cory Bohon on Twitter.
Comment