6/22/09
12:55 am

MacMost Now 256: Using the Text Editors Hidden in Terminal

There are four text editors you can use from the Terminal: Pico, Nano, Vi and Emacs. Each can be used to quickly edit text files. Learn how to access them and what makes them different.

Video Transcript
Hi. This is Gary with MacMost Now. Today’s episode let’s take a look at some of the hidden text editors on your Mac.
So how many text editors or word processors do you think come in your Mac? Well, maybe one. Text Edit which is pretty capable. There’s also of course Pages in iWork if you have pay extra for iWork. But in fact, the answer would be six because there’s four hidden inside the terminal.
So to open terminal look in “applications” and then in “utilities” and you’ll find it there. Now you have a window that looks like this. It’s a command line. You have to type a “command”. Or, in this case, the name of a program you want to run.
Let’s type in Pico which is going to be the first text editor we’ll look at. Type it in and hit return and you drop into the Pico text editor. Here you can type text. You can type new lines. You can go ahead and you can use the arrow keys to move around. And you can also use commands which you see at the bottom of the screen. They’re control commands so you have to use the “control” key not the “command” key to run them. So control “w” will allow you to search. And likewise, control “o” will allow you to save the file out. Like that. Control “x” will allow you to exit.
Now an alternative to Pico is Nano. N-A-N-O. Go into that and you’ll see it looks exactly the same as Pico. That’s because it pretty much is exactly the same, just a slightly different license. Either one’s available on your machine and you can use either one.
Now let’s look at how you would open a file using either Pico or Nano. One way to do it would be to type the name of the program you want to run, like Nano, and type the name of the file you want to open. So we’ll type in the name of the file we used last time. myfile.txt. And there we go. We’re editing that file we created in Pico, now in Nano.
Now suppose the file you want to edit isn’t conveniently in the same directory where you’re located and you’re not really good at navigating around the terminal. Well, you can use the finder to help you open a file. So here’s a finder window and there’s a file called anothertest.txt.
I’m going to open that up in Nano. Type Nano. And then I’m going to drag the file here into terminal and it will insert a full path to it. So instead of having to type all that, I simply use the drag command. I hit return and now I’m editing that file and I can save it back out. So it’s easy to edit files you can find in the finder using either Pico or Nano.
Now here’s a completely different type of word processor. It’s one called VI. Type V-I and it will drop you into this VI editor which looks very different; got this little welcome screen. This is also a modal editor. So it’s got to be in “insert” mode for you to type in. Or it can be command mode for you to do commands, that type of thing. So for instance, if I type now nothing happens. However, if I go ahead and press “i”, I see the “insert word” appear at the bottom of the screen and now I can type into it. To escape from the insert mode I hit the “escape” key. Now I can type in different commands, things that I want to do. I can use the arrow keys to navigate around. Let’s say if I want to delete a character, I press the “x” key. Now if I want to insert, again, press “insert”. I can do like that. I have to escape to get out of insert mode. I can do things like “o” to insert a new line after the current one. It’s very different than a standard word processor but it’s something that programmers have been using for decades. And those that really get into it find that they can type and edit text documents extremely quickly using just keyboard commands.
So let’s look at the king of all terminal text editors. It’s something called Emacs and it’s also been around for decades. So we’re going to type in E-M-A-C-S and it’ll bring us into Emacs. As you can see by this help screen that first appears when you run it, there’s a ton of stuff in Emacs. It’s extremely complex. There’s a lot of different things you can do. But it’s also extremely powerful. So for instance, let’s go ahead and open a file. We’ll do control “x” and then control “f” and it prompts us for the file name. Myfile.txt. We can also create a new file this way. But here’s the file that we were currently using. We can go ahead and type things in here as well. Let me go ahead and page something in from the buffer and you can see there’s all this different text in there.
Now one of the cool things you can do with Emacs is you can split the view. So you can look at two different parts of the document at the same time. So to do that, we’re going to do control “x” and we’re going to hit “2”. Split the window in two, you can see there’s a top and a bottom. I can go ahead and scroll at the top here and you can see that the bottom part of the window doesn’t change. If I change something, you can see it appears in both places. So if you need to edit two parts of a document, you can be reading one part of the document in one window and editing it in the other. Well, you can do that with Emacs. Just one example of the power inside this program.
Okay. So Pico and Nano are pretty easy to learn. You can pretty much play around with them a little bit and get around and change files. But both VI and Emacs are extremely difficult to learn with tons of commands and lots of power. However, you can search on the web and finds lots of different tutorials because both of these programs have been around for decades. There’s tons of documentation. There are even books written about how to use them.
So here’s a quick fun look at some of the hidden inside your terminal. Till next time, this is Gary Rosenzweig with MacMost Now.

