Skip to content

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

Tags:

4 thoughts on “Edit long Terminal commands in a visual editor”

  1. Or you could just use the emacs editing commands that bash defaults to.
    Cheers, Liam

  2. Yea, I should've stated "cursor movement keys" more clearly - I meant Emacs keys, i.e. Esc B, Ctrl F, etc.

    But as I use vi for most of my terminal file edits (if I don't open in BBEdit), I never remember all the Emacs keys :).

    -rob.

    1. If you run set -o without any options, it'll display everything you can set. It looks like editors are limited to emacs and vi.

      -rob.

Comments are closed.