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
Best Answer
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
then do
then change your default shell path (in Terminal Preferences) to
and that's it. Check your bash version with
and as an added bonus, you can get mode indicators!
and it will show a
+
for Insert mode and:
for Command mode.