Bash in vi-mode: browsing shell history with cursor up/down, cursor position not at the end?

StackOverflow https://stackoverflow.com/questions/844862

  •  20-08-2019
  •  | 
  •  

Question

I'm very fond of vim and really would like to enhance my bash experience with vi-mode. However one thing I'm so accustomed to the emacs behavior in that, when browsing the shell history with cursor up/down, the cursor is positioned at the end of the line.

In vi-mode it's the other way around; the cursor is always positioned at the beginning of the line. It's really burned in the brain to expect the cursor at the end, having used bash in emacs mode for ... way too long.

Any chance to get the cursor position by default at the end of the line in bash vi-mode when browsing through the shell history?

Was it helpful?

Solution

You need to remap the up and down cursors:

bind -m vi-command '"\201": previous-history'
bind -m vi-command '"\202": next-history'
bind -m vi-command '"\203": end-of-line'
bind -m vi-command '"\e[A": "\201\203"'
bind -m vi-command '"\e[B": "\202\203"'

This will map previous-history, next-history and end-of-line to non-existant keys, then map up and down to a sequence of those characters. You can do the same for j/k if you want, but I'd recommend leaving them alone. That way you can do both.

This only remaps the keys when you are in command mode. On my machine, the cursor is already at the end of the line in insert mode. If this is not true for you, you can do the same using vi-insert instead of vi-command as the keymap.

OTHER TIPS

I don't know of a way of doing this short of re-coding the history code inside of bash (or readline, really).

However, is it really so hard to enter Shift-A once you've found your line to start appending at the end?

If you're going to ditch emacs mode then, at some point, you're going to have to do it totally and accept the vi way of doing things. Join us, you know you want to :-)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top