2

In all that follows, the ^ indicates the position of the cursor.

I'm using bash on Terminal on OS X Yosemite and I'd like to use vi-style line editing:

prompt$ set -o vi
                 ^

By default it starts in vi Insert mode, so I can immediately type

prompt$ hello
             ^

Now let's say I use the left arrow key or H to move left:

prompt$ hello
            ^

Now I can no longer move to the right of the o in hello using either or L:

prompt$ hello  # I can't move here anymore!
             ^

If I am to append something, I have to hit Esc then ShiftA.

This does not happen in emacs mode (set -o emacs) or on certain Linux machines (ssh'd into SLC 6.6 with bash 4.1, or natively on my friend's (I think Arch) with bash 4.3). In all of these, then takes me to the end of the line where I can keep typing; on my Mac terminal I cannot unless I leave Insert mode.

This also does not happen with vim on my Mac; vim lets me move past end of lines with arrow keys in Insert mode. It's just readline vi mode.

I tried updating my bash (with Homebrew) from 3.2 to 4.3; that did not fix it.

I tried removing my .bash_profile and using an unmodified Terminal; that did not fix it. (I have some aliases and prompt modifiers in my .bash_profile.)

I have read this, this, and this. They don't have answers.

Thanks in advance for any advice!

Edit: On a Linux system where things work as I would like it to, the TERM variable gives

prompt$ echo $TERM
xterm-256color

The contents of /etc/inputrc are

# do not bell on tab-completion
#set bell-style none

set meta-flag on
set input-meta on
set convert-meta off
set output-meta on

# Completed names which are symbolic links to
# directories have a slash appended.
set mark-symlinked-directories on

$if mode=emacs

# for linux console and RH/Debian xterm
"\e[1~": beginning-of-line
"\e[4~": end-of-line
# commented out keymappings for pgup/pgdown to reach begin/end of history
#"\e[5~": beginning-of-history
#"\e[6~": end-of-history
"\e[5~": history-search-backward
"\e[6~": history-search-forward
"\e[3~": delete-char
"\e[2~": quoted-insert
"\e[5C": forward-word
"\e[5D": backward-word
"\e[1;5C": forward-word
"\e[1;5D": backward-word

# for rxvt
"\e[8~": end-of-line
"\eOc": forward-word
"\eOd": backward-word

# for non RH/Debian xterm, can't hurt for RH/DEbian xterm
"\eOH": beginning-of-line
"\eOF": end-of-line

# for freebsd console
"\e[H": beginning-of-line
"\e[F": end-of-line
  • Can you post the value of the TERM environment variable as well as the contents of /etc/inputrc and ~/.inputrc (if it exists) from one of the systems where this behaviour works as you want it to (just the non-commented lines as the file is possibly quite large)? – mjturner Aug 11 '15 at 9:52
  • @mjturner I edited my post. I don't have a .inputrc in my home directory. I posted the contents of /etc/inputrc, it was pretty short, but I don't know if this particular /etc/inputrc is the one you want, since the Linux machine on which I work serves many users. – Riju Dasgupta Aug 11 '15 at 11:25
  • There is an interesting discussion of exactly the same issue on the bug-bash mailing list, albeit on a DEC Alpha. If you read the full thread, Chet Ramey suggests that a termcap entry (terminfo on a Mac) could be sending an Esc, causing problems with vi mode. – mjturner Aug 11 '15 at 11:52
  • Okay, it actually just randomly started working. I guess my bash update took some time to "take". So I guess the solution is to update bash from 3.2 to 4.3 on Macs. @mjturner Thanks for your help! – Riju Dasgupta Aug 11 '15 at 12:04
1

It turns out the solution is simple: update bash to 4.3. The default bash on Macs (even Yosemite) is 3.2. (My question above says I did update and it didn't work, but I think it took a while for it to "take.")

Install Homebrew with

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

then do

brew install bash

then change your default shell path (in Terminal Preferences) to

/usr/local/bin/bash

and that's it. Check your bash version with

bash --version

and as an added bonus, you can get mode indicators!

bind 'set show-mode-in-prompt on'

and it will show a + for Insert mode and : for Command mode.

  • Ideally, you should also add your new version of bash to /etc/shells and then change your shell using chsh (or System Preferences > Accounts). – mjturner Aug 11 '15 at 12:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .