Image of the glider from the Game of Life by John Conway
Skip to content

Bash- The RIGHT Way

I guess when you have GNU developing both the bash shell and the text editor emacs, you're going to have one set of keyboard shortcuts work in the other. Case in point: the emacs keyboard shortcuts are also bound in the bash shell.

Well, being a vim enthusiast, that just isn't going to work. So, here's something that I learned at my Ubuntu-Utah LoCo meeting that ended just 20 minutes ago:

1
aaron@hercules:~$ set -o vi

Oh yeah. That's better. When executed, you are in insert mode, and not command mode. So, to take advantage of the vim keyboard shortcuts, 'esc' or 'ctrl-[' to get into command mode, and hack away! Make sure to add this to your ~/.bashrc.

You're welcome. :)

{ 13 } Comments

  1. Rick using Firefox 2.0.0.3 on Ubuntu | May 12, 2007 at 7:46 pm | Permalink

    This is amazingly cool. It might actually get me a bit better at using the vi methods of moving around. Thanks for the tip.

  2. Byron Clark using Firefox 2.0.0.3 on Ubuntu | May 12, 2007 at 8:36 pm | Permalink

    Even better, you can add 'set keymap vi-insert' to .inputrc and all of your readline using applications will have a vi keymap.

  3. Christer Edwards using Links on Debian GNU/Linux | May 12, 2007 at 10:07 pm | Permalink

    I was reluctant to switch to these because I have grown accustomed to some of the other emacs-based shortcut keys but after just a few minutes I got right back into it. Long live vim.

  4. Mathias using Firefox 2.0.0.3 on Ubuntu | May 13, 2007 at 4:23 am | Permalink

    The best bit is pressing v in command mode which starts a proper vi windows to edit the current command. This is SO good when doing a quick script.

    It would be nice if the prompt could reflect whether one is in command mode or insert mode though.

  5. Aaron using Firefox 2.0.0.3 on Ubuntu | May 13, 2007 at 7:05 am | Permalink

    Rick- No problem! :)

    Byron- Nice! Noted!

    Christer- You won't miss the emacs shortcuts. Except for maybe ^l.

    Mathias- I agree. That should be default, as it's part of the vi application.

  6. Frank using Konqueror 3.5 on Kubuntu | May 13, 2007 at 7:24 am | Permalink

    Be careful, though, since Bash's vi emulation is not 100% compatible with the vi emulation in Korn shell. For example, typing _ should insert the last word from the previous command line. Bash does this right in that it uses the last "word" including any quoted spaces, while Ksh does not -- but Bash does not insert a space before the inserted word like Ksh does, and I think the extra space makes more sense.
    Well, c'est la vie! (Did I spell that right?)

  7. Frank using Konqueror 3.5 on Kubuntu | May 13, 2007 at 7:25 am | Permalink

    Oops! That previous comment should have <ESC> before the "_" (underscore) character. The HTML parser ate it. :(

  8. Mathias using Firefox 2.0.0.3 on Ubuntu | May 13, 2007 at 8:17 am | Permalink

    If you miss the ^L:
    bind -m vi-insert "\C-l":clear-screen

    (found on http://www.jukie.net/~bart/blog/20040326082602)

  9. Zack Hobson using Firefox 2.0.0.3 on Mac OS | May 14, 2007 at 10:28 am | Permalink

    Actually, there's a better way. If you like vi keybindings then you'll probably want them for all your readline programs, and not just bash.

    Put this in your ~/.inputrc:

    set editing-mode vi

    Then EVERY readline program will use vi keybindings by default. I thought everyone did this, I've been doing it this way for years...

  10. Jason using Firefox 2.0.0.3 on Ubuntu | May 14, 2007 at 11:46 am | Permalink

    So... I want to test this out but not move permanently to it...

    How do I shut it off?

    man set (no entry)
    re-running set -o vi doesn't do it.
    set -h / --help don't provide any info.
    and there's no -O flag.

    None of the usual ideas for figuring it out worked.

    Help? :(

  11. Aaron using Firefox 2.0.0.3 on Ubuntu 64 bits | May 14, 2007 at 12:23 pm | Permalink

    Jason- As stated in the post, bash uses the emacs keybindings. So, just:

    1
    set -o emacs

    to get back to normal, or just 'exit' the terminal.

  12. phoenyx using Unknown on Unknown O.S. | May 14, 2007 at 4:36 pm | Permalink

    Are notes from that meeting going to be posted anywhere?

  13. kidd using BonEcho 2.0 on GNU/Linux | June 7, 2007 at 1:57 pm | Permalink

    IMHO zsh has a better support for vi keybindings, not to say it's waaay cooler than bash (globbing and autocompletion for example)

Post a Comment

Your email is never published nor shared.

Switch to our mobile site