Skip to content

vi

Edit long Terminal commands in a visual editor

Here's a quickie tip for those of us who occasionally string together complex commands at Terminal's prompt: You may want to add this simple line to your .profile (or whatever init file you use):

set -o vi

What does it do? It tells Unix/Terminal to set the input line editor to vi. When might this be useful? Let's say you've typed a long command, like the one to launch a background screen saver:

/System/Library/Frameworks/ScreenSaver.framework/Resources/ScreenSaverEngine.app/Contents/MacOS/ScreenSaverEngine -module "Arabesque" -background &

Before you hit Return, you notice a couple of typos early in the command. You could use cursor movement keys to move around, of course, but with the above command in place, just press Escape and hit v: The entered command will open for editing in vi. Make your changes, then do the usual :wq vi exit dance, and your edited command will then execute.

Note that if you edit a command but then don't save it (i.e. you press :q!, you may have to hit Return on the command line to get out of an odd "waiting for v to edit" mode. (At least that's the only way I found to return to normal typing.)