You are not logged in.

#1 2008-09-09 19:15:54

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

switching readline to vi mode -- binding commands

Hey guys,

Every once and a while I switch my readline between emacs and vi mode just for the fun of messing up my fingermemory for the keymappings.

Today I decided it was time to switch back to vi mode, so I did (for those who don't know, add set editing-mode vi and set keymap vi to your input rc). So great but...

I have \C-p: history-search-backward in there as well to automatically search my current line whenever I press control P. This worked great in emacs mode. In vi mode I have to switch to command mode first (ESC) and then hit Control-P. I would like to know how to bind history-search-backward to Control-P in insert mode.

I figured out how to do this using a bash command:

bind -m vi-insert "\C-p":history-search-backward

Works like a charm... but I want this inputrc so I can get the same behaviour in my python interpreter as well. Any ideas?

Dusty

Offline

#2 2008-09-09 19:24:44

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: switching readline to vi mode -- binding commands

While I'm on the topic, is there a way to get a visual indicator of whether you're in vi-command or vi-insert mode while you're using bash? I'm thinking a hollow cursor for command mode and solid for insert...

Dusty

Offline

#3 2008-09-09 19:26:55

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: switching readline to vi mode -- binding commands

You can do:

$if mode=vi
"\C-p":history-search-backward
$endif

Offline

#4 2008-09-09 19:27:29

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: switching readline to vi mode -- binding commands

Dusty wrote:

While I'm on the topic, is there a way to get a visual indicator of whether you're in vi-command or vi-insert mode while you're using bash? I'm thinking a hollow cursor for command mode and solid for insert...

Nope. That's one of the reasons I stopped using vi mode.

Offline

#5 2008-09-09 19:38:00

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: switching readline to vi mode -- binding commands

phrakture wrote:

You can do:

$if mode=vi
"\C-p":history-search-backward
$endif

Hah! I had a bet with myself that it would be you who replied and looks like I won. ;-)


But...
It doesn't seem to work, nor variations without the quotes or changing to vi-insert...


Here's my inputrc at the moment:

set editing-mode vi
set keymap vi
set show-all-if-ambiguous on
set match-hidden-files off
set expand-tilde off
set print-completion-horizontally on
set convert-meta off
$if mode=vi
"\C-p": history-search-backward
$endif

If I type vi<control-P> I get:

vi^P

but if I type
vi<esc><control-P> I get:

vim .inputrc

Any ideas?

Offline

#6 2008-09-09 19:38:21

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: switching readline to vi mode -- binding commands

phrakture wrote:
Dusty wrote:

While I'm on the topic, is there a way to get a visual indicator of whether you're in vi-command or vi-insert mode while you're using bash? I'm thinking a hollow cursor for command mode and solid for insert...

Nope. That's one of the reasons I stopped using vi mode.

Rather than submitting a patch? smile

Dusty

Offline

#7 2008-09-09 21:39:31

rson451
Member
From: Annapolis, MD USA
Registered: 2007-04-15
Posts: 1,233
Website

Re: switching readline to vi mode -- binding commands

I'm not on a linux box atm, but did you try:

$if editing-mode=vi
  "\C-p": history-search-backward
$endif

archlinux - please read this and this — twice — then ask questions.
--
http://rsontech.net | http://github.com/rson

Offline

#8 2008-09-10 04:47:52

peets
Member
From: Montreal
Registered: 2007-01-11
Posts: 936
Website

Re: switching readline to vi mode -- binding commands

Dusty wrote:
phrakture wrote:
Dusty wrote:

While I'm on the topic, is there a way to get a visual indicator of whether you're in vi-command or vi-insert mode while you're using bash? I'm thinking a hollow cursor for command mode and solid for insert...

Nope. That's one of the reasons I stopped using vi mode.

Rather than submitting a patch? smile

Dusty

Me too. Are you sure the answer is "nope" though? If so, a patch is required, I'll see what I can do.

Offline

#9 2008-09-10 10:38:51

PeteMo
Member
From: H'Burg, VA
Registered: 2006-01-26
Posts: 191
Website

Re: switching readline to vi mode -- binding commands

I'm also using bash in vi mode but wanted to save some of the emacs keybindings...especially ctrl-l to clear the screen.  I ended up putting them in ~/.bashrc: 

bind '"\C-p": history-search-backward'

The binding works in other readline enabled apps, not just bash.

Your topic, though got me thinking on this again.  Looking at /etc/inputrc you can see bindings like:

"\e[1~": beginning-of-line
"\e[4~": end-of-line

so maybe we've just got the syntax wrong.

edit: apparently this only works without set keymap vi in ~/.inputrc

Last edited by PeteMo (2008-09-10 10:54:46)

Offline

#10 2008-09-10 12:39:37

nogoma
Member
From: Cranston, RI
Registered: 2006-03-01
Posts: 217

Re: switching readline to vi mode -- binding commands

Dusty wrote:
phrakture wrote:
Dusty wrote:

While I'm on the topic, is there a way to get a visual indicator of whether you're in vi-command or vi-insert mode while you're using bash? I'm thinking a hollow cursor for command mode and solid for insert...

Nope. That's one of the reasons I stopped using vi mode.

Rather than submitting a patch? smile

Dusty

I have my zsh set up to show "-- INSERT --" or "-- NORMAL --" above my prompt depending on editing mode by using zle features; not sure if there's an equivalent in bash.


-nogoma
---
Code Happy, Code Ruby!
http://www.last.fm/user/nogoma/

Offline

#11 2008-09-10 13:27:04

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: switching readline to vi mode -- binding commands

rson451 wrote:

I'm not on a linux box atm, but did you try:

$if editing-mode=vi
  "\C-p": history-search-backward
$endif

That didn't work either hmm

peets wrote:

Me too. Are you sure the answer is "nope" though? If so, a patch is required, I'll see what I can do.

I did a search for 'readline vi visual mode indicator' on google and the top result was this thread. Similar searches don't yield any tutorials, but interestingly, they also don't yield any requests for this feature. I can't imagine I'm the first to think of it, so that must mean google sucks.

PeteMo wrote:

The binding works in other readline enabled apps, not just bash.

It doesn't work in python for me... ;/ vi mode is enabled, but putting the bind in .bashrc does not enable the ctrl-p keybinding in the python interpreter.

I'm starting to train myself to use <esc>/command and the n and p commands to move through results, but I really think the ctrl-P is a better option fro me. I use ctrl-P in vim all the time for completion, so it makes sense to use it on the commandline for the same purpose.

Dusty

Offline

#12 2008-10-01 03:15:36

Alp
Member
Registered: 2008-09-27
Posts: 79

Re: switching readline to vi mode -- binding commands

I can't get the history-search-back and -forward to work.

My .inputrc:

"\e[1~": beginning-of-line
"\e[4~": end-of-line
"\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\e[C": forward-word
"\e\e[D": backward-word
set completion-ignore-case off
set bell-style none
set expand-tilde on
set convert-meta off
set input-meta on
set output-meta on
set show-all-if-ambiguous on
set visible-stats on

Should there be an include command in any other file for .inputrc? Why my terminal (gnome-terminal atm) does not respond to the page up and down keys? In fact, it ignores page up and prints "~" when hitting page down. It seems that i can't fix that. What now?

Offline

#13 2008-10-03 15:59:38

kazuo
Member
From: São Paulo/Brazil
Registered: 2008-03-18
Posts: 413
Website

Re: switching readline to vi mode -- binding commands

phrakture wrote:
Dusty wrote:

While I'm on the topic, is there a way to get a visual indicator of whether you're in vi-command or vi-insert mode while you're using bash? I'm thinking a hollow cursor for command mode and solid for insert...

Nope. That's one of the reasons I stopped using vi mode.

I use screen and zsh. Zsh have a widget that is called every time you change the mode (from normal to insert) I used it to chance my hardstatus of screen to show the mode. Look at screenshot:
zsh_vi_screen_t.png

Full: http://www.soc.if.usp.br/~kazuo/zsh_vi_screen_f.png

You need to put something like this in zshrc:

show_mode() { printf '\e_--%s--\e\' $1 }
bindkey -v
show_mode INSERT
zle-keymap-select() {
  if [[ $KEYMAP == vicmd ]]; then
    show_mode NORMAL
  else
    show_mode INSERT
  fi
}
zle -N zle-keymap-select

(I put some cases in showmode for other terms too...)

And put a '%h' at the end of you hardstatus in screenrc. Like this:

hardstatus alwayslastline "  %{= kK}%-w%{+b W}%n %t%{-}%+w%=%{+b W}%h%{= dd}  "

For me this is working great.

Offline

#14 2008-10-03 16:57:45

rson451
Member
From: Annapolis, MD USA
Registered: 2007-04-15
Posts: 1,233
Website

Re: switching readline to vi mode -- binding commands

That's pretty nifty.  Thanks.


archlinux - please read this and this — twice — then ask questions.
--
http://rsontech.net | http://github.com/rson

Offline

#15 2008-10-04 09:16:55

Nikitka
Member
From: Zelenograd, Moscow, Russia
Registered: 2007-12-26
Posts: 11

Re: switching readline to vi mode -- binding commands

Hello Arch-people!
This is my .inputrc file.

set editing-mode vi
$if mode=vi
set keymap vi-command
# these are for vi-command mode
"\e[A": history-search-backward
"\e[B": history-search-forward
set keymap vi-insert
# these are for vi-insert mode
"\e[A": history-search-backward
"\e[B": history-search-forward
$endif

It works.

Offline

#16 2008-10-04 13:41:07

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: switching readline to vi mode -- binding commands

Nikitka: You registered in 2007, yet haven't posted here until today? 

Most useful first post!!

Your solution works flawlessly, thank you very much. I really appreciate it.

Dusty

Offline

#17 2010-10-11 19:31:47

mirci
Member
Registered: 2010-09-25
Posts: 7

Re: switching readline to vi mode -- binding commands

Dusty wrote:

While I'm on the topic, is there a way to get a visual indicator of whether you're in vi-command or vi-insert mode while you're using bash? I'm thinking a hollow cursor for command mode and solid for insert...

Dusty

I've created the readline patch along with some visualization script.
Have a look here.

Offline

#18 2010-11-10 06:00:43

Inxsible
Forum Fellow
From: Chicago
Registered: 2008-06-09
Posts: 9,059

Re: switching readline to vi mode -- binding commands

mirci.... maybe you should create a thread in the Community Contributions. It will probably help out more users that way.

Closing this one for necro-bumping.


Forum Rules

There's no such thing as a stupid question, but there sure are a lot of inquisitive idiots !

Offline

Board footer

Powered by FluxBB