Sep 26 2011

Unix text editors: the layered approach

Category: Uncategorizedzvolkov @ 9:49 pm

When it comes to learning things, I’m a big fan of layered approach. By this I mean a methodology of study that emphasizes the history of evolution of whatever it is we are learning. Recently I’ve been applying it to Unix text editors. This post is a document of my research into this topic.

As you may or may not know, in Unix operating systems there is a line of text editors that lead to creation of famous vim. The full line looks as follows:

  • ed — “the standard text EDitor” written by Ken Thompson for the first Unix back in 1971
  • em — “the ed for Mortals” — a mod of ed featuring one-line visual editing, developed by George Coulouris in 1976
  • ex — an “eXtended” version of em written by Bill Joy at Berkley University in 1977, featuring the full-screen visual editing mode
  • vi — a hard link to ex 2.0 that starts the editor with the visual mode turned ON by default
  • vim — a “vi iMitation” developed in 1988-91 for Amiga computers. Supports syntax highlighting, various plugins, mouse, unlimited undo, word-completion etc.

You can find more vi history in wikipedia.

What I want to look at today is the great-great-grandfather of vim: the venerable ed. Using ed is like writing with ink using a goose pen. Its Zen character hides itself under the surface of false simplicity. It requires concentration and focus. It teaches you the origins of the commands you will see in vim. Try using ed for a day to edit all your text files.

If you’re running Mac or any flavor of Unix, you already have it. If you are on Windows, you can get it from here.

Open it on command line like this: “ed 1.txt”.

At first you can be shocked: No only you don’t see scrollbars, there is no fucking text visible anywhere on screen! That’s because ed is a command-centric editor. You issue commands to move in the files and to edit text, one line at a time. Here, to make your experience less shocking I’ve organized ed commands into 8 layers, each building up on the previous. Once you have tried them all, you will not only understand vim better, but also get the taste of what computers felt like back in 1972.

Layer 1. Moving around

  • h — help explain why ed said “?”
  • q — quit
  • + — go to next line
  • - — go to prev line
  • +10 — go 10 lines forward
  • -10 — go 10 lines backward
  • 123 — go to line 123
  • $ — go to last line
  • [Enter] — move through text printing one line at a time
  • z — move through text printing one screen at a time

Layer 2. Editing text

  • a — append text after current line
  • . — exit the text mode and go back to command mode
  • p — print the current line
  • d — delete current line
  • c — change the text of the current line
  • i — insert text before current line
  • Q — quit even if there are unsaved changes
  • w — write the file back to disk

Layer 3. Search and replace

  • ?abc —  go to the previous line containing “abc”
  • /abc — same as ? but searches down, and wraps over to the top of the file
  • s/abc/xyz — if current line contains “abc”, substitutes it for “xyz”
  • s — substitutes the next match on the same line
  • s/abc/xyz/3 — substitutes third match on the current line, instead of the first one
  • s/abc/xyz/g — substitutes all matches on the current line

 Layer 4. Line addressing

  • 123a — append text after line 123
  • 123i — insert text before line 123
  • 10,20d — delete lines from 10th to 20th
  • 10,20c — delete lines from 10th to 20th and enter the text that will go in their place
  • 10,20p — print lines from 10th to 20th
  • 10,20n — print lines from 10th to 20th with their line numbers
  • %p — print all lines
  • 10,20s/abc/xyz/g — substitute all occurrences of “abc” with “xyz” in lines 10 through 20
  • %s/abc/xyz/g — substitute all occurrences of “abc” with “xyz” in the entire file

Layer 5. Regular expressions

The substitute command “s/abc/xyz” supports regular expressions in its “abc” part:

  • . means any character. To search for dot, use \.
  • .* means any sequence of any characters
  • ^ means beginning of line
  • $ means end of line

Also, it supports special characters in the “xyz” part:

  • & means the actual substring matched by the “abc”
  • \[Enter] means a new line

Layer 6. Advanced editing

  • j — join the next line to the end of the current line
  • m123 — move current line in front of line 123
  • t123 — copy current line in front of line 123

(the above commands also work with multiple lines)

  • u — undo the last command. doing the  “u” again redoes the undone command.
  • 123kx — mark line 123 with label “x”
  • ‘x= — print line number of line marked with label “x”
  • ‘x — go to line marked with label “x”

Layer 7. Interaction with external world

  • !dir — execute shell command “dir” without exiting to shell
  • r !dir — execute shell command “dir” and append its output to the end of the current text
  • 10,20w 2.txt — write lines 10 through 20 to file 2.txt
  • 10,20w !cmd — send lines 10 through 20 to standard input of shell command cmd
  • f 2.txt — changes current file name to 2.txt

 Layer 8. Interactive search

  • G/abc — for each line matching “abc”, makes it current and allows user to enter a command


