Industry guru Dave Taylor offers tech support on technical and business topics, including iPhone, iPod, Microsoft Windows, Sony PSP, cellphones, online advertising, CSS, Web design, business, Unix, Linux, SEO, Mac OS X, and shell script programming.     


How do I get "vi" shell edit mode in Mac OS X Terminal?

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?


Dave's Answer:

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:

ps -p $$

When I run that, for example, I see this:

$ ps -p $$
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:

keymap (emacs) 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:

set keymap vi

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:

man bash | col -b > ~/bash.man ; vi ~/bash.man ; rm ~/bash.man

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. :-)


More Useful Mac OS X Help Articles:
✔   Audacity can't find LAME library, I can't save Mp3?
Hey Dave. I read your article Audacity can't save mp3 audio files and am still puzzled because I downloaded the LAME Mp3 converter...
✔   How to remove Dashboard as a "space" in Mac OS X Spaces?
I'm a big fan of the Spaces utility in Mac OS X that lets me have multiple virtual screens [see Set Up Mac...
✔   Best place to buy a cheap MacBook laptop?
Hi Dave. I am looking for two gently used MacBook laptops for my teen daughters. Personal computers would greatly facilitate their studies as...
✔   File too big error copying to USB flash drive on my Mac?
I'm baffled. I have a 16GB Kingston USB flash drive that I use on my Mac system and I'm trying to copy a...
✔   Stealth image capture photo from webcam on my Mac?
Someone sneaks into my cubicle while I'm at lunch and takes candy out of my desk. Petty, but stupid too. I want to...

Let's stay in touch!
Sign up for my weekly AskDaveTaylor Newsletter and you'll receive even more tech and gadget help right to your inbox, along with exclusive news and industry updates. It's good stuff. I promise!
    Enter your name: and your email addr:  





Categorized: Mac OS X Help , Unix and Linux Help   (Article 6980, Written by )
Tagged: bash, linux, mac os x, terminal, unix, vi
Previous: What do all the flashing lights mean on my new Apple iPod Shuffle?
Next: How do I add Windows Live Search to my Blog?




Reader Comments To Date: 3

Adam Michel said, on November 17, 2006 3:50 PM:

Your last tidbit about man pages seems to boil down to using vim (which we have on OSX in lieu of vi) as a man page viewer. It's much easier than that.

In bash:
export PAGER="col -b | vim -R -c 'set ft=man nomod nolist' -"
In tcsh:
setenv PAGER="col -b | vim -R -c 'set ft=man nomod nolist' -"

Now, just type "man bash" or whatever you want the man page for. The man command will then use whatever is in $PAGER to show you the money. Syntax-highlighted (in a good terminal) vim command friendly manpage viewer. Some people have reported weirdness with vim as their PAGER, I've never had the slightest issue. The highlights are so stinkin' handy too. If you're not seeing colored highlighting and you're using a decent terminal (xterm, dtterm, etc), make a .vimrc in your home folder ($ vi ~/.vimrc) and add this:
syntax on

You should now have color. You know, if the planets aligned properly.

Kaolin Fire said, on August 1, 2008 3:24 AM:

Thank you so much for this! I encountered that setup a long, long time ago, before I was familiar with vi. I've (long) since become a vi/vim zealot and have dreamed of finding how to do get it back.

And hey, syntax-highlighted vim-embedded man pages are teh awesome, also. :)

Dave said, on July 11, 2012 7:22 AM:

One step to get vi for command line editing:

shopt -s -o vi

Starbucks coffee cup I do have a lot to say, and questions of my own for that matter, but first I'd like to say thank you, Dave, for all your helpful information by buying you a cup of coffee!

I do have a comment, now that you mention it!











I will never send you any unsolicited email. Ever.






Check This Out Too...

 
Look for Answers
Need Help? Ask Dave Taylor!


Follow Me on Pinterest

Find Me on Google+
ADT on G+
© 2002 - 2013 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site. Further, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site. My lawyer says "Thanks".
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.