Dig Into Unix: vi
![]()
Continuing our Dig Into Unix series, we’ve now covered the absolute basics of launching Terminal.app, moving around the file system, looking at files with cat, and learning about commands with man. Now, I’d like to introduce you to the power of vi.
vi (pronounced vee-eye) is a text editor for the command line, and was originally written by Bill Joy in 1976. vi is now part of every single Unix system, including OS X. The version included with Leopard is vim, which stands for Vi iMproved. One of the major problems that vi has faced over the years is that it has a major learning curve. Every action that you take in vi you do with keyboard combos. Since there are so many actions that you can do, it takes awhile to get used to the different keystrokes, but once you do, they become ingrained in your fingers. From time to time, when typing in some other app, I find myself hitting the escape key followed by jjj or hhh trying to move the curser.
Getting Started
To get started, open up Terminal.app and type vi. This starts the editor in command mode. Type i to enter insert mode, and then you can begin typing text into the file. Copy the text from this article and paste it into vi, regular CMD-C / CMD-V will work just fine. This will give you something to work with.
Now that you’ve got some text, it’d probably be good to save it. Hit the escape key to put the editor back into command mode, and type :w newfile.txt. This will create a text file named newfile.txt in your home directory. Press i again, and you can begin typing, starting where you left off. Press escape again.
Navigating Lines
To move to any line of text in vi, you can press the number of the line you want to go to and G. For example, to go to the first line, type 1G. If you type that and the characters “1G” show up on your screen, you are not in command mode, don’t forget to press escape first. To get back to the end of the file, you can just press G by itself. That will bring you to the start of the last line in the file.
- To get to the end of a line, you can press
$, and your curser will jump to the end of the line. - To go to the beginning of the line, you press
^. Now you can jump around the file. - To move the curser one character at a time, use the four keys
h j kandl.hmoves one character to the left,jmoves one character to the right,kto moves one character up, andlmoves one character down. - To open a line below, you can press
o, and to open a line above,O.
Configuration File
These basic text editing commands form the basis of the real strengths of vi, and the reason I continue to use it every day. But to really take advantage of the power of vi, you need to create a configuration file for shortcuts.
Exit out of vi and write the file to disk at the same time by typing :wq! while in command mode. Now, create a new file, and open the file in vi for editing by typing vi .vimrc. The .vimrc file is the configuration file vi reads when starting. The important thing to remember about this configuration file is that you can add any of the commands above into the file, including movement, inserting text, and opening lines, and then map those commands to a keyboard combo. If you are familiar with TextExpander, you’ll follow right along with this concept, but it’s even stronger than that.
We are going to create a single line of text in the file that looks like this:
map ,a 1GiJack Doe^MGuy in Charge^MACME Inc.^M111 Main St.^MAnytown, USA
^M^M^M^MDear Sir,^M^M^M^MSincerely,^M^M^MJohn Doe^[10G i
The line above has been separated into two lines to fit on the page. When you see ^M, that’s not really two characters, it’s only one. It’s the return key, captured in raw form in the config file for use in generating our commands. To capture it, you press Control-V, followed by return. The esc key is also captured above by pressing Control-V and then hitting the escape key. That creates the ^[ character above, and allows us to enter some of the vi commands above into the config. Enter in that line of text above, using the Control-V trick to capture the return and esc characters.
Now, save the new file by typing :wq! in command mode, open up a new file with vi file, and test out your new ,a shortcut. If everything went well, it should look just like the video below.
Worth Learning
The learning curve with vi is steep, but as with most things worth learning, the payoff is significant. This short article has barely scratched the surface of what vi can do. vi can be a full-fledged IDE, or you could use it to write your next book. The mapping features above are especially helpful if you do any programming, or if you manage any Unix or Linux servers. If you’d like to learn more, I’d suggest starting with man vi, and maybe picking up a copy of the O’Reilly book “Learning the vi and Vim Editors.” Of course, I’d be happy to help out as well.
Back when I was first learning Unix, I asked an old graybeard why I should bother learning such a complicated, ancient text editor. He told me that since everything in Unix is a file, and almost all files in Unix are text, I needed some way to edit those files to control the system. Someday, he said, I would find myself without any other text editor, and without any other way to change a file. He was right, since I’m now working as a Unix systems administrator, I’ve been in that position for several years. Over time, I’ve come to appreciate all that vi can do for me.
Dig Into Unix: vi
![]()
Continuing our Dig Into Unix series, we’ve now covered the absolute basics of launching Terminal.app, moving around the file system, looking at files with cat, and learning about commands with man. Now, I’d like to introduce you to the power of vi.
vi (pronounced vee-eye) is a text editor for the command line, and was originally written by Bill Joy in 1976. vi is now part of every single Unix system, including OS X. The version included with Leopard is vim, which stands for Vi iMproved. One of the major problems that vi has faced over the years is that it has a major learning curve. Every action that you take in vi you do with keyboard combos. Since there are so many actions that you can do, it takes awhile to get used to the different keystrokes, but once you do, they become ingrained in your fingers. From time to time, when typing in some other app, I find myself hitting the escape key followed by jjj or hhh trying to move the curser.
Getting Started
To get started, open up Terminal.app and type vi. This starts the editor in command mode. Type i to enter insert mode, and then you can begin typing text into the file. Copy the text from this article and paste it into vi, regular CMD-C / CMD-V will work just fine. This will give you something to work with.
Now that you’ve got some text, it’d probably be good to save it. Hit the escape key to put the editor back into command mode, and type :w newfile.txt. This will create a text file named newfile.txt in your home directory. Press i again, and you can begin typing, starting where you left off. Press escape again.
Navigating Lines
To move to any line of text in vi, you can press the number of the line you want to go to and G. For example, to go to the first line, type 1G. If you type that and the characters “1G” show up on your screen, you are not in command mode, don’t forget to press escape first. To get back to the end of the file, you can just press G by itself. That will bring you to the start of the last line in the file.
- To get to the end of a line, you can press
$, and your curser will jump to the end of the line. - To go to the beginning of the line, you press
^. Now you can jump around the file. - To move the curser one character at a time, use the four keys
h j kandl.hmoves one character to the left,jmoves one character to the right,kto moves one character up, andlmoves one character down. - To open a line below, you can press
o, and to open a line above,O.
Configuration File
These basic text editing commands form the basis of the real strengths of vi, and the reason I continue to use it every day. But to really take advantage of the power of vi, you need to create a configuration file for shortcuts.
Exit out of vi and write the file to disk at the same time by typing :wq! while in command mode. Now, create a new file, and open the file in vi for editing by typing vi .vimrc. The .vimrc file is the configuration file vi reads when starting. The important thing to remember about this configuration file is that you can add any of the commands above into the file, including movement, inserting text, and opening lines, and then map those commands to a keyboard combo. If you are familiar with TextExpander, you’ll follow right along with this concept, but it’s even stronger than that.
We are going to create a single line of text in the file that looks like this:
map ,a 1GiJack Doe^MGuy in Charge^MACME Inc.^M111 Main St.^MAnytown, USA
^M^M^M^MDear Sir,^M^M^M^MSincerely,^M^M^MJohn Doe^[10G i
The line above has been separated into two lines to fit on the page. When you see ^M, that’s not really two characters, it’s only one. It’s the return key, captured in raw form in the config file for use in generating our commands. To capture it, you press Control-V, followed by return. The esc key is also captured above by pressing Control-V and then hitting the escape key. That creates the ^[ character above, and allows us to enter some of the vi commands above into the config. Enter in that line of text above, using the Control-V trick to capture the return and esc characters.
Now, save the new file by typing :wq! in command mode, open up a new file with vi file, and test out your new ,a shortcut. If everything went well, it should look just like the video below.
Worth Learning
The learning curve with vi is steep, but as with most things worth learning, the payoff is significant. This short article has barely scratched the surface of what vi can do. vi can be a full-fledged IDE, or you could use it to write your next book. The mapping features above are especially helpful if you do any programming, or if you manage any Unix or Linux servers. If you’d like to learn more, I’d suggest starting with man vi, and maybe picking up a copy of the O’Reilly book “Learning the vi and Vim Editors.” Of course, I’d be happy to help out as well.
Back when I was first learning Unix, I asked an old graybeard why I should bother learning such a complicated, ancient text editor. He told me that since everything in Unix is a file, and almost all files in Unix are text, I needed some way to edit those files to control the system. Someday, he said, I would find myself without any other text editor, and without any other way to change a file. He was right, since I’m now working as a Unix systems administrator, I’ve been in that position for several years. Over time, I’ve come to appreciate all that vi can do for me.
It’s the Little Things: Command Line Improvements to Mac OS X

Recently, two articles appeared on TechRadar documenting various command line tweaks for various apps and functions of Mac OS X. While I didn’t find anything new there, it’s nice to have two articles that summarize a bunch instead of tracking them down one by one across countless bookmarks.
To be honest, most command line tweaks don’t appeal to me. For example, I’d sooner go back to running DOS than go back to using Safari’s “standard” tabs; I love the tabs on top. Still, it’s nice to know I could make the change if I felt so inclined.
What’s surprising to me is that some of the tweaks I do like are incredibly simple — for example, the command that turns on stack highlighting even when using the mouse (No. eight in the first article above). Obviously, having the cursor over an item tells me which one will be activated if I click, and yet I like the highlight as a further indicator.

I also like the one to remove the arrows from iTunes’ interface (No. seven in the second article). Again, this is a little thing, yet I value it in a manner that’s completely out of proportion to the actual change it makes. Put simply, I hate those arrows.
Meanwhile, there’s a tweak for Safari I really like that’s not in either article. It’s one that forces a click to open in a new tab (instead of a window). You can find that one here. Though documented for Safari 3.1, I use it for the Safari 4 beta with no issues.
I’m curious what you think. Which tweaks do you really like? Do you have any to add, and do they seem like major or minor changes to you?
It’s The Little Things: Command Line Improvements to Mac OS X

Recently, two articles appeared on TechRadar documenting various command line tweaks for various apps and functions of Mac OS X. While I didn’t find anything new there, it’s nice to have two articles that summarize a bunch instead of tracking them down one by one across countless bookmarks.
To be honest, most command line tweaks don’t appeal to me. For example, I’d sooner go back to running DOS than go back to using Safari’s “standard” tabs; I love the tabs on top. Still, it’s nice to know I could make the change if I felt so inclined.
What’s surprising to me is that some of the tweaks I do like are incredibly simple. For example, the command that turns on stack highlighting even when using the mouse (number eight in the first article above). Obviously, having the cursor over an item tells me which one will be activated if I click, and yet I like the highlight as a further indicator.

I also like the one to remove the arrows from iTunes’ interface (number seven in the second article). Again, this is a little thing, yet I value it in a manner that’s completely out of proportion to the actual change it makes. Put simply, I hate those arrows.
Meanwhile, there’s a tweak for Safari I really like that’s not in either article. It’s one that forces a click to open in a new tab (instead of a window). You can find that one here. Though documented for Safari 3.1, I use it for the Safari 4 beta with no issues.
I’m curious what you think. Which tweaks do you really like, do you have any to add, and do they seem like major or minor changes to you?
TUAW Tip: Moving your home folder to another disk (or moving it back)
Filed under: Terminal Tips, TUAW Tips
In ye olde times, with “Mack OSe 9,” many users chose to keep their personal files, work, and documents on a different physical disk from their startup disk. It was a safety measure: If one disk goes down, at least the other won’t. There was no structural reason to keep files in a particular disk location, other than keeping them out of the System Folder.
I visited a client yesterday whose drive scheme was set up exactly like this, and he wanted to be (finally) upgraded to Leopard. I wasn’t sure how Leopard would handle the fact that his Users folder had been moved to a different drive, so (knowing I had backups of his entire system) I cautiously proceeded with the installation.
After the installer finished, Leopard had created a fresh, blank Users folder on the startup disk with a home folder bearing the same username. This wasn’t exactly the answer I was looking for. I had to link, somehow, the new Users/hisname folder with his existing user folder on the other volume.
Turns out, Leopard handles this much better than previous versions of Mac OS X. Read on to find out how.
Continue reading TUAW Tip: Moving your home folder to another disk (or moving it back)
TUAWTUAW Tip: Moving your home folder to another disk (or moving it back) originally appeared on The Unofficial Apple Weblog (TUAW) on Thu, 14 May 2009 15:30:00 EST. Please see our terms for use of feeds.
Read | Permalink | Email this | Comments
Ask TUAW: tracking your iTunes purchases, quitting processes, doing a clean OSX install and more
Filed under: Software, iTunes, Ask TUAW, Leopard
Once again, it’s time for another edition of Ask TUAW: the place where we try to answer all of your Mac and Apple-related questions. This week we’re taking questions about tracking your total iTunes purchases, forcing processes to quit, doing a clean install of OSX and more.
As always, we welcome your suggestions for this week and questions for next time. Please leave your contributions in the comments for this post. When asking questions, please include which Mac and which version of OS X you’re running. If you don’t specify, we’ll assume you’re running Leopard on an Intel Mac.
Dima asks:
I would like a way to require a password when the computer wakes from sleep, but not to require a password for just the screensaver. Is there a way for me to accomplish this?
The only way I know of to accomplish this would be to modify the plist file for the screensaver via the Terminal. However, unless this is really important to you, I would not advise doing it. If you want to do it, proceed at your own risk.
Either way, here’s the command to enter in the Terminal if you want to try it.
defaults -currentHost write com.apple.screensaver askForPassword -int 0
This will set it so the computer only asks for a password after waking from sleep and not after the screensaver.
Frank asks:
In my finder window, next to my drive’s name, there’s a number in parentheses. it used to be a (2), but recently it changed to a (3). What does this number mean?
TUAWAsk TUAW: tracking your iTunes purchases, quitting processes, doing a clean OSX install and more originally appeared on The Unofficial Apple Weblog (TUAW) on Wed, 22 Apr 2009 23:00:00 EST. Please see our terms for use of feeds.
Read | Permalink | Email this | Comments
TUAW Tip: Stop Backup.app from bouncing
Filed under: Software, How-tos, Tips and tricks, TUAW Tips
TJ Luoma recently shared the solution to an annoying problem. Specifically, he wanted to keep Backup’s icon from frantically jumping in the Dock like so many hepped-up toddlers in a bouncy castle. The Backup icon typically bounces in the Dock for a minute two before it even begins backing up any data.
We know what you’re thinking, “Just go to the application’s preferences and disable the Dock animation.” The problem is that Backup has no preference pane! Fortunately, TJ found a fix.
While browsing “defaults read com.apple.backup” in Terminal (as suggested by a Twitter helper), he found
“Backup Timer” = 120
Realizing that’s how long the app is supposed to wait (and toss its icon up and down) before executing a backup, he entered
defaults write com.apple.backup “Backup Timer” 1
which forced it to bounce only once. Alternatively, you could install Dockless, which prevents running apps from showing up in the Dock (or vice versa). Check out TJ’s post and enjoy!
TUAWTUAW Tip: Stop Backup.app from bouncing originally appeared on The Unofficial Apple Weblog (TUAW) on Thu, 19 Feb 2009 14:00:00 EST. Please see our terms for use of feeds.
Read | Permalink | Email this | Comments
Terminal Tip: Enable half-star ratings in iTunes
Filed under: Terminal Tips
Do you like giving ratings to songs in iTunes? If so, then you’ve probably noticed that you are only able to rate songs on a full-star basis, not enough granularity for some music fans… there’s a longstanding AppleScript hack to enable half-stars, but now there’s an easier way around this issue. Macworld’s Rob Griffiths found a work around, involving a simple Terminal tip to enable half-star ratings.
To enable half-star ratings, close iTunes, and open Terminal (/Applications/Utilities). Once you have Terminal opened, type the following command and press enter:
defaults write com.apple.iTunes allow-half-stars -bool TRUE
When you reopen iTunes and rate a song, you will be able to give half-stars. That simple. If you wish to make things normal again, open Terminal and type the same command, replacing “TRUE” with “FALSE.”
TUAWTerminal Tip: Enable half-star ratings in iTunes originally appeared on The Unofficial Apple Weblog (TUAW) on Wed, 31 Dec 2008 11:00:00 EST. Please see our terms for use of feeds.
Read | Permalink | Email this | Comments
Terminal Tip: Enable Safari web inspector
Filed under: Terminal Tips

Perhaps you are a web developer, or maybe you’re just nosy. Either way, looking at certain websites’ CSS, HTML, and Script documents can be fun. If you are a Safari user, you can easily look at the loading documents in the Activity View (Window > Activity), but what if you want to take this a step further? You can with the Safari Web Inspector, and you can do it by enabling the Safari “Develop” menu.
To enable the Safari Develop menu, just close Safari and type (or copy/paste) the following command into Terminal.app (/Applications/Utilities):
defaults write com.apple.Safari WebKitDeveloperExtras -bool true
Once you enter the command, you will be able to launch Safari, load a webpage, and click Develop > “Show Web Inspector” to see the page attributes. You will be able to see the documents, stylesheets, images, and scripts.
Want more tips and tricks like this? Visit TUAW’s Terminal Tips section today!
TUAWTerminal Tip: Enable Safari web inspector originally appeared on The Unofficial Apple Weblog (TUAW) on Thu, 11 Dec 2008 13:00:00 EST. Please see our terms for use of feeds.
Read | Permalink | Email this | Comments
Terminal Tip: Change Time Machine backup interval
Filed under: Terminal Tips
Sometimes you want your Mac to be backed up more frequently than usual. If you want to instantly back up using Time Machine, you could click on the menu bar item and select “Back Up Now,” but what if you want to change the backup interval indefinitely? With this Terminal Tip, you can do just that.
Time Machine is set to automatically back up every hour, but if you would like to change it to every half hour, you can use the following Terminal (/Applications/Utilities) command:
sudo defaults write /System/Library/LaunchDaemons/com.apple.backupd-auto StartInterval -int 1800
You will need to authenticate as an administrator, since this command is run under a “sudo.” The time interval is measured in seconds, so you can enter any time you wish there; just make sure it is in seconds. By default, Time Machine backs up every 3600 seconds (every hour). If you wish to revert to the original, just replace “1800″ with “3600.”
Want more tips and tricks like this? Visit TUAW’s Terminal Tips section!
TUAWTerminal Tip: Change Time Machine backup interval originally appeared on The Unofficial Apple Weblog (TUAW) on Mon, 08 Dec 2008 09:30:00 EST. Please see our terms for use of feeds.
Read | Permalink | Email this | Comments




