I just finished Learning Unix for Mac OS X Tiger, and think it is an excellent little resource. I’m recently out of school, a school where we weren’t taught anything about Unix, just enough to get by. I started work with the data converter team at Agilent labs, and have been able to glean quite a bit off of them (considering many of them were either directly or indirectly responsible for writing many of them commands in common use today). Thus, I’m often embarrassed to ask them questions about exactly some of the stuff you cover in the book.
Okay, now for the question: at work, they have the terminal set up such that the command line works like vi. That is, if I want to search my command history, I simply change mode via escape, type /pattern_to_be_searched, it finds the command, and then I can use other vi commands to change the command a little bit, and then execute it via the shell. How do I set this up in OS X?
Thanks for your note! I like getting these sort of Unix and Linux questions, actually, since Iv’e been working in that environment for twenty-six years now! Sheesh.
The first step is to ensure that you’re running the Bash shell, which you can do by typing this in:
When I run that, for example, I see this:
PID TT STAT TIME COMMAND
958 p1 S 0:00.03 -bash
I’m running Bash. Good. (the ‘-‘ means it’s a login shell, by the way).
What you want is “vi mode” in the Readline function. Readline is the module in Bash that processes input typing and command entry. Check the man page for Bash (which is incredibly long) and you’ll find that you’re talking about the readline keymapping, which is described thusly:
Set the current readline keymap. The set of valid keymap names
is emacs, emacs-standard, emacs-meta, emacs-ctlx, vi, vi-com-
mand, and vi-insert. vi is equivalent to vi-command; emacs is
equivalent to emacs-standard. The default value is emacs; the
value of editing-mode also affects the default keymap.
That’s what you want. To set it once you’ve logged in, simply type:
To set it permanently for all future shells, add that line to your .bashrc in your home directory. That’s all there is to it!
Update: In some situations that doesn’t seem to work. My theory is that it’s to do with it not overriding shell environment variables (esp. SHELLOPTS). If that doesn’t work for you, try this command instead: set -o vi and I bet you’ll have some joy.
One more helpful tip: if you want to dig around in a Unix man page, here’s a very helpful command to remember:
This gives you an version of the specified man page that you can explore and read at your leisure that’s conveniently deleted once you’re done reading it. I find it far more pleasant than just scrolling through with the “man” command.
And say “Hi” to those Unix old-timers. We probably know each other. 🙂