

Bash Vi Command Line Editing Mode - timmorgan
http://www.catonmat.net/blog/bash-vi-editing-mode-cheat-sheet/

======
glymor
It's readline that has the vi mode

    
    
      set editing-mode vi
    

in .inputrc will enable it everywhere.

~~~
james2vegas
luckily not everything on the command line uses readline, else there'd be way
more gpl licensed command line tools. vi mode has been in nsh and pdksh,
independent of gnu readline. It is also part of the specification for sh at
opengroup.org:

[http://www.opengroup.org/onlinepubs/000095399/utilities/sh.h...](http://www.opengroup.org/onlinepubs/000095399/utilities/sh.html)

Interesting note about emacs mode though:

In early proposals, the KornShell-derived emacs mode of command line editing
was included, even though the emacs editor itself was not. The community of
emacs proponents was adamant that the full emacs editor not be standardized
because they were concerned that an attempt to standardize this very powerful
environment would encourage vendors to ship strictly conforming versions
lacking the extensibility required by the community. The author of the
original emacs program also expressed his desire to omit the program.
Furthermore, there were a number of historical systems that did not include
emacs, or included it without supporting it, but there were very few that did
not include and support vi. The shell emacs command line editing mode was
finally omitted because it became apparent that the KornShell version and the
editor being distributed with the GNU system had diverged in some respects.
The author of emacs requested that the POSIX emacs mode either be deleted or
have a significant number of unspecified conditions. Although the KornShell
author agreed to consider changes to bring the shell into alignment, the
standard developers decided to defer specification at that time. At the time,
it was assumed that convergence on an acceptable definition would occur for a
subsequent draft, but that has not happened, and there appears to be no
impetus to do so. In any case, implementations are free to offer additional
command line editing modes based on the exact models of editors their users
are most comfortable with.

------
bittersweet
This works in ZSH as well, I also didn't know about it so this is definitely
going to save me a lot of keystrokes. Although I already had some of the
regular shortcuts memorized (like ctrl-a goes to start of the line) being able
to use one set of keybindings is great!

~~~
sundarurfriend
And in Zsh, you could even have it show the current Vi mode (Insert or
Command) in the prompt. I don't have my .zshrc handy, I'll try to get the
command once I get the .zshrc.

~~~
nuclear_eclipse
Wow, that's been my biggest trouble with vi mode for years, and I've been
using Zsh for a long time and didn't know it could do that. I would be
eternally grateful if you could post the solution here...

~~~
sundarurfriend
I'm at work after a long vacation, so got access to my .zshrc. Here's the
relevant part:

    
    
        #It starts in insert mode
        export VIMODE=INS
        function zle-keymap-select {
            VIMODE="${${KEYMAP/vicmd/CMD}/(main|viins)/INS}"
            zle reset-prompt
        }
    
        zle -N zle-keymap-select
    
        bindkey -v
    

I use Oh My Zsh, but as far as I know this should work without it too.

Wish HN had a notify-on-reply system like Reddit's orangered. Would you see
this? Would you not? Would I miss eternal gratefulness from a fellow HN
citizen? Oh, how I wish I knew the answer!

~~~
nuclear_eclipse
Maybe you won't even see this ;) , but it unfortunately doesn't seem to work
on my vanilla Zsh. Perhaps I'll look into OMZ a bit and see if things work out
that way. Thanks anyways :)

------
timmorgan
I've been using Ubuntu and bash for several years now, and never knew until
today that Bash had a Vi mode. I'm ashamed.

------
nuclear_eclipse
My biggest complaint about readline's vi mode is that there is no visual
indicator of what mode you are in, and it seems there are quite a few actions
that unexpectedly take you out of insert mode as compared to using Vim, and
without some way of seeing this, it's an exercise in frustration.

I've been using Vim as my primary editor/IDE for three years now, and I still
can't get used to vi mode on the command line, and keep going back to emacs
mode because it functions exactly like you would expect it to.

------
imurray
I use vim as my editor but never got on with "vi mode" in bash/readline or
zsh/zle. However, when you have a complicated command line you can use vim (or
your favorite $EDITOR) on it.

In bash type Ctrl-x-e

In zsh I press Ctrl-z, which does what I want because I have this in my
.zshrc:

    
    
      setopt hist_ignore_space # trick so that history doesn't get polluted
      function edithist() {
         local tmp=${TMPPREFIX}${$}hist
         read -Erz >| "$tmp"
         "$EDITOR" "$tmp"
         print -Rz - "$(<$tmp)"
         rm "$tmp"
      }
      bindkey -e '\M-q' push-input # replaces push-line in 3.0.x
      bindkey -e -s '\C-z' '\M-q edithist\n'
    

The zsh version leaves you editing the command line after exiting the editor.
The bash version executes the command after exiting the editor.

~~~
james2vegas
And if you don't have a problem with vi mode, use ESC to enter command mode
and enter v to edit the command line in ${EDITOR}

------
dylanz
I've known about Vi mode for a long time, but have never been able to make the
switch. It's a brain bender!

~~~
rg3
Something similar happened to me. I'm a happy Vim user and have been for
years, but I've always used bash in emacs mode.

However, some months ago I started to work in an environment full of Solaris 8
machines with ksh as their default shell in vi mode for some reason. It's
configured that way for every machine and we have to constantly log in and out
of them to do our jobs, and it's always been that way so I simply didn't ask
for it to change. After all I was also the new guy.

After a few weeks I got so tired of trying to use emacs commands in vi mode
shells while at work, and vi commands while in emacs mode at home that I
changed to vi mode at home. And I've been happy since then. It's not that
hard. You get used to it quickly, especially if you use vi or Vim a lot like I
do.

------
sophacles
One really really annoying thing about this: pressing return in "normal" or
"insert" modes executes the command. That behaviour just doesn't feel right.

~~~
sundarurfriend
Why? What would you expect it to do?

~~~
sophacles
Well, Enter in normal mode in vi puts the cursor on the next line, at the
first non-whitespace character. I would like enter in "normal mode bash" to do
the same. I would also like to scroll up (j) and see the previous command as i
left it (i.e. if modified, show them).

------
albemuth
Is there an analog for \e on the postgres console? That makes more sense to me
than using vi editing all the time

~~~
gredman
fc