Comments: 7 Responses to “MacMost Now 256: Using the Text Editors Hidden in Terminal”

    Jean
    6/22/09 @ 3:37 pm

    Gary,
    I follow and like your ‘macmost-video’s’ for quite some time now. Still trying to figure out what your businessmodel looks like. Anyway … i was suprised about your video on text editors. Not so long ago I was a Windows user (never again) and just when I am more than happy with all the convenience the Mac offers you suggest we use terminal text editors which resemble my old DOS edlin commandline and early Wordstar programs. Why would you? Why would anyone want to use that when there are so many modern programs …?

    (English is not my native language so please forgive any mistakes)

    Jean (the Netherlands)

      6/22/09 @ 4:01 pm

      I don’t suggest you use them at all. There are many different types of Mac users looking to use many different things. I’ve been asked for more Terminal videos, so I came up with this one. Not all of the videos will be for everyone. It sounds like you, like most people, won’t really need this particular tip. But isn’t it interesting to know what is there?

    Jean
    6/22/09 @ 4:36 pm

    Thank you for your reply.
    OK you are correct you don’t literally suggest to use them ….
    Then again bringing this subject – editors with tons of information because they are around for decades – is still about quite obsolete programs ….. and why would anyone …
    OK OK, there might be some back-to-basics-people who would enjoy an ancient experience ….

    Anyhow, I’m honoured you’d take the time for a response. I’ll keep enjoying your video’s because in general they are very helpful for switchers like me. (I’m on OSX for 1,5 years now)

    Jean

    Clem
    6/23/09 @ 9:13 am

    Gary and Jean,

    I’d like to weight in.

    I liked and appreciate the “Using the Text Editors Hidden in Terminal” post. I rarely use emacs and the terminal window in general.

    But, when I do, I can do things that are difficult or impossible in MicroSoft office, to my knowledge.

    For example, in emacs, I can quickly construct a macro that’s difficult or impossible to express in TextEdit or Word. There’s also “regular expression” and stream capability to find/replace in emacs and elsewhere from the command line. (grep, sort, wc, sed with pipes makes it trivial to assemble scripts for complex, but one-time tasks.)

    There are other goodies buried in emacs on the Mac, by the way, including Eliza, Adventure, and other “games.”

    For programmers, emacs understands and color-codes syntax and keywords. I don’t believe that’s in TextEdit or Word.

    Here’s one more example. Last week, I needed a word occurrence count for a PDF document. I am not aware of that being elsewhere in the Mac. So, I Google-searched for a terminal command-line and found what became this:

    % pbpaste | perl ./jwords | sort -fd | uniq -c | sort -rfd | head -n 20

    (perl ./jwords acccomplishes a “deroff” task of converting a text stream into individual words on separate lines.)

    When run on the above text, the command line produces this list of word counts:
    8 in
    8 I
    8 and
    6 a
    5 the
    5 emacs
    4 to
    4 or
    2 thats
    2 that
    2 TextEdit

    So, Gary, keep those great posts coming!

    -Clem

    Wellington
    7/22/09 @ 11:53 am

    I am a new commer to Mac os x system, i have been a pc user for decades. Out of curiousity i bought a mac in other learn its amazing features. I thinkg i am liking it. Your tutorials have helped me a lot in navigating arround without much difficulties.

    Hi Gary, i have a question to ask. I am learning to play a guitar with aid of garrage Band. I donwloaded some learning lessons online, after a while some of the lessons no longer audoi sound. I have tried to re-download the same lessons, i am unable to. Can you please give me a suggestion ?

    Thanks.

      7/22/09 @ 12:36 pm

      Thanks.
      I’m not sure what the problem could be. Does other audio coming from GarageBand make any sound? In GarageBand preferences, under Audio/MIDI, is the Audio Output set OK? Check you system volume and also your speakers, of course, as well.

    Dave P
    9/24/12 @ 4:05 pm

    Most recent == best???
    OTC, I thought it was a Great video, Gary. It was just what I was looking for.

Comments Closed.