Using VI keys to edit shell commands in UNIX

12,906

Solution 1

You're talking about the greatest feature ever!

You can use vi commands to edit shell commands (and command history) by adding this to your .bashrc file:

set -o vi

You can also run that command from the command line to affect only your current session.

If you don't use bash, substitue the appropriate rc file for your shell.


This allows you to use vi commands to edit any command...

You can also use j and k to move through your history (after pressing ESC).

You can also use / (after hitting ESC) to search for old commands.

In other words, to find that super-long cp command you did ten minutes ago:

ESC/cpENTER

Then you can cycle through all the matching commands in your history with n and N.

All this makes me 10 trillion times more productive at the command line!

Solution 2

If you're using bash, as jahroy and evil otto have already answered, you can use

set -o vi

to cause bash to use vi-style editing commands.

Once you've done this, you can type Esc v to launch the vi editor with a temporary file containing a copy of the current command line. You can edit the command, even replacing it with multiple lines; when you save the file (:wq), the shell will execute the edited commands.

If you prefer

set -o emacs

you can use Ctl-XCtl-E to do the same thing. It will use your preferred editor $EDITOR, not necessarily emacs.

(Personally, I use vi (actually vim) for most of my editing, but I prefer set -o emacs in the shell; switching in and out of insert mode is great for editing files, but awkward for interactive commands. YMMV.)

Solution 3

You can use set -o vi to change your line editing commands as @jahroy posted, but you may be thinking of the fc command (available in bash and I think ksh, but probably not tcsh), which will put the previous command into an editor (FCEDIT or EDITOR, which you probably have set to vi) and then executes the command when you exit the editor. See the manpage or help fc for details, or just try it out.

Solution 4

To edit most recently used command in vim and invoke an edited version after editor saves and exits, use fc shell built-in. Without any arguments it will do the following:

  1. Creates a temporary file in /tmp and populates it with the most recently typed command
  2. After editor exits normally (with exit code 0) it executes command found in that temporary file and removes this file. Edited version is saved in history as a usual command (at least in zsh). It executes nothing if editor exits with code other then zero (in vim this can be achieved by either killing it or using :cquit).

Command works both in bash and zsh.

Solution 5

Indeed, this is in fact the greatest feature ever. There's more though:

Create a file named

.editrc

containing the line

bind -v

Editline, is a BSD licensed version library that provides readline-like services. That .editrc will set your keybindings to vi mode in MySQL's command line shell, or any application that does not use readline.

I have grown somewhat forgetful with the passing of years, so my .zshrc sources some wrappers for vi mode, to show me what mode I am in on the command line. It's nice.

I know in zsh you can type

bindkey -L

and get the list of keybindings. I'm sure bash must have something similar. Not all of the vi keys are bound by default. Bind them how you see fit and you are off to the races.

Share:
12,906

Related videos on Youtube

user919860
Author by

user919860

Updated on September 18, 2022

Comments

  • user919860
    user919860 over 1 year

    I used to have a co-worker who was really good at UNIX.

    He showed me how to use Vi key bindings to edit my shell commands.

    He placed the command in a file that ran every time I logged in.

    Since then, I've moved to a different project.

    Unfortunately I don't remember how to set this up.

    Is there anyone here who knows how to use Vi key bindings to edit commands in the terminal?

    How can I make that setting permanent?

  • Admin
    Admin almost 12 years
    Well, it's not really using vi, it's using vi-style keyboard commands for line editing.
  • jahroy
    jahroy almost 12 years
    Sure... I guess the appropriate terminology is that vi bindings in the shell are the greatest feature ever invented!
  • Overmind Jiang
    Overmind Jiang almost 12 years
    alias r='fc -e -' is a valuable alias to rerun previous commands. fc on its own originated in Korn shell, I believe. You can specify commands by number (fc 123 129 to edit command numbers 123 to 129 in the history). You can specify relative numbers (fc -10 -1 to edit the last 10 commands entered). You can list previous commands with fc -l. You can specify a command by prefix with the r alias (r make runs the last invocation of make; so does r m if you've not run a mv command since you last ran make, etc.).
  • Keith Thompson
    Keith Thompson almost 12 years
    That sets your default editor (for commands that pay attention to $EDITOR) to vi. It doesn't let you edit shell commands with vi (at least not directly).
  • iconoclast
    iconoclast almost 12 years
    I would switch from emacs bindings to vi bindings if there were a way to display the mode. Do you know of a way to do that?
  • steveyang
    steveyang almost 12 years
    They, is there a way to bind jj with ESC as we usually do in vim ?
  • jahroy
    jahroy almost 12 years
    @yangchenyun - Maybe this question will help with that: stackoverflow.com/questions/844862/… I don't personally bind/alter any of the commands, so I'm not quite sure what you're asking.
  • steveyang
    steveyang almost 12 years
    @jahroy that helps:) Thanks. I want to use jj to behave the same as pressing ESC.
  • jahroy
    jahroy almost 12 years
    Wish I could upvote this 5 times for vi bindings in MySQL!
  • Enlico
    Enlico over 7 years
    This answer explains how to use tab to cycle through completions and arrows to move between commands starting with the already written text. I put that lines in my .vimrc file. The option set -o vi make this settings ineffective. How can I make them work? (In particular I would like to use j and k in place of down and up arrows.)
  • Keith Thompson
    Keith Thompson over 7 years
    @piertoni: Does set -o emacs not do what you want?
  • piertoni
    piertoni over 7 years
    Yes, It does work, I missed the point, thanks