Sep 23 2011

Building my own PC

Category: Uncategorizedzvolkov @ 11:29 pm

Lately I felt slightly bored, and so I decided to entertain myself by assembling a custom “rig”. I took Jeff Atwood’s latest system as a starting point, and made a few tweaks.

Here’s what I’ve ordered so far:

CPU — $315

I knew that Intel Sandybridge is the latest and greatest processor technology at the moment. I went to the wikipedia article to research the models and arrived at the same Core i7 2600K model which Jeff has used in his build. This basically is the fastest Sandybridge currently available (the 2700K is not available yet as I’m writing this). I did not even consider AMD, not sure why.

Motherboard — $200

Next I went to wikipedia’s article for LGA 1155, which is socket for the CPU I’ve chosen. I’ve also looked at this table of chipsets. From these two I figured I want a P67 or Z68 chipset. From there I went to ASUS web-site (I knew I wanted an ASUS or Gigabyte motherboard), and looked at my options. Both P67 and Z68 had some very good boards but Z68 model line had more choices and looked more “enthusiast-oriented”. Finally I’ve settled on Asus P8Z68-V for $180. It is a full-size ATX board, with the full selection of slots, and all the modern technologies built-in, like USB 3.0, UEFI BIOS and 6Gb/s SATA. It also had standard stuff like the Audio card, the NIC etc. But when I saw that Jeff’s rig uses the Pro version of the same board I decided to upgrade… it only costed me $20 more. The Pro version has 2 extra SATA 6Gb/s ports, two 1394 ports, and an extra USB 3.0 back panel plate. I should have probably stayed with the non-Pro version since I won’t use any of these extra features.

Memory — $118

After reading this wonderful comparison of memories, I’ve decided there’s no point in wasting money on fancy memory, and went ahead with Corsair Vengeance 16 GB kit, the same one Jeff had in his rig. Being 1.5V this particular memory uses less power (=emits less heat), and runs almost as fast as G.Skill Ripjaws-X which costs twice as much.

CPU heatsink/cooler — $75

I wanted a cooler that would have a PWM fan (aka 4-pin fan), because my motherboard can control the speed of CPU fan based on CPU temperature. After some hesitation I’ve settled on Thermalright Archon, one of the best air-based (as opposed to liquid-based) coolers available. It is a little tall at 170mm, and I hope it will fit in my case.

Power Supply Unit — $127

I’ve noticed Jeff’s Seasonic X-760 was too powerful for his needs (he was only using 128W-332W of the available 760W), and since my plans were even less ambitious, I’ve decided to go with Seasonic X-560, a 560W version. Judging by the reviews I’ve seen, Seasonic X series are one of the best PSUs out there in terms of noise and power consumption.

Case — $173

First I’ve ordered NZXT Tempest 410 Elite – a nice-looking $100 case. In the specs they said it provided 170mm of space for the CPU heatsink. It also had some unique features like detachable front-fans and proper USB 3.0 header. But after reading some very nasty reviews I changed my mind and went with Corsair Obsidian 650D — a much more professional case with even more space inside, better cable management and overall higher build quality. I’m still on the edge as to which I should keep — must decide in the next few days, before they arrive, so I can refuse shipment on one of them and get my money back. I will most likely keep the Corsair, don’t want to open the NZXT and realize my CPU cooler does not fit by 1 millimeter.

Videocard — $0

I will not buy an external video-card until next year, the Intel HD 3000 graphic card being built right into the CPU. Out of all the cards available now I’m looking at NVidia 560. My main concern (besides price) is the power consumption, since my Power Supply is only 560 watt I can’t get a super-hungry card. This wikipedia article offers nice comparison of NVidia cards, which includes their TDP. According to my power calculations I need to get a card that would eat no more than 160 watt at its peak.

Total — $1008

This is a little more than $1000 spent so far. This is all I need for now to build my own PC. For harddrive, I’m planning to reuse an old IDE HDD (will buy an IDE-to-SATA adapter for ten bucks) coupled with a 16GB USB drive I have somewhere. For the monitor, I will use my Sony 52-inch LCD TV. The only other thing I will need to get is a keyboard.

Since I already have 5 computers at home (Matthew’s gaming desktop, 3 laptops, an 24-inch iMac — I’m not counting a Playstation 3, a Wii, and 3 iPhones) I’m not sure what I’m gonna do with this one once I’ done building it. Maybe I will sell it on Ebay… Or install Google Chrome OS and run a superfast internet browser on a 52-inch screen… Any ideas?